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)
183 if (lastProps.location.pathname !== lastProps.history.location.pathname) {
184 // Couldnt get a refresh working. This does for now.
191 <div class="container">
192 {this.state.loading ? (
194 <svg class="icon icon-spinner spin">
195 <use xlinkHref="#icon-spinner"></use>
200 <div class="col-12 col-md-8">
201 <h5>/u/{this.state.user.name}</h5>
203 {this.state.view == View.Overview && this.overview()}
204 {this.state.view == View.Comments && this.comments()}
205 {this.state.view == View.Posts && this.posts()}
206 {this.state.view == View.Saved && this.overview()}
209 <div class="col-12 col-md-4">
211 {this.isCurrentUser && this.userSettings()}
223 <div className="mb-2">
225 value={this.state.view}
226 onChange={linkEvent(this, this.handleViewChange)}
227 class="custom-select custom-select-sm w-auto"
230 <T i18nKey="view">#</T>
232 <option value={View.Overview}>
233 <T i18nKey="overview">#</T>
235 <option value={View.Comments}>
236 <T i18nKey="comments">#</T>
238 <option value={View.Posts}>
239 <T i18nKey="posts">#</T>
241 <option value={View.Saved}>
242 <T i18nKey="saved">#</T>
247 sort={this.state.sort}
248 onChange={this.handleSortChange}
257 let combined: Array<{ type_: string; data: Comment | Post }> = [];
258 let comments = this.state.comments.map(e => {
259 return { type_: 'comments', data: e };
261 let posts = this.state.posts.map(e => {
262 return { type_: 'posts', data: e };
265 combined.push(...comments);
266 combined.push(...posts);
269 if (this.state.sort == SortType.New) {
270 combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
272 combined.sort((a, b) => b.data.score - a.data.score);
279 {i.type_ == 'posts' ? (
281 post={i.data as Post}
282 admins={this.state.admins}
288 nodes={[{ comment: i.data as Comment }]}
289 admins={this.state.admins}
302 {this.state.comments.map(comment => (
304 nodes={[{ comment: comment }]}
305 admins={this.state.admins}
316 {this.state.posts.map(post => (
319 admins={this.state.admins}
329 let user = this.state.user;
332 <div class="card border-secondary mb-3">
333 <div class="card-body">
335 <ul class="list-inline mb-0">
336 <li className="list-inline-item">{user.name}</li>
338 <li className="list-inline-item badge badge-danger">
339 <T i18nKey="banned">#</T>
345 {i18n.t('joined')} <MomentTime data={user} />
347 <div class="table-responsive">
348 <table class="table table-bordered table-sm mt-2 mb-0">
352 i18nKey="number_of_points"
353 interpolation={{ count: user.post_score }}
360 i18nKey="number_of_posts"
361 interpolation={{ count: user.number_of_posts }}
370 i18nKey="number_of_points"
371 interpolation={{ count: user.comment_score }}
378 i18nKey="number_of_comments"
379 interpolation={{ count: user.number_of_comments }}
387 {this.isCurrentUser && (
389 class="btn btn-block btn-secondary mt-3"
390 onClick={linkEvent(this, this.handleLogoutClick)}
392 <T i18nKey="logout">#</T>
404 <div class="card border-secondary mb-3">
405 <div class="card-body">
407 <T i18nKey="settings">#</T>
409 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
410 <div class="form-group">
413 <T i18nKey="theme">#</T>
416 value={this.state.userSettingsForm.theme}
419 this.handleUserSettingsThemeChange
421 class="ml-2 custom-select custom-select-sm w-auto"
424 <T i18nKey="theme">#</T>
426 {themes.map(theme => (
427 <option value={theme}>{theme}</option>
432 <form className="form-group">
435 <T i18nKey="sort_type" class="mr-2">
440 type_={this.state.userSettingsForm.default_listing_type}
441 onChange={this.handleUserSettingsListingTypeChange}
445 <form className="form-group">
448 <T i18nKey="type" class="mr-2">
453 sort={this.state.userSettingsForm.default_sort_type}
454 onChange={this.handleUserSettingsSortTypeChange}
458 <div class="form-group">
460 <div class="form-check">
462 class="form-check-input"
464 checked={this.state.userSettingsForm.show_nsfw}
467 this.handleUserSettingsShowNsfwChange
470 <label class="form-check-label">
471 <T i18nKey="show_nsfw">#</T>
476 <div class="form-group">
480 class="btn btn-block btn-secondary mr-4"
482 {this.state.userSettingsLoading ? (
483 <svg class="icon icon-spinner spin">
484 <use xlinkHref="#icon-spinner"></use>
487 capitalizeFirstLetter(i18n.t('save'))
493 <div class="form-group mb-0">
496 class="btn btn-block btn-danger"
499 this.handleDeleteAccountShowConfirmToggle
502 <T i18nKey="delete_account">#</T>
504 {this.state.deleteAccountShowConfirm && (
506 <div class="my-2 alert alert-danger" role="alert">
507 <T i18nKey="delete_account_confirm">#</T>
511 value={this.state.deleteAccountForm.password}
514 this.handleDeleteAccountPasswordChange
516 class="form-control my-2"
519 class="btn btn-danger mr-4"
520 disabled={!this.state.deleteAccountForm.password}
521 onClick={linkEvent(this, this.handleDeleteAccount)}
523 {this.state.deleteAccountLoading ? (
524 <svg class="icon icon-spinner spin">
525 <use xlinkHref="#icon-spinner"></use>
528 capitalizeFirstLetter(i18n.t('delete'))
532 class="btn btn-secondary"
535 this.handleDeleteAccountShowConfirmToggle
538 <T i18nKey="cancel">#</T>
554 {this.state.moderates.length > 0 && (
555 <div class="card border-secondary mb-3">
556 <div class="card-body">
558 <T i18nKey="moderates">#</T>
560 <ul class="list-unstyled mb-0">
561 {this.state.moderates.map(community => (
563 <Link to={`/c/${community.community_name}`}>
564 {community.community_name}
579 {this.state.follows.length > 0 && (
580 <div class="card border-secondary mb-3">
581 <div class="card-body">
583 <T i18nKey="subscribed">#</T>
585 <ul class="list-unstyled mb-0">
586 {this.state.follows.map(community => (
588 <Link to={`/c/${community.community_name}`}>
589 {community.community_name}
604 {this.state.page > 1 && (
606 class="btn btn-sm btn-secondary mr-1"
607 onClick={linkEvent(this, this.prevPage)}
609 <T i18nKey="prev">#</T>
613 class="btn btn-sm btn-secondary"
614 onClick={linkEvent(this, this.nextPage)}
616 <T i18nKey="next">#</T>
623 let viewStr = View[this.state.view].toLowerCase();
624 let sortStr = SortType[this.state.sort].toLowerCase();
625 this.props.history.push(
626 `/u/${this.state.user.name}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`
645 let form: GetUserDetailsForm = {
646 user_id: this.state.user_id,
647 username: this.state.username,
648 sort: SortType[this.state.sort],
649 saved_only: this.state.view == View.Saved,
650 page: this.state.page,
653 WebSocketService.Instance.getUserDetails(form);
656 handleSortChange(val: SortType) {
657 this.state.sort = val;
659 this.setState(this.state);
664 handleViewChange(i: User, event: any) {
665 i.state.view = Number(event.target.value);
672 handleUserSettingsShowNsfwChange(i: User, event: any) {
673 i.state.userSettingsForm.show_nsfw = event.target.checked;
677 handleUserSettingsThemeChange(i: User, event: any) {
678 i.state.userSettingsForm.theme = event.target.value;
679 setTheme(event.target.value);
683 handleUserSettingsSortTypeChange(val: SortType) {
684 this.state.userSettingsForm.default_sort_type = val;
685 this.setState(this.state);
688 handleUserSettingsListingTypeChange(val: ListingType) {
689 this.state.userSettingsForm.default_listing_type = val;
690 this.setState(this.state);
693 handleUserSettingsSubmit(i: User, event: any) {
694 event.preventDefault();
695 i.state.userSettingsLoading = true;
698 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
701 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
702 event.preventDefault();
703 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
707 handleDeleteAccountPasswordChange(i: User, event: any) {
708 i.state.deleteAccountForm.password = event.target.value;
712 handleLogoutClick(i: User) {
713 UserService.Instance.logout();
714 i.context.router.history.push('/');
717 handleDeleteAccount(i: User, event: any) {
718 event.preventDefault();
719 i.state.deleteAccountLoading = true;
722 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
725 parseMessage(msg: any) {
727 let op: UserOperation = msgOp(msg);
729 alert(i18n.t(msg.error));
730 this.state.deleteAccountLoading = false;
731 this.setState(this.state);
733 } else if (op == UserOperation.GetUserDetails) {
734 let res: UserDetailsResponse = msg;
735 this.state.user = res.user;
736 this.state.comments = res.comments;
737 this.state.follows = res.follows;
738 this.state.moderates = res.moderates;
739 this.state.posts = res.posts;
740 this.state.admins = res.admins;
741 this.state.loading = false;
742 if (this.isCurrentUser) {
743 this.state.userSettingsForm.show_nsfw =
744 UserService.Instance.user.show_nsfw;
745 this.state.userSettingsForm.theme = UserService.Instance.user.theme
746 ? UserService.Instance.user.theme
748 this.state.userSettingsForm.default_sort_type =
749 UserService.Instance.user.default_sort_type;
750 this.state.userSettingsForm.default_listing_type =
751 UserService.Instance.user.default_listing_type;
753 document.title = `/u/${this.state.user.name} - ${WebSocketService.Instance.site.name}`;
754 window.scrollTo(0, 0);
755 this.setState(this.state);
756 } else if (op == UserOperation.EditComment) {
757 let res: CommentResponse = msg;
759 let found = this.state.comments.find(c => c.id == res.comment.id);
760 found.content = res.comment.content;
761 found.updated = res.comment.updated;
762 found.removed = res.comment.removed;
763 found.deleted = res.comment.deleted;
764 found.upvotes = res.comment.upvotes;
765 found.downvotes = res.comment.downvotes;
766 found.score = res.comment.score;
768 this.setState(this.state);
769 } else if (op == UserOperation.CreateComment) {
770 // let res: CommentResponse = msg;
771 alert(i18n.t('reply_sent'));
772 // this.state.comments.unshift(res.comment); // TODO do this right
773 // this.setState(this.state);
774 } else if (op == UserOperation.SaveComment) {
775 let res: CommentResponse = msg;
776 let found = this.state.comments.find(c => c.id == res.comment.id);
777 found.saved = res.comment.saved;
778 this.setState(this.state);
779 } else if (op == UserOperation.CreateCommentLike) {
780 let res: CommentResponse = msg;
781 let found: Comment = this.state.comments.find(
782 c => c.id === res.comment.id
784 found.score = res.comment.score;
785 found.upvotes = res.comment.upvotes;
786 found.downvotes = res.comment.downvotes;
787 if (res.comment.my_vote !== null) found.my_vote = res.comment.my_vote;
788 this.setState(this.state);
789 } else if (op == UserOperation.BanUser) {
790 let res: BanUserResponse = msg;
792 .filter(c => c.creator_id == res.user.id)
793 .forEach(c => (c.banned = res.banned));
795 .filter(c => c.creator_id == res.user.id)
796 .forEach(c => (c.banned = res.banned));
797 this.setState(this.state);
798 } else if (op == UserOperation.AddAdmin) {
799 let res: AddAdminResponse = msg;
800 this.state.admins = res.admins;
801 this.setState(this.state);
802 } else if (op == UserOperation.SaveUserSettings) {
803 this.state = this.emptyState;
804 this.state.userSettingsLoading = false;
805 this.setState(this.state);
806 let res: LoginResponse = msg;
807 UserService.Instance.login(res);
808 } else if (op == UserOperation.DeleteAccount) {
809 this.state.deleteAccountLoading = false;
810 this.state.deleteAccountShowConfirm = false;
811 this.setState(this.state);
812 this.context.router.history.push('/');