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 { SortSelect } from './sort-select';
32 import { CommentNodes } from './comment-nodes';
33 import { MomentTime } from './moment-time';
34 import { i18n } from '../i18next';
35 import { T } from 'inferno-i18next';
48 follows: Array<CommunityUser>;
49 moderates: Array<CommunityUser>;
50 comments: Array<Comment>;
53 admins: Array<UserView>;
58 userSettingsForm: UserSettingsForm;
59 userSettingsLoading: boolean;
60 deleteAccountLoading: boolean;
61 deleteAccountShowConfirm: boolean;
62 deleteAccountForm: DeleteAccountForm;
65 export class User extends Component<any, UserState> {
66 private subscription: Subscription;
67 private emptyState: UserState = {
73 number_of_posts: null,
75 number_of_comments: null,
87 view: this.getViewFromProps(this.props),
88 sort: this.getSortTypeFromProps(this.props),
89 page: this.getPageFromProps(this.props),
95 userSettingsLoading: null,
96 deleteAccountLoading: null,
97 deleteAccountShowConfirm: false,
103 constructor(props: any, context: any) {
104 super(props, context);
106 this.state = this.emptyState;
107 this.handleSortChange = this.handleSortChange.bind(this);
109 this.state.user_id = Number(this.props.match.params.id);
110 this.state.username = this.props.match.params.username;
112 this.subscription = WebSocketService.Instance.subject
122 msg => this.parseMessage(msg),
123 err => console.error(err),
124 () => console.log('complete')
130 get isCurrentUser() {
132 UserService.Instance.user &&
133 UserService.Instance.user.id == this.state.user.id
137 getViewFromProps(props: any): View {
138 return props.match.params.view
139 ? View[capitalizeFirstLetter(props.match.params.view)]
143 getSortTypeFromProps(props: any): SortType {
144 return props.match.params.sort
145 ? routeSortTypeToEnum(props.match.params.sort)
149 getPageFromProps(props: any): number {
150 return props.match.params.page ? Number(props.match.params.page) : 1;
153 componentWillUnmount() {
154 this.subscription.unsubscribe();
157 // Necessary for back button for some reason
158 componentWillReceiveProps(nextProps: any) {
160 nextProps.history.action == 'POP' ||
161 nextProps.history.action == 'PUSH'
163 this.state.view = this.getViewFromProps(nextProps);
164 this.state.sort = this.getSortTypeFromProps(nextProps);
165 this.state.page = this.getPageFromProps(nextProps);
166 this.setState(this.state);
173 <div class="container">
174 {this.state.loading ? (
176 <svg class="icon icon-spinner spin">
177 <use xlinkHref="#icon-spinner"></use>
182 <div class="col-12 col-md-8">
183 <h5>/u/{this.state.user.name}</h5>
185 {this.state.view == View.Overview && this.overview()}
186 {this.state.view == View.Comments && this.comments()}
187 {this.state.view == View.Posts && this.posts()}
188 {this.state.view == View.Saved && this.overview()}
191 <div class="col-12 col-md-4">
193 {this.isCurrentUser && this.userSettings()}
205 <div className="mb-2">
207 value={this.state.view}
208 onChange={linkEvent(this, this.handleViewChange)}
209 class="custom-select custom-select-sm w-auto"
212 <T i18nKey="view">#</T>
214 <option value={View.Overview}>
215 <T i18nKey="overview">#</T>
217 <option value={View.Comments}>
218 <T i18nKey="comments">#</T>
220 <option value={View.Posts}>
221 <T i18nKey="posts">#</T>
223 <option value={View.Saved}>
224 <T i18nKey="saved">#</T>
229 sort={this.state.sort}
230 onChange={this.handleSortChange}
239 let combined: Array<{ type_: string; data: Comment | Post }> = [];
240 let comments = this.state.comments.map(e => {
241 return { type_: 'comments', data: e };
243 let posts = this.state.posts.map(e => {
244 return { type_: 'posts', data: e };
247 combined.push(...comments);
248 combined.push(...posts);
251 if (this.state.sort == SortType.New) {
252 combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
254 combined.sort((a, b) => b.data.score - a.data.score);
261 {i.type_ == 'posts' ? (
263 post={i.data as Post}
264 admins={this.state.admins}
270 nodes={[{ comment: i.data as Comment }]}
271 admins={this.state.admins}
284 {this.state.comments.map(comment => (
286 nodes={[{ comment: comment }]}
287 admins={this.state.admins}
298 {this.state.posts.map(post => (
301 admins={this.state.admins}
311 let user = this.state.user;
314 <div class="card border-secondary mb-3">
315 <div class="card-body">
317 <ul class="list-inline mb-0">
318 <li className="list-inline-item">{user.name}</li>
320 <li className="list-inline-item badge badge-danger">
321 <T i18nKey="banned">#</T>
327 {i18n.t('joined')} <MomentTime data={user} />
329 <div class="table-responsive">
330 <table class="table table-bordered table-sm mt-2 mb-0">
334 i18nKey="number_of_points"
335 interpolation={{ count: user.post_score }}
342 i18nKey="number_of_posts"
343 interpolation={{ count: user.number_of_posts }}
352 i18nKey="number_of_points"
353 interpolation={{ count: user.comment_score }}
360 i18nKey="number_of_comments"
361 interpolation={{ count: user.number_of_comments }}
378 <div class="card border-secondary mb-3">
379 <div class="card-body">
381 <T i18nKey="settings">#</T>
383 <form onSubmit={linkEvent(this, this.handleUserSettingsSubmit)}>
384 <div class="form-group">
387 <T i18nKey="theme">#</T>
390 value={this.state.userSettingsForm.theme}
393 this.handleUserSettingsThemeChange
395 class="ml-2 custom-select custom-select-sm w-auto"
398 <T i18nKey="theme">#</T>
400 {themes.map(theme => (
401 <option value={theme}>{theme}</option>
406 <div class="form-group">
408 <div class="form-check">
410 class="form-check-input"
412 checked={this.state.userSettingsForm.show_nsfw}
415 this.handleUserSettingsShowNsfwChange
418 <label class="form-check-label">
419 <T i18nKey="show_nsfw">#</T>
424 <div class="form-group row mb-0">
426 <button type="submit" class="btn btn-secondary mr-4">
427 {this.state.userSettingsLoading ? (
428 <svg class="icon icon-spinner spin">
429 <use xlinkHref="#icon-spinner"></use>
432 capitalizeFirstLetter(i18n.t('save'))
436 class="btn btn-danger"
439 this.handleDeleteAccountShowConfirmToggle
442 <T i18nKey="delete_account">#</T>
444 {this.state.deleteAccountShowConfirm && (
446 <div class="my-2 alert alert-danger" role="alert">
447 <T i18nKey="delete_account_confirm">#</T>
451 value={this.state.deleteAccountForm.password}
454 this.handleDeleteAccountPasswordChange
456 class="form-control my-2"
459 class="btn btn-danger mr-4"
460 disabled={!this.state.deleteAccountForm.password}
461 onClick={linkEvent(this, this.handleDeleteAccount)}
463 {this.state.deleteAccountLoading ? (
464 <svg class="icon icon-spinner spin">
465 <use xlinkHref="#icon-spinner"></use>
468 capitalizeFirstLetter(i18n.t('delete'))
472 class="btn btn-secondary"
475 this.handleDeleteAccountShowConfirmToggle
478 <T i18nKey="cancel">#</T>
494 {this.state.moderates.length > 0 && (
495 <div class="card border-secondary mb-3">
496 <div class="card-body">
498 <T i18nKey="moderates">#</T>
500 <ul class="list-unstyled mb-0">
501 {this.state.moderates.map(community => (
503 <Link to={`/c/${community.community_name}`}>
504 {community.community_name}
519 {this.state.follows.length > 0 && (
520 <div class="card border-secondary mb-3">
521 <div class="card-body">
523 <T i18nKey="subscribed">#</T>
525 <ul class="list-unstyled mb-0">
526 {this.state.follows.map(community => (
528 <Link to={`/c/${community.community_name}`}>
529 {community.community_name}
544 {this.state.page > 1 && (
546 class="btn btn-sm btn-secondary mr-1"
547 onClick={linkEvent(this, this.prevPage)}
549 <T i18nKey="prev">#</T>
553 class="btn btn-sm btn-secondary"
554 onClick={linkEvent(this, this.nextPage)}
556 <T i18nKey="next">#</T>
563 let viewStr = View[this.state.view].toLowerCase();
564 let sortStr = SortType[this.state.sort].toLowerCase();
565 this.props.history.push(
566 `/u/${this.state.user.name}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`
585 let form: GetUserDetailsForm = {
586 user_id: this.state.user_id,
587 username: this.state.username,
588 sort: SortType[this.state.sort],
589 saved_only: this.state.view == View.Saved,
590 page: this.state.page,
593 WebSocketService.Instance.getUserDetails(form);
596 handleSortChange(val: SortType) {
597 this.state.sort = val;
599 this.setState(this.state);
604 handleViewChange(i: User, event: any) {
605 i.state.view = Number(event.target.value);
612 handleUserSettingsShowNsfwChange(i: User, event: any) {
613 i.state.userSettingsForm.show_nsfw = event.target.checked;
617 handleUserSettingsThemeChange(i: User, event: any) {
618 i.state.userSettingsForm.theme = event.target.value;
619 setTheme(event.target.value);
623 handleUserSettingsSubmit(i: User, event: any) {
624 event.preventDefault();
625 i.state.userSettingsLoading = true;
628 WebSocketService.Instance.saveUserSettings(i.state.userSettingsForm);
631 handleDeleteAccountShowConfirmToggle(i: User, event: any) {
632 event.preventDefault();
633 i.state.deleteAccountShowConfirm = !i.state.deleteAccountShowConfirm;
637 handleDeleteAccountPasswordChange(i: User, event: any) {
638 i.state.deleteAccountForm.password = event.target.value;
642 handleDeleteAccount(i: User, event: any) {
643 event.preventDefault();
644 i.state.deleteAccountLoading = true;
647 WebSocketService.Instance.deleteAccount(i.state.deleteAccountForm);
650 parseMessage(msg: any) {
652 let op: UserOperation = msgOp(msg);
654 alert(i18n.t(msg.error));
655 this.state.deleteAccountLoading = false;
656 this.setState(this.state);
658 } else if (op == UserOperation.GetUserDetails) {
659 let res: UserDetailsResponse = msg;
660 this.state.user = res.user;
661 this.state.comments = res.comments;
662 this.state.follows = res.follows;
663 this.state.moderates = res.moderates;
664 this.state.posts = res.posts;
665 this.state.admins = res.admins;
666 this.state.loading = false;
667 if (this.isCurrentUser) {
668 this.state.userSettingsForm.show_nsfw =
669 UserService.Instance.user.show_nsfw;
670 this.state.userSettingsForm.theme = UserService.Instance.user.theme
671 ? UserService.Instance.user.theme
674 document.title = `/u/${this.state.user.name} - ${WebSocketService.Instance.site.name}`;
675 window.scrollTo(0, 0);
676 this.setState(this.state);
677 } else if (op == UserOperation.EditComment) {
678 let res: CommentResponse = msg;
680 let found = this.state.comments.find(c => c.id == res.comment.id);
681 found.content = res.comment.content;
682 found.updated = res.comment.updated;
683 found.removed = res.comment.removed;
684 found.deleted = res.comment.deleted;
685 found.upvotes = res.comment.upvotes;
686 found.downvotes = res.comment.downvotes;
687 found.score = res.comment.score;
689 this.setState(this.state);
690 } else if (op == UserOperation.CreateComment) {
691 // let res: CommentResponse = msg;
692 alert(i18n.t('reply_sent'));
693 // this.state.comments.unshift(res.comment); // TODO do this right
694 // this.setState(this.state);
695 } else if (op == UserOperation.SaveComment) {
696 let res: CommentResponse = msg;
697 let found = this.state.comments.find(c => c.id == res.comment.id);
698 found.saved = res.comment.saved;
699 this.setState(this.state);
700 } else if (op == UserOperation.CreateCommentLike) {
701 let res: CommentResponse = msg;
702 let found: Comment = this.state.comments.find(
703 c => c.id === res.comment.id
705 found.score = res.comment.score;
706 found.upvotes = res.comment.upvotes;
707 found.downvotes = res.comment.downvotes;
708 if (res.comment.my_vote !== null) found.my_vote = res.comment.my_vote;
709 this.setState(this.state);
710 } else if (op == UserOperation.BanUser) {
711 let res: BanUserResponse = msg;
713 .filter(c => c.creator_id == res.user.id)
714 .forEach(c => (c.banned = res.banned));
716 .filter(c => c.creator_id == res.user.id)
717 .forEach(c => (c.banned = res.banned));
718 this.setState(this.state);
719 } else if (op == UserOperation.AddAdmin) {
720 let res: AddAdminResponse = msg;
721 this.state.admins = res.admins;
722 this.setState(this.state);
723 } else if (op == UserOperation.SaveUserSettings) {
724 this.state = this.emptyState;
725 this.state.userSettingsLoading = false;
726 this.setState(this.state);
727 let res: LoginResponse = msg;
728 UserService.Instance.login(res);
729 } else if (op == UserOperation.DeleteAccount) {
730 this.state.deleteAccountLoading = false;
731 this.state.deleteAccountShowConfirm = false;
732 this.setState(this.state);
733 this.context.router.history.push('/');