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}
253 href={`/feeds/u/${this.state.username}.xml?sort=${
254 SortType[this.state.sort]
258 <svg class="icon mx-2 text-muted small">
259 <use xlinkHref="#icon-rss">#</use>
267 let combined: Array<{ type_: string; data: Comment | Post }> = [];
268 let comments = this.state.comments.map(e => {
269 return { type_: 'comments', data: e };
271 let posts = this.state.posts.map(e => {
272 return { type_: 'posts', data: e };
275 combined.push(...comments);
276 combined.push(...posts);
279 if (this.state.sort == SortType.New) {
280 combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
282 combined.sort((a, b) => b.data.score - a.data.score);
289 {i.type_ == 'posts' ? (
291 post={i.data as Post}
292 admins={this.state.admins}
298 nodes={[{ comment: i.data as Comment }]}
299 admins={this.state.admins}
312 {this.state.comments.map(comment => (
314 nodes={[{ comment: comment }]}
315 admins={this.state.admins}
326 {this.state.posts.map(post => (
329 admins={this.state.admins}
339 let user = this.state.user;
342 <div class="card border-secondary mb-3">
343 <div class="card-body">
345 <ul class="list-inline mb-0">
346 <li className="list-inline-item">{user.name}</li>
348 <li className="list-inline-item badge badge-danger">
349 <T i18nKey="banned">#</T>
355 {i18n.t('joined')} <MomentTime data={user} />
357 <div class="table-responsive">
358 <table class="table table-bordered table-sm mt-2 mb-0">
362 i18nKey="number_of_points"
363 interpolation={{ count: user.post_score }}
370 i18nKey="number_of_posts"
371 interpolation={{ count: user.number_of_posts }}
380 i18nKey="number_of_points"
381 interpolation={{ count: user.comment_score }}
388 i18nKey="number_of_comments"
389 interpolation={{ count: user.number_of_comments }}
397 {this.isCurrentUser && (
399 class="btn btn-block btn-secondary mt-3"
400 onClick={linkEvent(this, this.handleLogoutClick)}
402 <T i18nKey="logout">#</T>
414 <div class="card border-secondary mb-3">
415 <div class="card-body">
417 <T i18nKey="settings">#</T>
419 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
420 <div class="form-group">
423 <T i18nKey="theme">#</T>
426 value={this.state.userSettingsForm.theme}
429 this.handleUserSettingsThemeChange
431 class="ml-2 custom-select custom-select-sm w-auto"
434 <T i18nKey="theme">#</T>
436 {themes.map(theme => (
437 <option value={theme}>{theme}</option>
442 <form className="form-group">
445 <T i18nKey="sort_type" class="mr-2">
450 type_={this.state.userSettingsForm.default_listing_type}
451 onChange={this.handleUserSettingsListingTypeChange}
455 <form className="form-group">
458 <T i18nKey="type" class="mr-2">
463 sort={this.state.userSettingsForm.default_sort_type}
464 onChange={this.handleUserSettingsSortTypeChange}
468 <div class="form-group">
470 <div class="form-check">
472 class="form-check-input"
474 checked={this.state.userSettingsForm.show_nsfw}
477 this.handleUserSettingsShowNsfwChange
480 <label class="form-check-label">
481 <T i18nKey="show_nsfw">#</T>
486 <div class="form-group">
490 class="btn btn-block btn-secondary mr-4"
492 {this.state.userSettingsLoading ? (
493 <svg class="icon icon-spinner spin">
494 <use xlinkHref="#icon-spinner"></use>
497 capitalizeFirstLetter(i18n.t('save'))
503 <div class="form-group mb-0">
506 class="btn btn-block btn-danger"
509 this.handleDeleteAccountShowConfirmToggle
512 <T i18nKey="delete_account">#</T>
514 {this.state.deleteAccountShowConfirm && (
516 <div class="my-2 alert alert-danger" role="alert">
517 <T i18nKey="delete_account_confirm">#</T>
521 value={this.state.deleteAccountForm.password}
524 this.handleDeleteAccountPasswordChange
526 class="form-control my-2"
529 class="btn btn-danger mr-4"
530 disabled={!this.state.deleteAccountForm.password}
531 onClick={linkEvent(this, this.handleDeleteAccount)}
533 {this.state.deleteAccountLoading ? (
534 <svg class="icon icon-spinner spin">
535 <use xlinkHref="#icon-spinner"></use>
538 capitalizeFirstLetter(i18n.t('delete'))
542 class="btn btn-secondary"
545 this.handleDeleteAccountShowConfirmToggle
548 <T i18nKey="cancel">#</T>
564 {this.state.moderates.length > 0 && (
565 <div class="card border-secondary mb-3">
566 <div class="card-body">
568 <T i18nKey="moderates">#</T>
570 <ul class="list-unstyled mb-0">
571 {this.state.moderates.map(community => (
573 <Link to={`/c/${community.community_name}`}>
574 {community.community_name}
589 {this.state.follows.length > 0 && (
590 <div class="card border-secondary mb-3">
591 <div class="card-body">
593 <T i18nKey="subscribed">#</T>
595 <ul class="list-unstyled mb-0">
596 {this.state.follows.map(community => (
598 <Link to={`/c/${community.community_name}`}>
599 {community.community_name}
614 {this.state.page > 1 && (
616 class="btn btn-sm btn-secondary mr-1"
617 onClick={linkEvent(this, this.prevPage)}
619 <T i18nKey="prev">#</T>
623 class="btn btn-sm btn-secondary"
624 onClick={linkEvent(this, this.nextPage)}
626 <T i18nKey="next">#</T>
633 let viewStr = View[this.state.view].toLowerCase();
634 let sortStr = SortType[this.state.sort].toLowerCase();
635 this.props.history.push(
636 `/u/${this.state.user.name}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`
655 let form: GetUserDetailsForm = {
656 user_id: this.state.user_id,
657 username: this.state.username,
658 sort: SortType[this.state.sort],
659 saved_only: this.state.view == View.Saved,
660 page: this.state.page,
663 WebSocketService.Instance.getUserDetails(form);
666 handleSortChange(val: SortType) {
667 this.state.sort = val;
669 this.setState(this.state);
674 handleViewChange(i: User, event: any) {
675 i.state.view = Number(event.target.value);
682 handleUserSettingsShowNsfwChange(i: User, event: any) {
683 i.state.userSettingsForm.show_nsfw = event.target.checked;
687 handleUserSettingsThemeChange(i: User, event: any) {
688 i.state.userSettingsForm.theme = event.target.value;
689 setTheme(event.target.value);
693 handleUserSettingsSortTypeChange(val: SortType) {
694 this.state.userSettingsForm.default_sort_type = val;
695 this.setState(this.state);
698 handleUserSettingsListingTypeChange(val: ListingType) {
699 this.state.userSettingsForm.default_listing_type = val;
700 this.setState(this.state);
703 handleUserSettingsSubmit(i: User, event: any) {
704 event.preventDefault();
705 i.state.userSettingsLoading = true;
708 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
711 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
712 event.preventDefault();
713 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
717 handleDeleteAccountPasswordChange(i: User, event: any) {
718 i.state.deleteAccountForm.password = event.target.value;
722 handleLogoutClick(i: User) {
723 UserService.Instance.logout();
724 i.context.router.history.push('/');
727 handleDeleteAccount(i: User, event: any) {
728 event.preventDefault();
729 i.state.deleteAccountLoading = true;
732 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
735 parseMessage(msg: any) {
737 let op: UserOperation = msgOp(msg);
739 alert(i18n.t(msg.error));
740 this.state.deleteAccountLoading = false;
741 this.setState(this.state);
743 } else if (op == UserOperation.GetUserDetails) {
744 let res: UserDetailsResponse = msg;
745 this.state.user = res.user;
746 this.state.comments = res.comments;
747 this.state.follows = res.follows;
748 this.state.moderates = res.moderates;
749 this.state.posts = res.posts;
750 this.state.admins = res.admins;
751 this.state.loading = false;
752 if (this.isCurrentUser) {
753 this.state.userSettingsForm.show_nsfw =
754 UserService.Instance.user.show_nsfw;
755 this.state.userSettingsForm.theme = UserService.Instance.user.theme
756 ? UserService.Instance.user.theme
758 this.state.userSettingsForm.default_sort_type =
759 UserService.Instance.user.default_sort_type;
760 this.state.userSettingsForm.default_listing_type =
761 UserService.Instance.user.default_listing_type;
763 document.title = `/u/${this.state.user.name} - ${WebSocketService.Instance.site.name}`;
764 window.scrollTo(0, 0);
765 this.setState(this.state);
766 } else if (op == UserOperation.EditComment) {
767 let res: CommentResponse = msg;
769 let found = this.state.comments.find(c => c.id == res.comment.id);
770 found.content = res.comment.content;
771 found.updated = res.comment.updated;
772 found.removed = res.comment.removed;
773 found.deleted = res.comment.deleted;
774 found.upvotes = res.comment.upvotes;
775 found.downvotes = res.comment.downvotes;
776 found.score = res.comment.score;
778 this.setState(this.state);
779 } else if (op == UserOperation.CreateComment) {
780 // let res: CommentResponse = msg;
781 alert(i18n.t('reply_sent'));
782 // this.state.comments.unshift(res.comment); // TODO do this right
783 // this.setState(this.state);
784 } else if (op == UserOperation.SaveComment) {
785 let res: CommentResponse = msg;
786 let found = this.state.comments.find(c => c.id == res.comment.id);
787 found.saved = res.comment.saved;
788 this.setState(this.state);
789 } else if (op == UserOperation.CreateCommentLike) {
790 let res: CommentResponse = msg;
791 let found: Comment = this.state.comments.find(
792 c => c.id === res.comment.id
794 found.score = res.comment.score;
795 found.upvotes = res.comment.upvotes;
796 found.downvotes = res.comment.downvotes;
797 if (res.comment.my_vote !== null) found.my_vote = res.comment.my_vote;
798 this.setState(this.state);
799 } else if (op == UserOperation.BanUser) {
800 let res: BanUserResponse = msg;
802 .filter(c => c.creator_id == res.user.id)
803 .forEach(c => (c.banned = res.banned));
805 .filter(c => c.creator_id == res.user.id)
806 .forEach(c => (c.banned = res.banned));
807 this.setState(this.state);
808 } else if (op == UserOperation.AddAdmin) {
809 let res: AddAdminResponse = msg;
810 this.state.admins = res.admins;
811 this.setState(this.state);
812 } else if (op == UserOperation.SaveUserSettings) {
813 this.state = this.emptyState;
814 this.state.userSettingsLoading = false;
815 this.setState(this.state);
816 let res: LoginResponse = msg;
817 UserService.Instance.login(res);
818 } else if (op == UserOperation.DeleteAccount) {
819 this.state.deleteAccountLoading = false;
820 this.state.deleteAccountShowConfirm = false;
821 this.setState(this.state);
822 this.context.router.history.push('/');