1 import { Component, linkEvent } from 'inferno';
2 import { Link } from 'inferno-router';
3 import { Subscription } from 'rxjs';
4 import { retryWhen, delay, take } from 'rxjs/operators';
21 } from '../interfaces';
22 import { WebSocketService, UserService } from '../services';
27 capitalizeFirstLetter,
31 import { PostListing } from './post-listing';
32 import { SortSelect } from './sort-select';
33 import { ListingTypeSelect } from './listing-type-select';
34 import { CommentNodes } from './comment-nodes';
35 import { MomentTime } from './moment-time';
36 import { i18n } from '../i18next';
37 import { T } from 'inferno-i18next';
50 follows: Array<CommunityUser>;
51 moderates: Array<CommunityUser>;
52 comments: Array<Comment>;
55 admins: Array<UserView>;
60 userSettingsForm: UserSettingsForm;
61 userSettingsLoading: boolean;
62 deleteAccountLoading: boolean;
63 deleteAccountShowConfirm: boolean;
64 deleteAccountForm: DeleteAccountForm;
67 export class User extends Component<any, UserState> {
68 private subscription: Subscription;
69 private emptyState: UserState = {
75 number_of_posts: null,
77 number_of_comments: null,
89 view: this.getViewFromProps(this.props),
90 sort: this.getSortTypeFromProps(this.props),
91 page: this.getPageFromProps(this.props),
95 default_sort_type: null,
96 default_listing_type: null,
99 userSettingsLoading: null,
100 deleteAccountLoading: null,
101 deleteAccountShowConfirm: false,
107 constructor(props: any, context: any) {
108 super(props, context);
110 this.state = this.emptyState;
111 this.handleSortChange = this.handleSortChange.bind(this);
112 this.handleUserSettingsSortTypeChange = this.handleUserSettingsSortTypeChange.bind(
115 this.handleUserSettingsListingTypeChange = this.handleUserSettingsListingTypeChange.bind(
119 this.state.user_id = Number(this.props.match.params.id);
120 this.state.username = this.props.match.params.username;
122 this.subscription = WebSocketService.Instance.subject
132 msg => this.parseMessage(msg),
133 err => console.error(err),
134 () => console.log('complete')
140 get isCurrentUser() {
142 UserService.Instance.user &&
143 UserService.Instance.user.id == this.state.user.id
147 getViewFromProps(props: any): View {
148 return props.match.params.view
149 ? View[capitalizeFirstLetter(props.match.params.view)]
153 getSortTypeFromProps(props: any): SortType {
154 return props.match.params.sort
155 ? routeSortTypeToEnum(props.match.params.sort)
159 getPageFromProps(props: any): number {
160 return props.match.params.page ? Number(props.match.params.page) : 1;
163 componentWillUnmount() {
164 this.subscription.unsubscribe();
167 // Necessary for back button for some reason
168 componentWillReceiveProps(nextProps: any) {
170 nextProps.history.action == 'POP' ||
171 nextProps.history.action == 'PUSH'
173 this.state.view = this.getViewFromProps(nextProps);
174 this.state.sort = this.getSortTypeFromProps(nextProps);
175 this.state.page = this.getPageFromProps(nextProps);
176 this.setState(this.state);
181 componentDidUpdate(lastProps: any, _lastState: UserState, _snapshot: any) {
182 // Necessary if you are on a post and you click another post (same route)
184 lastProps.location.pathname.split('/')[2] !==
185 lastProps.history.location.pathname.split('/')[2]
187 // Couldnt get a refresh working. This does for now.
194 <div class="container">
195 {this.state.loading ? (
197 <svg class="icon icon-spinner spin">
198 <use xlinkHref="#icon-spinner"></use>
203 <div class="col-12 col-md-8">
204 <h5>/u/{this.state.user.name}</h5>
206 {this.state.view == View.Overview && this.overview()}
207 {this.state.view == View.Comments && this.comments()}
208 {this.state.view == View.Posts && this.posts()}
209 {this.state.view == View.Saved && this.overview()}
212 <div class="col-12 col-md-4">
214 {this.isCurrentUser && this.userSettings()}
226 <div className="mb-2">
228 value={this.state.view}
229 onChange={linkEvent(this, this.handleViewChange)}
230 class="custom-select custom-select-sm w-auto"
233 <T i18nKey="view">#</T>
235 <option value={View.Overview}>
236 <T i18nKey="overview">#</T>
238 <option value={View.Comments}>
239 <T i18nKey="comments">#</T>
241 <option value={View.Posts}>
242 <T i18nKey="posts">#</T>
244 <option value={View.Saved}>
245 <T i18nKey="saved">#</T>
250 sort={this.state.sort}
251 onChange={this.handleSortChange}
256 href={`/feeds/u/${this.state.username}.xml?sort=${
257 SortType[this.state.sort]
261 <svg class="icon mx-2 text-muted small">
262 <use xlinkHref="#icon-rss">#</use>
270 let combined: Array<{ type_: string; data: Comment | Post }> = [];
271 let comments = this.state.comments.map(e => {
272 return { type_: 'comments', data: e };
274 let posts = this.state.posts.map(e => {
275 return { type_: 'posts', data: e };
278 combined.push(...comments);
279 combined.push(...posts);
282 if (this.state.sort == SortType.New) {
283 combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
285 combined.sort((a, b) => b.data.score - a.data.score);
292 {i.type_ == 'posts' ? (
294 post={i.data as Post}
295 admins={this.state.admins}
301 nodes={[{ comment: i.data as Comment }]}
302 admins={this.state.admins}
315 {this.state.comments.map(comment => (
317 nodes={[{ comment: comment }]}
318 admins={this.state.admins}
329 {this.state.posts.map(post => (
332 admins={this.state.admins}
342 let user = this.state.user;
345 <div class="card border-secondary mb-3">
346 <div class="card-body">
348 <ul class="list-inline mb-0">
349 <li className="list-inline-item">{user.name}</li>
351 <li className="list-inline-item badge badge-danger">
352 <T i18nKey="banned">#</T>
358 {i18n.t('joined')} <MomentTime data={user} />
360 <div class="table-responsive">
361 <table class="table table-bordered table-sm mt-2 mb-0">
365 i18nKey="number_of_points"
366 interpolation={{ count: user.post_score }}
373 i18nKey="number_of_posts"
374 interpolation={{ count: user.number_of_posts }}
383 i18nKey="number_of_points"
384 interpolation={{ count: user.comment_score }}
391 i18nKey="number_of_comments"
392 interpolation={{ count: user.number_of_comments }}
400 {this.isCurrentUser && (
402 class="btn btn-block btn-secondary mt-3"
403 onClick={linkEvent(this, this.handleLogoutClick)}
405 <T i18nKey="logout">#</T>
417 <div class="card border-secondary mb-3">
418 <div class="card-body">
420 <T i18nKey="settings">#</T>
422 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
423 <div class="form-group">
426 <T i18nKey="theme">#</T>
429 value={this.state.userSettingsForm.theme}
432 this.handleUserSettingsThemeChange
434 class="ml-2 custom-select custom-select-sm w-auto"
437 <T i18nKey="theme">#</T>
439 {themes.map(theme => (
440 <option value={theme}>{theme}</option>
445 <form className="form-group">
448 <T i18nKey="sort_type" class="mr-2">
453 type_={this.state.userSettingsForm.default_listing_type}
454 onChange={this.handleUserSettingsListingTypeChange}
458 <form className="form-group">
461 <T i18nKey="type" class="mr-2">
466 sort={this.state.userSettingsForm.default_sort_type}
467 onChange={this.handleUserSettingsSortTypeChange}
471 <div class="form-group">
473 <div class="form-check">
475 class="form-check-input"
477 checked={this.state.userSettingsForm.show_nsfw}
480 this.handleUserSettingsShowNsfwChange
483 <label class="form-check-label">
484 <T i18nKey="show_nsfw">#</T>
489 <div class="form-group">
493 class="btn btn-block btn-secondary mr-4"
495 {this.state.userSettingsLoading ? (
496 <svg class="icon icon-spinner spin">
497 <use xlinkHref="#icon-spinner"></use>
500 capitalizeFirstLetter(i18n.t('save'))
506 <div class="form-group mb-0">
509 class="btn btn-block btn-danger"
512 this.handleDeleteAccountShowConfirmToggle
515 <T i18nKey="delete_account">#</T>
517 {this.state.deleteAccountShowConfirm && (
519 <div class="my-2 alert alert-danger" role="alert">
520 <T i18nKey="delete_account_confirm">#</T>
524 value={this.state.deleteAccountForm.password}
527 this.handleDeleteAccountPasswordChange
529 class="form-control my-2"
532 class="btn btn-danger mr-4"
533 disabled={!this.state.deleteAccountForm.password}
534 onClick={linkEvent(this, this.handleDeleteAccount)}
536 {this.state.deleteAccountLoading ? (
537 <svg class="icon icon-spinner spin">
538 <use xlinkHref="#icon-spinner"></use>
541 capitalizeFirstLetter(i18n.t('delete'))
545 class="btn btn-secondary"
548 this.handleDeleteAccountShowConfirmToggle
551 <T i18nKey="cancel">#</T>
567 {this.state.moderates.length > 0 && (
568 <div class="card border-secondary mb-3">
569 <div class="card-body">
571 <T i18nKey="moderates">#</T>
573 <ul class="list-unstyled mb-0">
574 {this.state.moderates.map(community => (
576 <Link to={`/c/${community.community_name}`}>
577 {community.community_name}
592 {this.state.follows.length > 0 && (
593 <div class="card border-secondary mb-3">
594 <div class="card-body">
596 <T i18nKey="subscribed">#</T>
598 <ul class="list-unstyled mb-0">
599 {this.state.follows.map(community => (
601 <Link to={`/c/${community.community_name}`}>
602 {community.community_name}
617 {this.state.page > 1 && (
619 class="btn btn-sm btn-secondary mr-1"
620 onClick={linkEvent(this, this.prevPage)}
622 <T i18nKey="prev">#</T>
626 class="btn btn-sm btn-secondary"
627 onClick={linkEvent(this, this.nextPage)}
629 <T i18nKey="next">#</T>
636 let viewStr = View[this.state.view].toLowerCase();
637 let sortStr = SortType[this.state.sort].toLowerCase();
638 this.props.history.push(
639 `/u/${this.state.user.name}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`
658 let form: GetUserDetailsForm = {
659 user_id: this.state.user_id,
660 username: this.state.username,
661 sort: SortType[this.state.sort],
662 saved_only: this.state.view == View.Saved,
663 page: this.state.page,
666 WebSocketService.Instance.getUserDetails(form);
669 handleSortChange(val: SortType) {
670 this.state.sort = val;
672 this.setState(this.state);
677 handleViewChange(i: User, event: any) {
678 i.state.view = Number(event.target.value);
685 handleUserSettingsShowNsfwChange(i: User, event: any) {
686 i.state.userSettingsForm.show_nsfw = event.target.checked;
690 handleUserSettingsThemeChange(i: User, event: any) {
691 i.state.userSettingsForm.theme = event.target.value;
692 setTheme(event.target.value);
696 handleUserSettingsSortTypeChange(val: SortType) {
697 this.state.userSettingsForm.default_sort_type = val;
698 this.setState(this.state);
701 handleUserSettingsListingTypeChange(val: ListingType) {
702 this.state.userSettingsForm.default_listing_type = val;
703 this.setState(this.state);
706 handleUserSettingsSubmit(i: User, event: any) {
707 event.preventDefault();
708 i.state.userSettingsLoading = true;
711 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
714 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
715 event.preventDefault();
716 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
720 handleDeleteAccountPasswordChange(i: User, event: any) {
721 i.state.deleteAccountForm.password = event.target.value;
725 handleLogoutClick(i: User) {
726 UserService.Instance.logout();
727 i.context.router.history.push('/');
730 handleDeleteAccount(i: User, event: any) {
731 event.preventDefault();
732 i.state.deleteAccountLoading = true;
735 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
738 parseMessage(msg: any) {
740 let op: UserOperation = msgOp(msg);
742 alert(i18n.t(msg.error));
743 this.state.deleteAccountLoading = false;
744 this.setState(this.state);
746 } else if (op == UserOperation.GetUserDetails) {
747 let res: UserDetailsResponse = msg;
748 this.state.user = res.user;
749 this.state.comments = res.comments;
750 this.state.follows = res.follows;
751 this.state.moderates = res.moderates;
752 this.state.posts = res.posts;
753 this.state.admins = res.admins;
754 this.state.loading = false;
755 if (this.isCurrentUser) {
756 this.state.userSettingsForm.show_nsfw =
757 UserService.Instance.user.show_nsfw;
758 this.state.userSettingsForm.theme = UserService.Instance.user.theme
759 ? UserService.Instance.user.theme
761 this.state.userSettingsForm.default_sort_type =
762 UserService.Instance.user.default_sort_type;
763 this.state.userSettingsForm.default_listing_type =
764 UserService.Instance.user.default_listing_type;
766 document.title = `/u/${this.state.user.name} - ${WebSocketService.Instance.site.name}`;
767 window.scrollTo(0, 0);
768 this.setState(this.state);
769 } else if (op == UserOperation.EditComment) {
770 let res: CommentResponse = msg;
772 let found = this.state.comments.find(c => c.id == res.comment.id);
773 found.content = res.comment.content;
774 found.updated = res.comment.updated;
775 found.removed = res.comment.removed;
776 found.deleted = res.comment.deleted;
777 found.upvotes = res.comment.upvotes;
778 found.downvotes = res.comment.downvotes;
779 found.score = res.comment.score;
781 this.setState(this.state);
782 } else if (op == UserOperation.CreateComment) {
783 // let res: CommentResponse = msg;
784 alert(i18n.t('reply_sent'));
785 // this.state.comments.unshift(res.comment); // TODO do this right
786 // this.setState(this.state);
787 } else if (op == UserOperation.SaveComment) {
788 let res: CommentResponse = msg;
789 let found = this.state.comments.find(c => c.id == res.comment.id);
790 found.saved = res.comment.saved;
791 this.setState(this.state);
792 } else if (op == UserOperation.CreateCommentLike) {
793 let res: CommentResponse = msg;
794 let found: Comment = this.state.comments.find(
795 c => c.id === res.comment.id
797 found.score = res.comment.score;
798 found.upvotes = res.comment.upvotes;
799 found.downvotes = res.comment.downvotes;
800 if (res.comment.my_vote !== null) found.my_vote = res.comment.my_vote;
801 this.setState(this.state);
802 } else if (op == UserOperation.BanUser) {
803 let res: BanUserResponse = msg;
805 .filter(c => c.creator_id == res.user.id)
806 .forEach(c => (c.banned = res.banned));
808 .filter(c => c.creator_id == res.user.id)
809 .forEach(c => (c.banned = res.banned));
810 this.setState(this.state);
811 } else if (op == UserOperation.AddAdmin) {
812 let res: AddAdminResponse = msg;
813 this.state.admins = res.admins;
814 this.setState(this.state);
815 } else if (op == UserOperation.SaveUserSettings) {
816 this.state = this.emptyState;
817 this.state.userSettingsLoading = false;
818 this.setState(this.state);
819 let res: LoginResponse = msg;
820 UserService.Instance.login(res);
821 } else if (op == UserOperation.DeleteAccount) {
822 this.state.deleteAccountLoading = false;
823 this.state.deleteAccountShowConfirm = false;
824 this.setState(this.state);
825 this.context.router.history.push('/');