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,
32 import { PostListing } from './post-listing';
33 import { SortSelect } from './sort-select';
34 import { ListingTypeSelect } from './listing-type-select';
35 import { CommentNodes } from './comment-nodes';
36 import { MomentTime } from './moment-time';
37 import { i18n } from '../i18next';
38 import { T } from 'inferno-i18next';
51 follows: Array<CommunityUser>;
52 moderates: Array<CommunityUser>;
53 comments: Array<Comment>;
56 admins: Array<UserView>;
61 userSettingsForm: UserSettingsForm;
62 userSettingsLoading: boolean;
63 deleteAccountLoading: boolean;
64 deleteAccountShowConfirm: boolean;
65 deleteAccountForm: DeleteAccountForm;
68 export class User extends Component<any, UserState> {
69 private subscription: Subscription;
70 private emptyState: UserState = {
76 number_of_posts: null,
78 number_of_comments: null,
90 view: this.getViewFromProps(this.props),
91 sort: this.getSortTypeFromProps(this.props),
92 page: this.getPageFromProps(this.props),
96 default_sort_type: null,
97 default_listing_type: null,
101 userSettingsLoading: null,
102 deleteAccountLoading: null,
103 deleteAccountShowConfirm: false,
109 constructor(props: any, context: any) {
110 super(props, context);
112 this.state = this.emptyState;
113 this.handleSortChange = this.handleSortChange.bind(this);
114 this.handleUserSettingsSortTypeChange = this.handleUserSettingsSortTypeChange.bind(
117 this.handleUserSettingsListingTypeChange = this.handleUserSettingsListingTypeChange.bind(
121 this.state.user_id = Number(this.props.match.params.id);
122 this.state.username = this.props.match.params.username;
124 this.subscription = WebSocketService.Instance.subject
134 msg => this.parseMessage(msg),
135 err => console.error(err),
136 () => console.log('complete')
142 get isCurrentUser() {
144 UserService.Instance.user &&
145 UserService.Instance.user.id == this.state.user.id
149 getViewFromProps(props: any): View {
150 return props.match.params.view
151 ? View[capitalizeFirstLetter(props.match.params.view)]
155 getSortTypeFromProps(props: any): SortType {
156 return props.match.params.sort
157 ? routeSortTypeToEnum(props.match.params.sort)
161 getPageFromProps(props: any): number {
162 return props.match.params.page ? Number(props.match.params.page) : 1;
165 componentWillUnmount() {
166 this.subscription.unsubscribe();
169 // Necessary for back button for some reason
170 componentWillReceiveProps(nextProps: any) {
172 nextProps.history.action == 'POP' ||
173 nextProps.history.action == 'PUSH'
175 this.state.view = this.getViewFromProps(nextProps);
176 this.state.sort = this.getSortTypeFromProps(nextProps);
177 this.state.page = this.getPageFromProps(nextProps);
178 this.setState(this.state);
183 componentDidUpdate(lastProps: any, _lastState: UserState, _snapshot: any) {
184 // Necessary if you are on a post and you click another post (same route)
186 lastProps.location.pathname.split('/')[2] !==
187 lastProps.history.location.pathname.split('/')[2]
189 // Couldnt get a refresh working. This does for now.
196 <div class="container">
197 {this.state.loading ? (
199 <svg class="icon icon-spinner spin">
200 <use xlinkHref="#icon-spinner"></use>
205 <div class="col-12 col-md-8">
206 <h5>/u/{this.state.user.name}</h5>
208 {this.state.view == View.Overview && this.overview()}
209 {this.state.view == View.Comments && this.comments()}
210 {this.state.view == View.Posts && this.posts()}
211 {this.state.view == View.Saved && this.overview()}
214 <div class="col-12 col-md-4">
216 {this.isCurrentUser && this.userSettings()}
228 <div className="mb-2">
230 value={this.state.view}
231 onChange={linkEvent(this, this.handleViewChange)}
232 class="custom-select custom-select-sm w-auto"
235 <T i18nKey="view">#</T>
237 <option value={View.Overview}>
238 <T i18nKey="overview">#</T>
240 <option value={View.Comments}>
241 <T i18nKey="comments">#</T>
243 <option value={View.Posts}>
244 <T i18nKey="posts">#</T>
246 <option value={View.Saved}>
247 <T i18nKey="saved">#</T>
252 sort={this.state.sort}
253 onChange={this.handleSortChange}
258 href={`/feeds/u/${this.state.username}.xml?sort=${
259 SortType[this.state.sort]
263 <svg class="icon mx-2 text-muted small">
264 <use xlinkHref="#icon-rss">#</use>
272 let combined: Array<{ type_: string; data: Comment | Post }> = [];
273 let comments = this.state.comments.map(e => {
274 return { type_: 'comments', data: e };
276 let posts = this.state.posts.map(e => {
277 return { type_: 'posts', data: e };
280 combined.push(...comments);
281 combined.push(...posts);
284 if (this.state.sort == SortType.New) {
285 combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
287 combined.sort((a, b) => b.data.score - a.data.score);
294 {i.type_ == 'posts' ? (
296 post={i.data as Post}
297 admins={this.state.admins}
303 nodes={[{ comment: i.data as Comment }]}
304 admins={this.state.admins}
317 {this.state.comments.map(comment => (
319 nodes={[{ comment: comment }]}
320 admins={this.state.admins}
331 {this.state.posts.map(post => (
334 admins={this.state.admins}
344 let user = this.state.user;
347 <div class="card border-secondary mb-3">
348 <div class="card-body">
350 <ul class="list-inline mb-0">
351 <li className="list-inline-item">{user.name}</li>
353 <li className="list-inline-item badge badge-danger">
354 <T i18nKey="banned">#</T>
360 {i18n.t('joined')} <MomentTime data={user} />
362 <div class="table-responsive">
363 <table class="table table-bordered table-sm mt-2 mb-0">
367 i18nKey="number_of_points"
368 interpolation={{ count: user.post_score }}
375 i18nKey="number_of_posts"
376 interpolation={{ count: user.number_of_posts }}
385 i18nKey="number_of_points"
386 interpolation={{ count: user.comment_score }}
393 i18nKey="number_of_comments"
394 interpolation={{ count: user.number_of_comments }}
402 {this.isCurrentUser && (
404 class="btn btn-block btn-secondary mt-3"
405 onClick={linkEvent(this, this.handleLogoutClick)}
407 <T i18nKey="logout">#</T>
419 <div class="card border-secondary mb-3">
420 <div class="card-body">
422 <T i18nKey="settings">#</T>
424 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
425 <div class="form-group">
428 <T i18nKey="language">#</T>
431 value={this.state.userSettingsForm.lang}
434 this.handleUserSettingsLangChange
436 class="ml-2 custom-select custom-select-sm w-auto"
439 <T i18nKey="language">#</T>
441 <option value="browser">
442 <T i18nKey="browser_default">#</T>
444 <option disabled>──</option>
445 {languages.map(lang => (
446 <option value={lang.code}>{lang.name}</option>
451 <div class="form-group">
454 <T i18nKey="theme">#</T>
457 value={this.state.userSettingsForm.theme}
460 this.handleUserSettingsThemeChange
462 class="ml-2 custom-select custom-select-sm w-auto"
465 <T i18nKey="theme">#</T>
467 {themes.map(theme => (
468 <option value={theme}>{theme}</option>
473 <form className="form-group">
476 <T i18nKey="sort_type" class="mr-2">
481 type_={this.state.userSettingsForm.default_listing_type}
482 onChange={this.handleUserSettingsListingTypeChange}
486 <form className="form-group">
489 <T i18nKey="type" class="mr-2">
494 sort={this.state.userSettingsForm.default_sort_type}
495 onChange={this.handleUserSettingsSortTypeChange}
499 {WebSocketService.Instance.site.enable_nsfw && (
500 <div class="form-group">
502 <div class="form-check">
504 class="form-check-input"
506 checked={this.state.userSettingsForm.show_nsfw}
509 this.handleUserSettingsShowNsfwChange
512 <label class="form-check-label">
513 <T i18nKey="show_nsfw">#</T>
519 <div class="form-group">
523 class="btn btn-block btn-secondary mr-4"
525 {this.state.userSettingsLoading ? (
526 <svg class="icon icon-spinner spin">
527 <use xlinkHref="#icon-spinner"></use>
530 capitalizeFirstLetter(i18n.t('save'))
536 <div class="form-group mb-0">
539 class="btn btn-block btn-danger"
542 this.handleDeleteAccountShowConfirmToggle
545 <T i18nKey="delete_account">#</T>
547 {this.state.deleteAccountShowConfirm && (
549 <div class="my-2 alert alert-danger" role="alert">
550 <T i18nKey="delete_account_confirm">#</T>
554 value={this.state.deleteAccountForm.password}
557 this.handleDeleteAccountPasswordChange
559 class="form-control my-2"
562 class="btn btn-danger mr-4"
563 disabled={!this.state.deleteAccountForm.password}
564 onClick={linkEvent(this, this.handleDeleteAccount)}
566 {this.state.deleteAccountLoading ? (
567 <svg class="icon icon-spinner spin">
568 <use xlinkHref="#icon-spinner"></use>
571 capitalizeFirstLetter(i18n.t('delete'))
575 class="btn btn-secondary"
578 this.handleDeleteAccountShowConfirmToggle
581 <T i18nKey="cancel">#</T>
597 {this.state.moderates.length > 0 && (
598 <div class="card border-secondary mb-3">
599 <div class="card-body">
601 <T i18nKey="moderates">#</T>
603 <ul class="list-unstyled mb-0">
604 {this.state.moderates.map(community => (
606 <Link to={`/c/${community.community_name}`}>
607 {community.community_name}
622 {this.state.follows.length > 0 && (
623 <div class="card border-secondary mb-3">
624 <div class="card-body">
626 <T i18nKey="subscribed">#</T>
628 <ul class="list-unstyled mb-0">
629 {this.state.follows.map(community => (
631 <Link to={`/c/${community.community_name}`}>
632 {community.community_name}
647 {this.state.page > 1 && (
649 class="btn btn-sm btn-secondary mr-1"
650 onClick={linkEvent(this, this.prevPage)}
652 <T i18nKey="prev">#</T>
656 class="btn btn-sm btn-secondary"
657 onClick={linkEvent(this, this.nextPage)}
659 <T i18nKey="next">#</T>
666 let viewStr = View[this.state.view].toLowerCase();
667 let sortStr = SortType[this.state.sort].toLowerCase();
668 this.props.history.push(
669 `/u/${this.state.user.name}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`
688 let form: GetUserDetailsForm = {
689 user_id: this.state.user_id,
690 username: this.state.username,
691 sort: SortType[this.state.sort],
692 saved_only: this.state.view == View.Saved,
693 page: this.state.page,
696 WebSocketService.Instance.getUserDetails(form);
699 handleSortChange(val: SortType) {
700 this.state.sort = val;
702 this.setState(this.state);
707 handleViewChange(i: User, event: any) {
708 i.state.view = Number(event.target.value);
715 handleUserSettingsShowNsfwChange(i: User, event: any) {
716 i.state.userSettingsForm.show_nsfw = event.target.checked;
720 handleUserSettingsThemeChange(i: User, event: any) {
721 i.state.userSettingsForm.theme = event.target.value;
722 setTheme(event.target.value);
726 handleUserSettingsLangChange(i: User, event: any) {
727 i.state.userSettingsForm.lang = event.target.value;
728 i18n.changeLanguage(i.state.userSettingsForm.lang);
732 handleUserSettingsSortTypeChange(val: SortType) {
733 this.state.userSettingsForm.default_sort_type = val;
734 this.setState(this.state);
737 handleUserSettingsListingTypeChange(val: ListingType) {
738 this.state.userSettingsForm.default_listing_type = val;
739 this.setState(this.state);
742 handleUserSettingsSubmit(i: User, event: any) {
743 event.preventDefault();
744 i.state.userSettingsLoading = true;
747 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
750 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
751 event.preventDefault();
752 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
756 handleDeleteAccountPasswordChange(i: User, event: any) {
757 i.state.deleteAccountForm.password = event.target.value;
761 handleLogoutClick(i: User) {
762 UserService.Instance.logout();
763 i.context.router.history.push('/');
766 handleDeleteAccount(i: User, event: any) {
767 event.preventDefault();
768 i.state.deleteAccountLoading = true;
771 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
774 parseMessage(msg: any) {
776 let op: UserOperation = msgOp(msg);
778 alert(i18n.t(msg.error));
779 this.state.deleteAccountLoading = false;
780 this.setState(this.state);
782 } else if (op == UserOperation.GetUserDetails) {
783 let res: UserDetailsResponse = msg;
784 this.state.user = res.user;
785 this.state.comments = res.comments;
786 this.state.follows = res.follows;
787 this.state.moderates = res.moderates;
788 this.state.posts = res.posts;
789 this.state.admins = res.admins;
790 this.state.loading = false;
791 if (this.isCurrentUser) {
792 this.state.userSettingsForm.show_nsfw =
793 UserService.Instance.user.show_nsfw;
794 this.state.userSettingsForm.theme = UserService.Instance.user.theme
795 ? UserService.Instance.user.theme
797 this.state.userSettingsForm.default_sort_type =
798 UserService.Instance.user.default_sort_type;
799 this.state.userSettingsForm.default_listing_type =
800 UserService.Instance.user.default_listing_type;
801 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
803 document.title = `/u/${this.state.user.name} - ${WebSocketService.Instance.site.name}`;
804 window.scrollTo(0, 0);
805 this.setState(this.state);
806 } else if (op == UserOperation.EditComment) {
807 let res: CommentResponse = msg;
809 let found = this.state.comments.find(c => c.id == res.comment.id);
810 found.content = res.comment.content;
811 found.updated = res.comment.updated;
812 found.removed = res.comment.removed;
813 found.deleted = res.comment.deleted;
814 found.upvotes = res.comment.upvotes;
815 found.downvotes = res.comment.downvotes;
816 found.score = res.comment.score;
818 this.setState(this.state);
819 } else if (op == UserOperation.CreateComment) {
820 // let res: CommentResponse = msg;
821 alert(i18n.t('reply_sent'));
822 // this.state.comments.unshift(res.comment); // TODO do this right
823 // this.setState(this.state);
824 } else if (op == UserOperation.SaveComment) {
825 let res: CommentResponse = msg;
826 let found = this.state.comments.find(c => c.id == res.comment.id);
827 found.saved = res.comment.saved;
828 this.setState(this.state);
829 } else if (op == UserOperation.CreateCommentLike) {
830 let res: CommentResponse = msg;
831 let found: Comment = this.state.comments.find(
832 c => c.id === res.comment.id
834 found.score = res.comment.score;
835 found.upvotes = res.comment.upvotes;
836 found.downvotes = res.comment.downvotes;
837 if (res.comment.my_vote !== null) found.my_vote = res.comment.my_vote;
838 this.setState(this.state);
839 } else if (op == UserOperation.BanUser) {
840 let res: BanUserResponse = msg;
842 .filter(c => c.creator_id == res.user.id)
843 .forEach(c => (c.banned = res.banned));
845 .filter(c => c.creator_id == res.user.id)
846 .forEach(c => (c.banned = res.banned));
847 this.setState(this.state);
848 } else if (op == UserOperation.AddAdmin) {
849 let res: AddAdminResponse = msg;
850 this.state.admins = res.admins;
851 this.setState(this.state);
852 } else if (op == UserOperation.SaveUserSettings) {
853 this.state = this.emptyState;
854 this.state.userSettingsLoading = false;
855 this.setState(this.state);
856 let res: LoginResponse = msg;
857 UserService.Instance.login(res);
858 } else if (op == UserOperation.DeleteAccount) {
859 this.state.deleteAccountLoading = false;
860 this.state.deleteAccountShowConfirm = false;
861 this.setState(this.state);
862 this.context.router.history.push('/');