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';
20 } from '../interfaces';
21 import { WebSocketService, UserService } from '../services';
26 capitalizeFirstLetter,
30 import { PostListing } from './post-listing';
31 import { CommentNodes } from './comment-nodes';
32 import { MomentTime } from './moment-time';
33 import { i18n } from '../i18next';
34 import { T } from 'inferno-i18next';
47 follows: Array<CommunityUser>;
48 moderates: Array<CommunityUser>;
49 comments: Array<Comment>;
52 admins: Array<UserView>;
57 userSettingsForm: UserSettingsForm;
58 userSettingsLoading: boolean;
59 deleteAccountLoading: boolean;
60 deleteAccountShowConfirm: boolean;
61 deleteAccountForm: DeleteAccountForm;
64 export class User extends Component<any, UserState> {
65 private subscription: Subscription;
66 private emptyState: UserState = {
72 number_of_posts: null,
74 number_of_comments: null,
86 view: this.getViewFromProps(this.props),
87 sort: this.getSortTypeFromProps(this.props),
88 page: this.getPageFromProps(this.props),
94 userSettingsLoading: null,
95 deleteAccountLoading: null,
96 deleteAccountShowConfirm: false,
102 constructor(props: any, context: any) {
103 super(props, context);
105 this.state = this.emptyState;
107 this.state.user_id = Number(this.props.match.params.id);
108 this.state.username = this.props.match.params.username;
110 this.subscription = WebSocketService.Instance.subject
120 msg => this.parseMessage(msg),
121 err => console.error(err),
122 () => console.log('complete')
128 get isCurrentUser() {
130 UserService.Instance.user &&
131 UserService.Instance.user.id == this.state.user.id
135 getViewFromProps(props: any): View {
136 return props.match.params.view
137 ? View[capitalizeFirstLetter(props.match.params.view)]
141 getSortTypeFromProps(props: any): SortType {
142 return props.match.params.sort
143 ? routeSortTypeToEnum(props.match.params.sort)
147 getPageFromProps(props: any): number {
148 return props.match.params.page ? Number(props.match.params.page) : 1;
151 componentWillUnmount() {
152 this.subscription.unsubscribe();
155 // Necessary for back button for some reason
156 componentWillReceiveProps(nextProps: any) {
157 if (nextProps.history.action == 'POP') {
158 this.state = this.emptyState;
159 this.state.view = this.getViewFromProps(nextProps);
160 this.state.sort = this.getSortTypeFromProps(nextProps);
161 this.state.page = this.getPageFromProps(nextProps);
168 <div class="container">
169 {this.state.loading ? (
171 <svg class="icon icon-spinner spin">
172 <use xlinkHref="#icon-spinner"></use>
177 <div class="col-12 col-md-8">
178 <h5>/u/{this.state.user.name}</h5>
180 {this.state.view == View.Overview && this.overview()}
181 {this.state.view == View.Comments && this.comments()}
182 {this.state.view == View.Posts && this.posts()}
183 {this.state.view == View.Saved && this.overview()}
186 <div class="col-12 col-md-4">
188 {this.isCurrentUser && this.userSettings()}
200 <div className="mb-2">
202 value={this.state.view}
203 onChange={linkEvent(this, this.handleViewChange)}
204 class="custom-select custom-select-sm w-auto"
207 <T i18nKey="view">#</T>
209 <option value={View.Overview}>
210 <T i18nKey="overview">#</T>
212 <option value={View.Comments}>
213 <T i18nKey="comments">#</T>
215 <option value={View.Posts}>
216 <T i18nKey="posts">#</T>
218 <option value={View.Saved}>
219 <T i18nKey="saved">#</T>
223 value={this.state.sort}
224 onChange={linkEvent(this, this.handleSortChange)}
225 class="custom-select custom-select-sm w-auto ml-2"
228 <T i18nKey="sort_type">#</T>
230 <option value={SortType.New}>
231 <T i18nKey="new">#</T>
233 <option value={SortType.TopDay}>
234 <T i18nKey="top_day">#</T>
236 <option value={SortType.TopWeek}>
237 <T i18nKey="week">#</T>
239 <option value={SortType.TopMonth}>
240 <T i18nKey="month">#</T>
242 <option value={SortType.TopYear}>
243 <T i18nKey="year">#</T>
245 <option value={SortType.TopAll}>
246 <T i18nKey="all">#</T>
254 let combined: Array<{ type_: string; data: Comment | Post }> = [];
255 let comments = this.state.comments.map(e => {
256 return { type_: 'comments', data: e };
258 let posts = this.state.posts.map(e => {
259 return { type_: 'posts', data: e };
262 combined.push(...comments);
263 combined.push(...posts);
266 if (this.state.sort == SortType.New) {
267 combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
269 combined.sort((a, b) => b.data.score - a.data.score);
276 {i.type_ == 'posts' ? (
278 post={i.data as Post}
279 admins={this.state.admins}
285 nodes={[{ comment: i.data as Comment }]}
286 admins={this.state.admins}
299 {this.state.comments.map(comment => (
301 nodes={[{ comment: comment }]}
302 admins={this.state.admins}
313 {this.state.posts.map(post => (
316 admins={this.state.admins}
326 let user = this.state.user;
329 <div class="card border-secondary mb-3">
330 <div class="card-body">
332 <ul class="list-inline mb-0">
333 <li className="list-inline-item">{user.name}</li>
335 <li className="list-inline-item badge badge-danger">
336 <T i18nKey="banned">#</T>
342 {i18n.t('joined')} <MomentTime data={user} />
344 <div class="table-responsive">
345 <table class="table table-bordered table-sm mt-2 mb-0">
349 i18nKey="number_of_points"
350 interpolation={{ count: user.post_score }}
357 i18nKey="number_of_posts"
358 interpolation={{ count: user.number_of_posts }}
367 i18nKey="number_of_points"
368 interpolation={{ count: user.comment_score }}
375 i18nKey="number_of_comments"
376 interpolation={{ count: user.number_of_comments }}
393 <div class="card border-secondary mb-3">
394 <div class="card-body">
396 <T i18nKey="settings">#</T>
398 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
399 <div class="form-group">
402 <T i18nKey="theme">#</T>
405 value={this.state.userSettingsForm.theme}
408 this.handleUserSettingsThemeChange
410 class="ml-2 custom-select custom-select-sm w-auto"
413 <T i18nKey="theme">#</T>
415 {themes.map(theme => (
416 <option value={theme}>{theme}</option>
421 <div class="form-group">
423 <div class="form-check">
425 class="form-check-input"
427 checked={this.state.userSettingsForm.show_nsfw}
430 this.handleUserSettingsShowNsfwChange
433 <label class="form-check-label">
434 <T i18nKey="show_nsfw">#</T>
439 <div class="form-group row mb-0">
441 <button type="submit" class="btn btn-secondary mr-4">
442 {this.state.userSettingsLoading ? (
443 <svg class="icon icon-spinner spin">
444 <use xlinkHref="#icon-spinner"></use>
447 capitalizeFirstLetter(i18n.t('save'))
451 class="btn btn-danger"
454 this.handleDeleteAccountShowConfirmToggle
457 <T i18nKey="delete_account">#</T>
459 {this.state.deleteAccountShowConfirm && (
461 <div class="my-2 alert alert-danger" role="alert">
462 <T i18nKey="delete_account_confirm">#</T>
466 value={this.state.deleteAccountForm.password}
469 this.handleDeleteAccountPasswordChange
471 class="form-control my-2"
474 class="btn btn-danger mr-4"
475 disabled={!this.state.deleteAccountForm.password}
476 onClick={linkEvent(this, this.handleDeleteAccount)}
478 {this.state.deleteAccountLoading ? (
479 <svg class="icon icon-spinner spin">
480 <use xlinkHref="#icon-spinner"></use>
483 capitalizeFirstLetter(i18n.t('delete'))
487 class="btn btn-secondary"
490 this.handleDeleteAccountShowConfirmToggle
493 <T i18nKey="cancel">#</T>
509 {this.state.moderates.length > 0 && (
510 <div class="card border-secondary mb-3">
511 <div class="card-body">
513 <T i18nKey="moderates">#</T>
515 <ul class="list-unstyled mb-0">
516 {this.state.moderates.map(community => (
518 <Link to={`/c/${community.community_name}`}>
519 {community.community_name}
534 {this.state.follows.length > 0 && (
535 <div class="card border-secondary mb-3">
536 <div class="card-body">
538 <T i18nKey="subscribed">#</T>
540 <ul class="list-unstyled mb-0">
541 {this.state.follows.map(community => (
543 <Link to={`/c/${community.community_name}`}>
544 {community.community_name}
559 {this.state.page > 1 && (
561 class="btn btn-sm btn-secondary mr-1"
562 onClick={linkEvent(this, this.prevPage)}
564 <T i18nKey="prev">#</T>
568 class="btn btn-sm btn-secondary"
569 onClick={linkEvent(this, this.nextPage)}
571 <T i18nKey="next">#</T>
578 let viewStr = View[this.state.view].toLowerCase();
579 let sortStr = SortType[this.state.sort].toLowerCase();
580 this.props.history.push(
581 `/u/${this.state.user.name}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`
600 let form: GetUserDetailsForm = {
601 user_id: this.state.user_id,
602 username: this.state.username,
603 sort: SortType[this.state.sort],
604 saved_only: this.state.view == View.Saved,
605 page: this.state.page,
608 WebSocketService.Instance.getUserDetails(form);
611 handleSortChange(i: User, event: any) {
612 i.state.sort = Number(event.target.value);
619 handleViewChange(i: User, event: any) {
620 i.state.view = Number(event.target.value);
627 handleUserSettingsShowNsfwChange(i: User, event: any) {
628 i.state.userSettingsForm.show_nsfw = event.target.checked;
632 handleUserSettingsThemeChange(i: User, event: any) {
633 i.state.userSettingsForm.theme = event.target.value;
634 setTheme(event.target.value);
638 handleUserSettingsSubmit(i: User, event: any) {
639 event.preventDefault();
640 i.state.userSettingsLoading = true;
643 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
646 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
647 event.preventDefault();
648 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
652 handleDeleteAccountPasswordChange(i: User, event: any) {
653 i.state.deleteAccountForm.password = event.target.value;
657 handleDeleteAccount(i: User, event: any) {
658 event.preventDefault();
659 i.state.deleteAccountLoading = true;
662 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
665 parseMessage(msg: any) {
667 let op: UserOperation = msgOp(msg);
669 alert(i18n.t(msg.error));
670 this.state.deleteAccountLoading = false;
671 this.setState(this.state);
673 } else if (op == UserOperation.GetUserDetails) {
674 let res: UserDetailsResponse = msg;
675 this.state.user = res.user;
676 this.state.comments = res.comments;
677 this.state.follows = res.follows;
678 this.state.moderates = res.moderates;
679 this.state.posts = res.posts;
680 this.state.admins = res.admins;
681 this.state.loading = false;
682 if (this.isCurrentUser) {
683 this.state.userSettingsForm.show_nsfw =
684 UserService.Instance.user.show_nsfw;
685 this.state.userSettingsForm.theme = UserService.Instance.user.theme
686 ? UserService.Instance.user.theme
689 document.title = `/u/${this.state.user.name} - ${WebSocketService.Instance.site.name}`;
690 window.scrollTo(0, 0);
691 this.setState(this.state);
692 } else if (op == UserOperation.EditComment) {
693 let res: CommentResponse = msg;
695 let found = this.state.comments.find(c => c.id == res.comment.id);
696 found.content = res.comment.content;
697 found.updated = res.comment.updated;
698 found.removed = res.comment.removed;
699 found.deleted = res.comment.deleted;
700 found.upvotes = res.comment.upvotes;
701 found.downvotes = res.comment.downvotes;
702 found.score = res.comment.score;
704 this.setState(this.state);
705 } else if (op == UserOperation.CreateComment) {
706 // let res: CommentResponse = msg;
707 alert(i18n.t('reply_sent'));
708 // this.state.comments.unshift(res.comment); // TODO do this right
709 // this.setState(this.state);
710 } else if (op == UserOperation.SaveComment) {
711 let res: CommentResponse = msg;
712 let found = this.state.comments.find(c => c.id == res.comment.id);
713 found.saved = res.comment.saved;
714 this.setState(this.state);
715 } else if (op == UserOperation.CreateCommentLike) {
716 let res: CommentResponse = msg;
717 let found: Comment = this.state.comments.find(
718 c => c.id === res.comment.id
720 found.score = res.comment.score;
721 found.upvotes = res.comment.upvotes;
722 found.downvotes = res.comment.downvotes;
723 if (res.comment.my_vote !== null) found.my_vote = res.comment.my_vote;
724 this.setState(this.state);
725 } else if (op == UserOperation.BanUser) {
726 let res: BanUserResponse = msg;
728 .filter(c => c.creator_id == res.user.id)
729 .forEach(c => (c.banned = res.banned));
731 .filter(c => c.creator_id == res.user.id)
732 .forEach(c => (c.banned = res.banned));
733 this.setState(this.state);
734 } else if (op == UserOperation.AddAdmin) {
735 let res: AddAdminResponse = msg;
736 this.state.admins = res.admins;
737 this.setState(this.state);
738 } else if (op == UserOperation.SaveUserSettings) {
739 this.state = this.emptyState;
740 this.state.userSettingsLoading = false;
741 this.setState(this.state);
742 let res: LoginResponse = msg;
743 UserService.Instance.login(res);
744 } else if (op == UserOperation.DeleteAccount) {
745 this.state.deleteAccountLoading = false;
746 this.state.deleteAccountShowConfirm = false;
747 this.setState(this.state);
748 this.context.router.history.push('/');