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 }}
388 <div class="card border-secondary mb-3">
389 <div class="card-body">
391 <T i18nKey="settings">#</T>
393 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
394 <div class="form-group">
397 <T i18nKey="theme">#</T>
400 value={this.state.userSettingsForm.theme}
403 this.handleUserSettingsThemeChange
405 class="ml-2 custom-select custom-select-sm w-auto"
408 <T i18nKey="theme">#</T>
410 {themes.map(theme => (
411 <option value={theme}>{theme}</option>
416 <form className="form-group">
419 <T i18nKey="sort_type" class="mr-2">
424 type_={this.state.userSettingsForm.default_listing_type}
425 onChange={this.handleUserSettingsListingTypeChange}
429 <form className="form-group">
432 <T i18nKey="type" class="mr-2">
437 sort={this.state.userSettingsForm.default_sort_type}
438 onChange={this.handleUserSettingsSortTypeChange}
442 <div class="form-group">
444 <div class="form-check">
446 class="form-check-input"
448 checked={this.state.userSettingsForm.show_nsfw}
451 this.handleUserSettingsShowNsfwChange
454 <label class="form-check-label">
455 <T i18nKey="show_nsfw">#</T>
460 <div class="form-group">
464 class="btn btn-block btn-secondary mr-4"
466 {this.state.userSettingsLoading ? (
467 <svg class="icon icon-spinner spin">
468 <use xlinkHref="#icon-spinner"></use>
471 capitalizeFirstLetter(i18n.t('save'))
477 <div class="form-group mb-0">
480 class="btn btn-block btn-danger"
483 this.handleDeleteAccountShowConfirmToggle
486 <T i18nKey="delete_account">#</T>
488 {this.state.deleteAccountShowConfirm && (
490 <div class="my-2 alert alert-danger" role="alert">
491 <T i18nKey="delete_account_confirm">#</T>
495 value={this.state.deleteAccountForm.password}
498 this.handleDeleteAccountPasswordChange
500 class="form-control my-2"
503 class="btn btn-danger mr-4"
504 disabled={!this.state.deleteAccountForm.password}
505 onClick={linkEvent(this, this.handleDeleteAccount)}
507 {this.state.deleteAccountLoading ? (
508 <svg class="icon icon-spinner spin">
509 <use xlinkHref="#icon-spinner"></use>
512 capitalizeFirstLetter(i18n.t('delete'))
516 class="btn btn-secondary"
519 this.handleDeleteAccountShowConfirmToggle
522 <T i18nKey="cancel">#</T>
538 {this.state.moderates.length > 0 && (
539 <div class="card border-secondary mb-3">
540 <div class="card-body">
542 <T i18nKey="moderates">#</T>
544 <ul class="list-unstyled mb-0">
545 {this.state.moderates.map(community => (
547 <Link to={`/c/${community.community_name}`}>
548 {community.community_name}
563 {this.state.follows.length > 0 && (
564 <div class="card border-secondary mb-3">
565 <div class="card-body">
567 <T i18nKey="subscribed">#</T>
569 <ul class="list-unstyled mb-0">
570 {this.state.follows.map(community => (
572 <Link to={`/c/${community.community_name}`}>
573 {community.community_name}
588 {this.state.page > 1 && (
590 class="btn btn-sm btn-secondary mr-1"
591 onClick={linkEvent(this, this.prevPage)}
593 <T i18nKey="prev">#</T>
597 class="btn btn-sm btn-secondary"
598 onClick={linkEvent(this, this.nextPage)}
600 <T i18nKey="next">#</T>
607 let viewStr = View[this.state.view].toLowerCase();
608 let sortStr = SortType[this.state.sort].toLowerCase();
609 this.props.history.push(
610 `/u/${this.state.user.name}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`
629 let form: GetUserDetailsForm = {
630 user_id: this.state.user_id,
631 username: this.state.username,
632 sort: SortType[this.state.sort],
633 saved_only: this.state.view == View.Saved,
634 page: this.state.page,
637 WebSocketService.Instance.getUserDetails(form);
640 handleSortChange(val: SortType) {
641 this.state.sort = val;
643 this.setState(this.state);
648 handleViewChange(i: User, event: any) {
649 i.state.view = Number(event.target.value);
656 handleUserSettingsShowNsfwChange(i: User, event: any) {
657 i.state.userSettingsForm.show_nsfw = event.target.checked;
661 handleUserSettingsThemeChange(i: User, event: any) {
662 i.state.userSettingsForm.theme = event.target.value;
663 setTheme(event.target.value);
667 handleUserSettingsSortTypeChange(val: SortType) {
668 this.state.userSettingsForm.default_sort_type = val;
669 this.setState(this.state);
672 handleUserSettingsListingTypeChange(val: ListingType) {
673 this.state.userSettingsForm.default_listing_type = val;
674 this.setState(this.state);
677 handleUserSettingsSubmit(i: User, event: any) {
678 event.preventDefault();
679 i.state.userSettingsLoading = true;
682 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
685 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
686 event.preventDefault();
687 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
691 handleDeleteAccountPasswordChange(i: User, event: any) {
692 i.state.deleteAccountForm.password = event.target.value;
696 handleDeleteAccount(i: User, event: any) {
697 event.preventDefault();
698 i.state.deleteAccountLoading = true;
701 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
704 parseMessage(msg: any) {
706 let op: UserOperation = msgOp(msg);
708 alert(i18n.t(msg.error));
709 this.state.deleteAccountLoading = false;
710 this.setState(this.state);
712 } else if (op == UserOperation.GetUserDetails) {
713 let res: UserDetailsResponse = msg;
714 this.state.user = res.user;
715 this.state.comments = res.comments;
716 this.state.follows = res.follows;
717 this.state.moderates = res.moderates;
718 this.state.posts = res.posts;
719 this.state.admins = res.admins;
720 this.state.loading = false;
721 if (this.isCurrentUser) {
722 this.state.userSettingsForm.show_nsfw =
723 UserService.Instance.user.show_nsfw;
724 this.state.userSettingsForm.theme = UserService.Instance.user.theme
725 ? UserService.Instance.user.theme
727 this.state.userSettingsForm.default_sort_type =
728 UserService.Instance.user.default_sort_type;
729 this.state.userSettingsForm.default_listing_type =
730 UserService.Instance.user.default_listing_type;
732 document.title = `/u/${this.state.user.name} - ${WebSocketService.Instance.site.name}`;
733 window.scrollTo(0, 0);
734 this.setState(this.state);
735 } else if (op == UserOperation.EditComment) {
736 let res: CommentResponse = msg;
738 let found = this.state.comments.find(c => c.id == res.comment.id);
739 found.content = res.comment.content;
740 found.updated = res.comment.updated;
741 found.removed = res.comment.removed;
742 found.deleted = res.comment.deleted;
743 found.upvotes = res.comment.upvotes;
744 found.downvotes = res.comment.downvotes;
745 found.score = res.comment.score;
747 this.setState(this.state);
748 } else if (op == UserOperation.CreateComment) {
749 // let res: CommentResponse = msg;
750 alert(i18n.t('reply_sent'));
751 // this.state.comments.unshift(res.comment); // TODO do this right
752 // this.setState(this.state);
753 } else if (op == UserOperation.SaveComment) {
754 let res: CommentResponse = msg;
755 let found = this.state.comments.find(c => c.id == res.comment.id);
756 found.saved = res.comment.saved;
757 this.setState(this.state);
758 } else if (op == UserOperation.CreateCommentLike) {
759 let res: CommentResponse = msg;
760 let found: Comment = this.state.comments.find(
761 c => c.id === res.comment.id
763 found.score = res.comment.score;
764 found.upvotes = res.comment.upvotes;
765 found.downvotes = res.comment.downvotes;
766 if (res.comment.my_vote !== null) found.my_vote = res.comment.my_vote;
767 this.setState(this.state);
768 } else if (op == UserOperation.BanUser) {
769 let res: BanUserResponse = msg;
771 .filter(c => c.creator_id == res.user.id)
772 .forEach(c => (c.banned = res.banned));
774 .filter(c => c.creator_id == res.user.id)
775 .forEach(c => (c.banned = res.banned));
776 this.setState(this.state);
777 } else if (op == UserOperation.AddAdmin) {
778 let res: AddAdminResponse = msg;
779 this.state.admins = res.admins;
780 this.setState(this.state);
781 } else if (op == UserOperation.SaveUserSettings) {
782 this.state = this.emptyState;
783 this.state.userSettingsLoading = false;
784 this.setState(this.state);
785 let res: LoginResponse = msg;
786 UserService.Instance.login(res);
787 } else if (op == UserOperation.DeleteAccount) {
788 this.state.deleteAccountLoading = false;
789 this.state.deleteAccountShowConfirm = false;
790 this.setState(this.state);
791 this.context.router.history.push('/');