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);
183 <div class="container">
184 {this.state.loading ? (
186 <svg class="icon icon-spinner spin">
187 <use xlinkHref="#icon-spinner"></use>
192 <div class="col-12 col-md-8">
193 <h5>/u/{this.state.user.name}</h5>
195 {this.state.view == View.Overview && this.overview()}
196 {this.state.view == View.Comments && this.comments()}
197 {this.state.view == View.Posts && this.posts()}
198 {this.state.view == View.Saved && this.overview()}
201 <div class="col-12 col-md-4">
203 {this.isCurrentUser && this.userSettings()}
215 <div className="mb-2">
217 value={this.state.view}
218 onChange={linkEvent(this, this.handleViewChange)}
219 class="custom-select custom-select-sm w-auto"
222 <T i18nKey="view">#</T>
224 <option value={View.Overview}>
225 <T i18nKey="overview">#</T>
227 <option value={View.Comments}>
228 <T i18nKey="comments">#</T>
230 <option value={View.Posts}>
231 <T i18nKey="posts">#</T>
233 <option value={View.Saved}>
234 <T i18nKey="saved">#</T>
239 sort={this.state.sort}
240 onChange={this.handleSortChange}
249 let combined: Array<{ type_: string; data: Comment | Post }> = [];
250 let comments = this.state.comments.map(e => {
251 return { type_: 'comments', data: e };
253 let posts = this.state.posts.map(e => {
254 return { type_: 'posts', data: e };
257 combined.push(...comments);
258 combined.push(...posts);
261 if (this.state.sort == SortType.New) {
262 combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
264 combined.sort((a, b) => b.data.score - a.data.score);
271 {i.type_ == 'posts' ? (
273 post={i.data as Post}
274 admins={this.state.admins}
280 nodes={[{ comment: i.data as Comment }]}
281 admins={this.state.admins}
294 {this.state.comments.map(comment => (
296 nodes={[{ comment: comment }]}
297 admins={this.state.admins}
308 {this.state.posts.map(post => (
311 admins={this.state.admins}
321 let user = this.state.user;
324 <div class="card border-secondary mb-3">
325 <div class="card-body">
327 <ul class="list-inline mb-0">
328 <li className="list-inline-item">{user.name}</li>
330 <li className="list-inline-item badge badge-danger">
331 <T i18nKey="banned">#</T>
337 {i18n.t('joined')} <MomentTime data={user} />
339 <div class="table-responsive">
340 <table class="table table-bordered table-sm mt-2 mb-0">
344 i18nKey="number_of_points"
345 interpolation={{ count: user.post_score }}
352 i18nKey="number_of_posts"
353 interpolation={{ count: user.number_of_posts }}
362 i18nKey="number_of_points"
363 interpolation={{ count: user.comment_score }}
370 i18nKey="number_of_comments"
371 interpolation={{ count: user.number_of_comments }}
379 {this.isCurrentUser && (
381 class="btn btn-block btn-secondary mt-3"
382 onClick={linkEvent(this, this.handleLogoutClick)}
384 <T i18nKey="logout">#</T>
396 <div class="card border-secondary mb-3">
397 <div class="card-body">
399 <T i18nKey="settings">#</T>
401 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
402 <div class="form-group">
405 <T i18nKey="theme">#</T>
408 value={this.state.userSettingsForm.theme}
411 this.handleUserSettingsThemeChange
413 class="ml-2 custom-select custom-select-sm w-auto"
416 <T i18nKey="theme">#</T>
418 {themes.map(theme => (
419 <option value={theme}>{theme}</option>
424 <form className="form-group">
427 <T i18nKey="sort_type" class="mr-2">
432 type_={this.state.userSettingsForm.default_listing_type}
433 onChange={this.handleUserSettingsListingTypeChange}
437 <form className="form-group">
440 <T i18nKey="type" class="mr-2">
445 sort={this.state.userSettingsForm.default_sort_type}
446 onChange={this.handleUserSettingsSortTypeChange}
450 <div class="form-group">
452 <div class="form-check">
454 class="form-check-input"
456 checked={this.state.userSettingsForm.show_nsfw}
459 this.handleUserSettingsShowNsfwChange
462 <label class="form-check-label">
463 <T i18nKey="show_nsfw">#</T>
468 <div class="form-group">
472 class="btn btn-block btn-secondary mr-4"
474 {this.state.userSettingsLoading ? (
475 <svg class="icon icon-spinner spin">
476 <use xlinkHref="#icon-spinner"></use>
479 capitalizeFirstLetter(i18n.t('save'))
485 <div class="form-group mb-0">
488 class="btn btn-block btn-danger"
491 this.handleDeleteAccountShowConfirmToggle
494 <T i18nKey="delete_account">#</T>
496 {this.state.deleteAccountShowConfirm && (
498 <div class="my-2 alert alert-danger" role="alert">
499 <T i18nKey="delete_account_confirm">#</T>
503 value={this.state.deleteAccountForm.password}
506 this.handleDeleteAccountPasswordChange
508 class="form-control my-2"
511 class="btn btn-danger mr-4"
512 disabled={!this.state.deleteAccountForm.password}
513 onClick={linkEvent(this, this.handleDeleteAccount)}
515 {this.state.deleteAccountLoading ? (
516 <svg class="icon icon-spinner spin">
517 <use xlinkHref="#icon-spinner"></use>
520 capitalizeFirstLetter(i18n.t('delete'))
524 class="btn btn-secondary"
527 this.handleDeleteAccountShowConfirmToggle
530 <T i18nKey="cancel">#</T>
546 {this.state.moderates.length > 0 && (
547 <div class="card border-secondary mb-3">
548 <div class="card-body">
550 <T i18nKey="moderates">#</T>
552 <ul class="list-unstyled mb-0">
553 {this.state.moderates.map(community => (
555 <Link to={`/c/${community.community_name}`}>
556 {community.community_name}
571 {this.state.follows.length > 0 && (
572 <div class="card border-secondary mb-3">
573 <div class="card-body">
575 <T i18nKey="subscribed">#</T>
577 <ul class="list-unstyled mb-0">
578 {this.state.follows.map(community => (
580 <Link to={`/c/${community.community_name}`}>
581 {community.community_name}
596 {this.state.page > 1 && (
598 class="btn btn-sm btn-secondary mr-1"
599 onClick={linkEvent(this, this.prevPage)}
601 <T i18nKey="prev">#</T>
605 class="btn btn-sm btn-secondary"
606 onClick={linkEvent(this, this.nextPage)}
608 <T i18nKey="next">#</T>
615 let viewStr = View[this.state.view].toLowerCase();
616 let sortStr = SortType[this.state.sort].toLowerCase();
617 this.props.history.push(
618 `/u/${this.state.user.name}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`
637 let form: GetUserDetailsForm = {
638 user_id: this.state.user_id,
639 username: this.state.username,
640 sort: SortType[this.state.sort],
641 saved_only: this.state.view == View.Saved,
642 page: this.state.page,
645 WebSocketService.Instance.getUserDetails(form);
648 handleSortChange(val: SortType) {
649 this.state.sort = val;
651 this.setState(this.state);
656 handleViewChange(i: User, event: any) {
657 i.state.view = Number(event.target.value);
664 handleUserSettingsShowNsfwChange(i: User, event: any) {
665 i.state.userSettingsForm.show_nsfw = event.target.checked;
669 handleUserSettingsThemeChange(i: User, event: any) {
670 i.state.userSettingsForm.theme = event.target.value;
671 setTheme(event.target.value);
675 handleUserSettingsSortTypeChange(val: SortType) {
676 this.state.userSettingsForm.default_sort_type = val;
677 this.setState(this.state);
680 handleUserSettingsListingTypeChange(val: ListingType) {
681 this.state.userSettingsForm.default_listing_type = val;
682 this.setState(this.state);
685 handleUserSettingsSubmit(i: User, event: any) {
686 event.preventDefault();
687 i.state.userSettingsLoading = true;
690 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
693 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
694 event.preventDefault();
695 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
699 handleDeleteAccountPasswordChange(i: User, event: any) {
700 i.state.deleteAccountForm.password = event.target.value;
704 handleLogoutClick(i: User) {
705 UserService.Instance.logout();
706 i.context.router.history.push('/');
709 handleDeleteAccount(i: User, event: any) {
710 event.preventDefault();
711 i.state.deleteAccountLoading = true;
714 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
717 parseMessage(msg: any) {
719 let op: UserOperation = msgOp(msg);
721 alert(i18n.t(msg.error));
722 this.state.deleteAccountLoading = false;
723 this.setState(this.state);
725 } else if (op == UserOperation.GetUserDetails) {
726 let res: UserDetailsResponse = msg;
727 this.state.user = res.user;
728 this.state.comments = res.comments;
729 this.state.follows = res.follows;
730 this.state.moderates = res.moderates;
731 this.state.posts = res.posts;
732 this.state.admins = res.admins;
733 this.state.loading = false;
734 if (this.isCurrentUser) {
735 this.state.userSettingsForm.show_nsfw =
736 UserService.Instance.user.show_nsfw;
737 this.state.userSettingsForm.theme = UserService.Instance.user.theme
738 ? UserService.Instance.user.theme
740 this.state.userSettingsForm.default_sort_type =
741 UserService.Instance.user.default_sort_type;
742 this.state.userSettingsForm.default_listing_type =
743 UserService.Instance.user.default_listing_type;
745 document.title = `/u/${this.state.user.name} - ${WebSocketService.Instance.site.name}`;
746 window.scrollTo(0, 0);
747 this.setState(this.state);
748 } else if (op == UserOperation.EditComment) {
749 let res: CommentResponse = msg;
751 let found = this.state.comments.find(c => c.id == res.comment.id);
752 found.content = res.comment.content;
753 found.updated = res.comment.updated;
754 found.removed = res.comment.removed;
755 found.deleted = res.comment.deleted;
756 found.upvotes = res.comment.upvotes;
757 found.downvotes = res.comment.downvotes;
758 found.score = res.comment.score;
760 this.setState(this.state);
761 } else if (op == UserOperation.CreateComment) {
762 // let res: CommentResponse = msg;
763 alert(i18n.t('reply_sent'));
764 // this.state.comments.unshift(res.comment); // TODO do this right
765 // this.setState(this.state);
766 } else if (op == UserOperation.SaveComment) {
767 let res: CommentResponse = msg;
768 let found = this.state.comments.find(c => c.id == res.comment.id);
769 found.saved = res.comment.saved;
770 this.setState(this.state);
771 } else if (op == UserOperation.CreateCommentLike) {
772 let res: CommentResponse = msg;
773 let found: Comment = this.state.comments.find(
774 c => c.id === res.comment.id
776 found.score = res.comment.score;
777 found.upvotes = res.comment.upvotes;
778 found.downvotes = res.comment.downvotes;
779 if (res.comment.my_vote !== null) found.my_vote = res.comment.my_vote;
780 this.setState(this.state);
781 } else if (op == UserOperation.BanUser) {
782 let res: BanUserResponse = msg;
784 .filter(c => c.creator_id == res.user.id)
785 .forEach(c => (c.banned = res.banned));
787 .filter(c => c.creator_id == res.user.id)
788 .forEach(c => (c.banned = res.banned));
789 this.setState(this.state);
790 } else if (op == UserOperation.AddAdmin) {
791 let res: AddAdminResponse = msg;
792 this.state.admins = res.admins;
793 this.setState(this.state);
794 } else if (op == UserOperation.SaveUserSettings) {
795 this.state = this.emptyState;
796 this.state.userSettingsLoading = false;
797 this.setState(this.state);
798 let res: LoginResponse = msg;
799 UserService.Instance.login(res);
800 } else if (op == UserOperation.DeleteAccount) {
801 this.state.deleteAccountLoading = false;
802 this.state.deleteAccountShowConfirm = false;
803 this.setState(this.state);
804 this.context.router.history.push('/');