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={`${document.location.origin}/feeds/u/${this.state.username}.xml`}
255 <svg class="icon mx-2 text-muted small">
256 <use xlinkHref="#icon-rss">#</use>
264 let combined: Array<{ type_: string; data: Comment | Post }> = [];
265 let comments = this.state.comments.map(e => {
266 return { type_: 'comments', data: e };
268 let posts = this.state.posts.map(e => {
269 return { type_: 'posts', data: e };
272 combined.push(...comments);
273 combined.push(...posts);
276 if (this.state.sort == SortType.New) {
277 combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
279 combined.sort((a, b) => b.data.score - a.data.score);
286 {i.type_ == 'posts' ? (
288 post={i.data as Post}
289 admins={this.state.admins}
295 nodes={[{ comment: i.data as Comment }]}
296 admins={this.state.admins}
309 {this.state.comments.map(comment => (
311 nodes={[{ comment: comment }]}
312 admins={this.state.admins}
323 {this.state.posts.map(post => (
326 admins={this.state.admins}
336 let user = this.state.user;
339 <div class="card border-secondary mb-3">
340 <div class="card-body">
342 <ul class="list-inline mb-0">
343 <li className="list-inline-item">{user.name}</li>
345 <li className="list-inline-item badge badge-danger">
346 <T i18nKey="banned">#</T>
352 {i18n.t('joined')} <MomentTime data={user} />
354 <div class="table-responsive">
355 <table class="table table-bordered table-sm mt-2 mb-0">
359 i18nKey="number_of_points"
360 interpolation={{ count: user.post_score }}
367 i18nKey="number_of_posts"
368 interpolation={{ count: user.number_of_posts }}
377 i18nKey="number_of_points"
378 interpolation={{ count: user.comment_score }}
385 i18nKey="number_of_comments"
386 interpolation={{ count: user.number_of_comments }}
394 {this.isCurrentUser && (
396 class="btn btn-block btn-secondary mt-3"
397 onClick={linkEvent(this, this.handleLogoutClick)}
399 <T i18nKey="logout">#</T>
411 <div class="card border-secondary mb-3">
412 <div class="card-body">
414 <T i18nKey="settings">#</T>
416 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
417 <div class="form-group">
420 <T i18nKey="theme">#</T>
423 value={this.state.userSettingsForm.theme}
426 this.handleUserSettingsThemeChange
428 class="ml-2 custom-select custom-select-sm w-auto"
431 <T i18nKey="theme">#</T>
433 {themes.map(theme => (
434 <option value={theme}>{theme}</option>
439 <form className="form-group">
442 <T i18nKey="sort_type" class="mr-2">
447 type_={this.state.userSettingsForm.default_listing_type}
448 onChange={this.handleUserSettingsListingTypeChange}
452 <form className="form-group">
455 <T i18nKey="type" class="mr-2">
460 sort={this.state.userSettingsForm.default_sort_type}
461 onChange={this.handleUserSettingsSortTypeChange}
465 <div class="form-group">
467 <div class="form-check">
469 class="form-check-input"
471 checked={this.state.userSettingsForm.show_nsfw}
474 this.handleUserSettingsShowNsfwChange
477 <label class="form-check-label">
478 <T i18nKey="show_nsfw">#</T>
483 <div class="form-group">
487 class="btn btn-block btn-secondary mr-4"
489 {this.state.userSettingsLoading ? (
490 <svg class="icon icon-spinner spin">
491 <use xlinkHref="#icon-spinner"></use>
494 capitalizeFirstLetter(i18n.t('save'))
500 <div class="form-group mb-0">
503 class="btn btn-block btn-danger"
506 this.handleDeleteAccountShowConfirmToggle
509 <T i18nKey="delete_account">#</T>
511 {this.state.deleteAccountShowConfirm && (
513 <div class="my-2 alert alert-danger" role="alert">
514 <T i18nKey="delete_account_confirm">#</T>
518 value={this.state.deleteAccountForm.password}
521 this.handleDeleteAccountPasswordChange
523 class="form-control my-2"
526 class="btn btn-danger mr-4"
527 disabled={!this.state.deleteAccountForm.password}
528 onClick={linkEvent(this, this.handleDeleteAccount)}
530 {this.state.deleteAccountLoading ? (
531 <svg class="icon icon-spinner spin">
532 <use xlinkHref="#icon-spinner"></use>
535 capitalizeFirstLetter(i18n.t('delete'))
539 class="btn btn-secondary"
542 this.handleDeleteAccountShowConfirmToggle
545 <T i18nKey="cancel">#</T>
561 {this.state.moderates.length > 0 && (
562 <div class="card border-secondary mb-3">
563 <div class="card-body">
565 <T i18nKey="moderates">#</T>
567 <ul class="list-unstyled mb-0">
568 {this.state.moderates.map(community => (
570 <Link to={`/c/${community.community_name}`}>
571 {community.community_name}
586 {this.state.follows.length > 0 && (
587 <div class="card border-secondary mb-3">
588 <div class="card-body">
590 <T i18nKey="subscribed">#</T>
592 <ul class="list-unstyled mb-0">
593 {this.state.follows.map(community => (
595 <Link to={`/c/${community.community_name}`}>
596 {community.community_name}
611 {this.state.page > 1 && (
613 class="btn btn-sm btn-secondary mr-1"
614 onClick={linkEvent(this, this.prevPage)}
616 <T i18nKey="prev">#</T>
620 class="btn btn-sm btn-secondary"
621 onClick={linkEvent(this, this.nextPage)}
623 <T i18nKey="next">#</T>
630 let viewStr = View[this.state.view].toLowerCase();
631 let sortStr = SortType[this.state.sort].toLowerCase();
632 this.props.history.push(
633 `/u/${this.state.user.name}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`
652 let form: GetUserDetailsForm = {
653 user_id: this.state.user_id,
654 username: this.state.username,
655 sort: SortType[this.state.sort],
656 saved_only: this.state.view == View.Saved,
657 page: this.state.page,
660 WebSocketService.Instance.getUserDetails(form);
663 handleSortChange(val: SortType) {
664 this.state.sort = val;
666 this.setState(this.state);
671 handleViewChange(i: User, event: any) {
672 i.state.view = Number(event.target.value);
679 handleUserSettingsShowNsfwChange(i: User, event: any) {
680 i.state.userSettingsForm.show_nsfw = event.target.checked;
684 handleUserSettingsThemeChange(i: User, event: any) {
685 i.state.userSettingsForm.theme = event.target.value;
686 setTheme(event.target.value);
690 handleUserSettingsSortTypeChange(val: SortType) {
691 this.state.userSettingsForm.default_sort_type = val;
692 this.setState(this.state);
695 handleUserSettingsListingTypeChange(val: ListingType) {
696 this.state.userSettingsForm.default_listing_type = val;
697 this.setState(this.state);
700 handleUserSettingsSubmit(i: User, event: any) {
701 event.preventDefault();
702 i.state.userSettingsLoading = true;
705 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
708 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
709 event.preventDefault();
710 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
714 handleDeleteAccountPasswordChange(i: User, event: any) {
715 i.state.deleteAccountForm.password = event.target.value;
719 handleLogoutClick(i: User) {
720 UserService.Instance.logout();
721 i.context.router.history.push('/');
724 handleDeleteAccount(i: User, event: any) {
725 event.preventDefault();
726 i.state.deleteAccountLoading = true;
729 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
732 parseMessage(msg: any) {
734 let op: UserOperation = msgOp(msg);
736 alert(i18n.t(msg.error));
737 this.state.deleteAccountLoading = false;
738 this.setState(this.state);
740 } else if (op == UserOperation.GetUserDetails) {
741 let res: UserDetailsResponse = msg;
742 this.state.user = res.user;
743 this.state.comments = res.comments;
744 this.state.follows = res.follows;
745 this.state.moderates = res.moderates;
746 this.state.posts = res.posts;
747 this.state.admins = res.admins;
748 this.state.loading = false;
749 if (this.isCurrentUser) {
750 this.state.userSettingsForm.show_nsfw =
751 UserService.Instance.user.show_nsfw;
752 this.state.userSettingsForm.theme = UserService.Instance.user.theme
753 ? UserService.Instance.user.theme
755 this.state.userSettingsForm.default_sort_type =
756 UserService.Instance.user.default_sort_type;
757 this.state.userSettingsForm.default_listing_type =
758 UserService.Instance.user.default_listing_type;
760 document.title = `/u/${this.state.user.name} - ${WebSocketService.Instance.site.name}`;
761 window.scrollTo(0, 0);
762 this.setState(this.state);
763 } else if (op == UserOperation.EditComment) {
764 let res: CommentResponse = msg;
766 let found = this.state.comments.find(c => c.id == res.comment.id);
767 found.content = res.comment.content;
768 found.updated = res.comment.updated;
769 found.removed = res.comment.removed;
770 found.deleted = res.comment.deleted;
771 found.upvotes = res.comment.upvotes;
772 found.downvotes = res.comment.downvotes;
773 found.score = res.comment.score;
775 this.setState(this.state);
776 } else if (op == UserOperation.CreateComment) {
777 // let res: CommentResponse = msg;
778 alert(i18n.t('reply_sent'));
779 // this.state.comments.unshift(res.comment); // TODO do this right
780 // this.setState(this.state);
781 } else if (op == UserOperation.SaveComment) {
782 let res: CommentResponse = msg;
783 let found = this.state.comments.find(c => c.id == res.comment.id);
784 found.saved = res.comment.saved;
785 this.setState(this.state);
786 } else if (op == UserOperation.CreateCommentLike) {
787 let res: CommentResponse = msg;
788 let found: Comment = this.state.comments.find(
789 c => c.id === res.comment.id
791 found.score = res.comment.score;
792 found.upvotes = res.comment.upvotes;
793 found.downvotes = res.comment.downvotes;
794 if (res.comment.my_vote !== null) found.my_vote = res.comment.my_vote;
795 this.setState(this.state);
796 } else if (op == UserOperation.BanUser) {
797 let res: BanUserResponse = msg;
799 .filter(c => c.creator_id == res.user.id)
800 .forEach(c => (c.banned = res.banned));
802 .filter(c => c.creator_id == res.user.id)
803 .forEach(c => (c.banned = res.banned));
804 this.setState(this.state);
805 } else if (op == UserOperation.AddAdmin) {
806 let res: AddAdminResponse = msg;
807 this.state.admins = res.admins;
808 this.setState(this.state);
809 } else if (op == UserOperation.SaveUserSettings) {
810 this.state = this.emptyState;
811 this.state.userSettingsLoading = false;
812 this.setState(this.state);
813 let res: LoginResponse = msg;
814 UserService.Instance.login(res);
815 } else if (op == UserOperation.DeleteAccount) {
816 this.state.deleteAccountLoading = false;
817 this.state.deleteAccountShowConfirm = false;
818 this.setState(this.state);
819 this.context.router.history.push('/');