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 <div class="form-group">
501 <div class="form-check">
503 class="form-check-input"
505 checked={this.state.userSettingsForm.show_nsfw}
508 this.handleUserSettingsShowNsfwChange
511 <label class="form-check-label">
512 <T i18nKey="show_nsfw">#</T>
517 <div class="form-group">
521 class="btn btn-block btn-secondary mr-4"
523 {this.state.userSettingsLoading ? (
524 <svg class="icon icon-spinner spin">
525 <use xlinkHref="#icon-spinner"></use>
528 capitalizeFirstLetter(i18n.t('save'))
534 <div class="form-group mb-0">
537 class="btn btn-block btn-danger"
540 this.handleDeleteAccountShowConfirmToggle
543 <T i18nKey="delete_account">#</T>
545 {this.state.deleteAccountShowConfirm && (
547 <div class="my-2 alert alert-danger" role="alert">
548 <T i18nKey="delete_account_confirm">#</T>
552 value={this.state.deleteAccountForm.password}
555 this.handleDeleteAccountPasswordChange
557 class="form-control my-2"
560 class="btn btn-danger mr-4"
561 disabled={!this.state.deleteAccountForm.password}
562 onClick={linkEvent(this, this.handleDeleteAccount)}
564 {this.state.deleteAccountLoading ? (
565 <svg class="icon icon-spinner spin">
566 <use xlinkHref="#icon-spinner"></use>
569 capitalizeFirstLetter(i18n.t('delete'))
573 class="btn btn-secondary"
576 this.handleDeleteAccountShowConfirmToggle
579 <T i18nKey="cancel">#</T>
595 {this.state.moderates.length > 0 && (
596 <div class="card border-secondary mb-3">
597 <div class="card-body">
599 <T i18nKey="moderates">#</T>
601 <ul class="list-unstyled mb-0">
602 {this.state.moderates.map(community => (
604 <Link to={`/c/${community.community_name}`}>
605 {community.community_name}
620 {this.state.follows.length > 0 && (
621 <div class="card border-secondary mb-3">
622 <div class="card-body">
624 <T i18nKey="subscribed">#</T>
626 <ul class="list-unstyled mb-0">
627 {this.state.follows.map(community => (
629 <Link to={`/c/${community.community_name}`}>
630 {community.community_name}
645 {this.state.page > 1 && (
647 class="btn btn-sm btn-secondary mr-1"
648 onClick={linkEvent(this, this.prevPage)}
650 <T i18nKey="prev">#</T>
654 class="btn btn-sm btn-secondary"
655 onClick={linkEvent(this, this.nextPage)}
657 <T i18nKey="next">#</T>
664 let viewStr = View[this.state.view].toLowerCase();
665 let sortStr = SortType[this.state.sort].toLowerCase();
666 this.props.history.push(
667 `/u/${this.state.user.name}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`
686 let form: GetUserDetailsForm = {
687 user_id: this.state.user_id,
688 username: this.state.username,
689 sort: SortType[this.state.sort],
690 saved_only: this.state.view == View.Saved,
691 page: this.state.page,
694 WebSocketService.Instance.getUserDetails(form);
697 handleSortChange(val: SortType) {
698 this.state.sort = val;
700 this.setState(this.state);
705 handleViewChange(i: User, event: any) {
706 i.state.view = Number(event.target.value);
713 handleUserSettingsShowNsfwChange(i: User, event: any) {
714 i.state.userSettingsForm.show_nsfw = event.target.checked;
718 handleUserSettingsThemeChange(i: User, event: any) {
719 i.state.userSettingsForm.theme = event.target.value;
720 setTheme(event.target.value);
724 handleUserSettingsLangChange(i: User, event: any) {
725 i.state.userSettingsForm.lang = event.target.value;
726 i18n.changeLanguage(i.state.userSettingsForm.lang);
730 handleUserSettingsSortTypeChange(val: SortType) {
731 this.state.userSettingsForm.default_sort_type = val;
732 this.setState(this.state);
735 handleUserSettingsListingTypeChange(val: ListingType) {
736 this.state.userSettingsForm.default_listing_type = val;
737 this.setState(this.state);
740 handleUserSettingsSubmit(i: User, event: any) {
741 event.preventDefault();
742 i.state.userSettingsLoading = true;
745 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
748 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
749 event.preventDefault();
750 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
754 handleDeleteAccountPasswordChange(i: User, event: any) {
755 i.state.deleteAccountForm.password = event.target.value;
759 handleLogoutClick(i: User) {
760 UserService.Instance.logout();
761 i.context.router.history.push('/');
764 handleDeleteAccount(i: User, event: any) {
765 event.preventDefault();
766 i.state.deleteAccountLoading = true;
769 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
772 parseMessage(msg: any) {
774 let op: UserOperation = msgOp(msg);
776 alert(i18n.t(msg.error));
777 this.state.deleteAccountLoading = false;
778 this.setState(this.state);
780 } else if (op == UserOperation.GetUserDetails) {
781 let res: UserDetailsResponse = msg;
782 this.state.user = res.user;
783 this.state.comments = res.comments;
784 this.state.follows = res.follows;
785 this.state.moderates = res.moderates;
786 this.state.posts = res.posts;
787 this.state.admins = res.admins;
788 this.state.loading = false;
789 if (this.isCurrentUser) {
790 this.state.userSettingsForm.show_nsfw =
791 UserService.Instance.user.show_nsfw;
792 this.state.userSettingsForm.theme = UserService.Instance.user.theme
793 ? UserService.Instance.user.theme
795 this.state.userSettingsForm.default_sort_type =
796 UserService.Instance.user.default_sort_type;
797 this.state.userSettingsForm.default_listing_type =
798 UserService.Instance.user.default_listing_type;
799 this.state.userSettingsForm.lang = UserService.Instance.user.lang;
801 document.title = `/u/${this.state.user.name} - ${WebSocketService.Instance.site.name}`;
802 window.scrollTo(0, 0);
803 this.setState(this.state);
804 } else if (op == UserOperation.EditComment) {
805 let res: CommentResponse = msg;
807 let found = this.state.comments.find(c => c.id == res.comment.id);
808 found.content = res.comment.content;
809 found.updated = res.comment.updated;
810 found.removed = res.comment.removed;
811 found.deleted = res.comment.deleted;
812 found.upvotes = res.comment.upvotes;
813 found.downvotes = res.comment.downvotes;
814 found.score = res.comment.score;
816 this.setState(this.state);
817 } else if (op == UserOperation.CreateComment) {
818 // let res: CommentResponse = msg;
819 alert(i18n.t('reply_sent'));
820 // this.state.comments.unshift(res.comment); // TODO do this right
821 // this.setState(this.state);
822 } else if (op == UserOperation.SaveComment) {
823 let res: CommentResponse = msg;
824 let found = this.state.comments.find(c => c.id == res.comment.id);
825 found.saved = res.comment.saved;
826 this.setState(this.state);
827 } else if (op == UserOperation.CreateCommentLike) {
828 let res: CommentResponse = msg;
829 let found: Comment = this.state.comments.find(
830 c => c.id === res.comment.id
832 found.score = res.comment.score;
833 found.upvotes = res.comment.upvotes;
834 found.downvotes = res.comment.downvotes;
835 if (res.comment.my_vote !== null) found.my_vote = res.comment.my_vote;
836 this.setState(this.state);
837 } else if (op == UserOperation.BanUser) {
838 let res: BanUserResponse = msg;
840 .filter(c => c.creator_id == res.user.id)
841 .forEach(c => (c.banned = res.banned));
843 .filter(c => c.creator_id == res.user.id)
844 .forEach(c => (c.banned = res.banned));
845 this.setState(this.state);
846 } else if (op == UserOperation.AddAdmin) {
847 let res: AddAdminResponse = msg;
848 this.state.admins = res.admins;
849 this.setState(this.state);
850 } else if (op == UserOperation.SaveUserSettings) {
851 this.state = this.emptyState;
852 this.state.userSettingsLoading = false;
853 this.setState(this.state);
854 let res: LoginResponse = msg;
855 UserService.Instance.login(res);
856 } else if (op == UserOperation.DeleteAccount) {
857 this.state.deleteAccountLoading = false;
858 this.state.deleteAccountShowConfirm = false;
859 this.setState(this.state);
860 this.context.router.history.push('/');