2019-04-08 05:19:02 +00:00
|
|
|
import { Component, linkEvent } from 'inferno';
|
|
|
|
import { Link } from 'inferno-router';
|
|
|
|
import { Subscription } from "rxjs";
|
|
|
|
import { retryWhen, delay, take } from 'rxjs/operators';
|
2019-04-22 18:32:50 +00:00
|
|
|
import { UserOperation, Post, Comment, CommunityUser, GetUserDetailsForm, SortType, UserDetailsResponse, UserView, CommentResponse } from '../interfaces';
|
2019-04-08 05:19:02 +00:00
|
|
|
import { WebSocketService } from '../services';
|
2019-04-29 00:41:24 +00:00
|
|
|
import { msgOp, fetchLimit, routeSortTypeToEnum, capitalizeFirstLetter } from '../utils';
|
2019-04-08 05:19:02 +00:00
|
|
|
import { PostListing } from './post-listing';
|
|
|
|
import { CommentNodes } from './comment-nodes';
|
|
|
|
import { MomentTime } from './moment-time';
|
|
|
|
|
|
|
|
enum View {
|
|
|
|
Overview, Comments, Posts, Saved
|
|
|
|
}
|
|
|
|
|
|
|
|
interface UserState {
|
|
|
|
user: UserView;
|
2019-04-17 18:30:13 +00:00
|
|
|
user_id: number;
|
2019-04-25 21:52:18 +00:00
|
|
|
username: string;
|
2019-04-08 05:19:02 +00:00
|
|
|
follows: Array<CommunityUser>;
|
|
|
|
moderates: Array<CommunityUser>;
|
|
|
|
comments: Array<Comment>;
|
|
|
|
posts: Array<Post>;
|
|
|
|
saved?: Array<Post>;
|
|
|
|
view: View;
|
|
|
|
sort: SortType;
|
2019-04-17 18:30:13 +00:00
|
|
|
page: number;
|
2019-04-29 00:41:24 +00:00
|
|
|
loading: boolean;
|
2019-04-08 05:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class User extends Component<any, UserState> {
|
|
|
|
|
|
|
|
private subscription: Subscription;
|
|
|
|
private emptyState: UserState = {
|
|
|
|
user: {
|
|
|
|
id: null,
|
|
|
|
name: null,
|
|
|
|
fedi_name: null,
|
|
|
|
published: null,
|
|
|
|
number_of_posts: null,
|
|
|
|
post_score: null,
|
|
|
|
number_of_comments: null,
|
|
|
|
comment_score: null,
|
|
|
|
},
|
2019-04-17 18:30:13 +00:00
|
|
|
user_id: null,
|
2019-04-25 21:52:18 +00:00
|
|
|
username: null,
|
2019-04-08 05:19:02 +00:00
|
|
|
follows: [],
|
|
|
|
moderates: [],
|
|
|
|
comments: [],
|
|
|
|
posts: [],
|
2019-04-29 00:41:24 +00:00
|
|
|
loading: true,
|
|
|
|
view: this.getViewFromProps(this.props),
|
|
|
|
sort: this.getSortTypeFromProps(this.props),
|
|
|
|
page: this.getPageFromProps(this.props),
|
2019-04-08 05:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.state = this.emptyState;
|
|
|
|
|
2019-04-17 18:30:13 +00:00
|
|
|
this.state.user_id = Number(this.props.match.params.id);
|
2019-04-25 21:52:18 +00:00
|
|
|
this.state.username = this.props.match.params.username;
|
2019-04-08 05:19:02 +00:00
|
|
|
|
|
|
|
this.subscription = WebSocketService.Instance.subject
|
|
|
|
.pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
|
|
|
|
.subscribe(
|
|
|
|
(msg) => this.parseMessage(msg),
|
|
|
|
(err) => console.error(err),
|
|
|
|
() => console.log('complete')
|
|
|
|
);
|
|
|
|
|
2019-04-17 18:30:13 +00:00
|
|
|
this.refetch();
|
2019-04-08 05:19:02 +00:00
|
|
|
}
|
|
|
|
|
2019-04-29 00:41:24 +00:00
|
|
|
getViewFromProps(props: any): View {
|
|
|
|
return (props.match.params.view) ?
|
|
|
|
View[capitalizeFirstLetter(props.match.params.view)] :
|
|
|
|
View.Overview;
|
|
|
|
}
|
|
|
|
|
|
|
|
getSortTypeFromProps(props: any): SortType {
|
|
|
|
return (props.match.params.sort) ?
|
|
|
|
routeSortTypeToEnum(props.match.params.sort) :
|
|
|
|
SortType.New;
|
|
|
|
}
|
|
|
|
|
|
|
|
getPageFromProps(props: any): number {
|
|
|
|
return (props.match.params.page) ? Number(props.match.params.page) : 1;
|
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
|
2019-04-29 00:41:24 +00:00
|
|
|
// Necessary for back button for some reason
|
|
|
|
componentWillReceiveProps(nextProps: any) {
|
|
|
|
if (nextProps.history.action == 'POP') {
|
|
|
|
this.state = this.emptyState;
|
|
|
|
this.state.view = this.getViewFromProps(nextProps);
|
|
|
|
this.state.sort = this.getSortTypeFromProps(nextProps);
|
|
|
|
this.state.page = this.getPageFromProps(nextProps);
|
|
|
|
this.refetch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div class="container">
|
2019-04-29 00:41:24 +00:00
|
|
|
{this.state.loading ?
|
|
|
|
<h5><svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg></h5> :
|
2019-04-08 05:19:02 +00:00
|
|
|
<div class="row">
|
2019-04-09 00:04:03 +00:00
|
|
|
<div class="col-12 col-md-9">
|
2019-04-20 04:06:25 +00:00
|
|
|
<h5>/u/{this.state.user.name}</h5>
|
2019-04-08 05:19:02 +00:00
|
|
|
{this.selects()}
|
|
|
|
{this.state.view == View.Overview &&
|
|
|
|
this.overview()
|
|
|
|
}
|
|
|
|
{this.state.view == View.Comments &&
|
|
|
|
this.comments()
|
|
|
|
}
|
|
|
|
{this.state.view == View.Posts &&
|
|
|
|
this.posts()
|
|
|
|
}
|
2019-04-20 04:06:25 +00:00
|
|
|
{this.state.view == View.Saved &&
|
|
|
|
this.overview()
|
|
|
|
}
|
2019-04-17 18:30:13 +00:00
|
|
|
{this.paginator()}
|
2019-04-08 05:19:02 +00:00
|
|
|
</div>
|
2019-04-09 00:04:03 +00:00
|
|
|
<div class="col-12 col-md-3">
|
2019-04-08 05:19:02 +00:00
|
|
|
{this.userInfo()}
|
|
|
|
{this.moderates()}
|
|
|
|
{this.follows()}
|
|
|
|
</div>
|
|
|
|
</div>
|
2019-04-29 00:41:24 +00:00
|
|
|
}
|
2019-04-08 05:19:02 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
selects() {
|
|
|
|
return (
|
|
|
|
<div className="mb-2">
|
2019-05-02 05:26:31 +00:00
|
|
|
<select value={this.state.view} onChange={linkEvent(this, this.handleViewChange)} class="custom-select custom-select-sm w-auto">
|
2019-04-08 05:19:02 +00:00
|
|
|
<option disabled>View</option>
|
|
|
|
<option value={View.Overview}>Overview</option>
|
|
|
|
<option value={View.Comments}>Comments</option>
|
|
|
|
<option value={View.Posts}>Posts</option>
|
2019-04-20 04:06:25 +00:00
|
|
|
<option value={View.Saved}>Saved</option>
|
2019-04-08 05:19:02 +00:00
|
|
|
</select>
|
2019-05-02 05:26:31 +00:00
|
|
|
<select value={this.state.sort} onChange={linkEvent(this, this.handleSortChange)} class="custom-select custom-select-sm w-auto ml-2">
|
2019-04-08 05:19:02 +00:00
|
|
|
<option disabled>Sort Type</option>
|
|
|
|
<option value={SortType.New}>New</option>
|
|
|
|
<option value={SortType.TopDay}>Top Day</option>
|
|
|
|
<option value={SortType.TopWeek}>Week</option>
|
|
|
|
<option value={SortType.TopMonth}>Month</option>
|
|
|
|
<option value={SortType.TopYear}>Year</option>
|
|
|
|
<option value={SortType.TopAll}>All</option>
|
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
overview() {
|
2019-04-15 23:12:06 +00:00
|
|
|
let combined: Array<{type_: string, data: Comment | Post}> = [];
|
|
|
|
let comments = this.state.comments.map(e => {return {type_: "comments", data: e}});
|
|
|
|
let posts = this.state.posts.map(e => {return {type_: "posts", data: e}});
|
|
|
|
|
|
|
|
combined.push(...comments);
|
|
|
|
combined.push(...posts);
|
2019-04-08 05:19:02 +00:00
|
|
|
|
|
|
|
// Sort it
|
|
|
|
if (this.state.sort == SortType.New) {
|
2019-04-15 23:12:06 +00:00
|
|
|
combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
|
2019-04-08 05:19:02 +00:00
|
|
|
} else {
|
2019-04-15 23:12:06 +00:00
|
|
|
combined.sort((a, b) => b.data.score - a.data.score);
|
2019-04-08 05:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{combined.map(i =>
|
|
|
|
<div>
|
2019-04-15 23:12:06 +00:00
|
|
|
{i.type_ == "posts"
|
|
|
|
? <PostListing post={i.data as Post} showCommunity viewOnly />
|
2019-04-22 18:32:50 +00:00
|
|
|
: <CommentNodes nodes={[{comment: i.data as Comment}]} noIndent />
|
2019-04-08 05:19:02 +00:00
|
|
|
}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
comments() {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{this.state.comments.map(comment =>
|
|
|
|
<CommentNodes nodes={[{comment: comment}]} noIndent viewOnly />
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
posts() {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{this.state.posts.map(post =>
|
|
|
|
<PostListing post={post} showCommunity viewOnly />
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
userInfo() {
|
|
|
|
let user = this.state.user;
|
|
|
|
return (
|
|
|
|
<div>
|
2019-04-20 04:06:25 +00:00
|
|
|
<h5>{user.name}</h5>
|
2019-04-08 05:19:02 +00:00
|
|
|
<div>Joined <MomentTime data={user} /></div>
|
|
|
|
<table class="table table-bordered table-sm mt-2">
|
|
|
|
<tr>
|
|
|
|
<td>{user.post_score} points</td>
|
|
|
|
<td>{user.number_of_posts} posts</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>{user.comment_score} points</td>
|
|
|
|
<td>{user.number_of_comments} comments</td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
<hr />
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
moderates() {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{this.state.moderates.length > 0 &&
|
|
|
|
<div>
|
2019-04-20 04:06:25 +00:00
|
|
|
<h5>Moderates</h5>
|
2019-04-08 05:19:02 +00:00
|
|
|
<ul class="list-unstyled">
|
|
|
|
{this.state.moderates.map(community =>
|
2019-04-29 02:05:11 +00:00
|
|
|
<li><Link to={`/c/${community.community_name}`}>{community.community_name}</Link></li>
|
2019-04-08 05:19:02 +00:00
|
|
|
)}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
follows() {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{this.state.follows.length > 0 &&
|
|
|
|
<div>
|
|
|
|
<hr />
|
2019-04-20 04:06:25 +00:00
|
|
|
<h5>Subscribed</h5>
|
2019-04-08 05:19:02 +00:00
|
|
|
<ul class="list-unstyled">
|
|
|
|
{this.state.follows.map(community =>
|
2019-04-29 02:05:11 +00:00
|
|
|
<li><Link to={`/c/${community.community_name}`}>{community.community_name}</Link></li>
|
2019-04-08 05:19:02 +00:00
|
|
|
)}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-04-17 18:30:13 +00:00
|
|
|
paginator() {
|
|
|
|
return (
|
|
|
|
<div class="mt-2">
|
|
|
|
{this.state.page > 1 &&
|
|
|
|
<button class="btn btn-sm btn-secondary mr-1" onClick={linkEvent(this, this.prevPage)}>Prev</button>
|
|
|
|
}
|
|
|
|
<button class="btn btn-sm btn-secondary" onClick={linkEvent(this, this.nextPage)}>Next</button>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-29 00:41:24 +00:00
|
|
|
updateUrl() {
|
|
|
|
let viewStr = View[this.state.view].toLowerCase();
|
|
|
|
let sortStr = SortType[this.state.sort].toLowerCase();
|
|
|
|
this.props.history.push(`/u/${this.state.user.name}/view/${viewStr}/sort/${sortStr}/page/${this.state.page}`);
|
|
|
|
}
|
|
|
|
|
2019-04-17 18:30:13 +00:00
|
|
|
nextPage(i: User) {
|
|
|
|
i.state.page++;
|
|
|
|
i.setState(i.state);
|
2019-04-29 00:41:24 +00:00
|
|
|
i.updateUrl();
|
2019-04-17 18:30:13 +00:00
|
|
|
i.refetch();
|
|
|
|
}
|
|
|
|
|
|
|
|
prevPage(i: User) {
|
|
|
|
i.state.page--;
|
2019-04-08 05:19:02 +00:00
|
|
|
i.setState(i.state);
|
2019-04-29 00:41:24 +00:00
|
|
|
i.updateUrl();
|
2019-04-17 18:30:13 +00:00
|
|
|
i.refetch();
|
|
|
|
}
|
2019-04-08 05:19:02 +00:00
|
|
|
|
2019-04-17 18:30:13 +00:00
|
|
|
refetch() {
|
2019-04-08 05:19:02 +00:00
|
|
|
let form: GetUserDetailsForm = {
|
2019-04-17 18:30:13 +00:00
|
|
|
user_id: this.state.user_id,
|
2019-04-25 21:52:18 +00:00
|
|
|
username: this.state.username,
|
2019-04-17 18:30:13 +00:00
|
|
|
sort: SortType[this.state.sort],
|
2019-04-20 04:06:25 +00:00
|
|
|
saved_only: this.state.view == View.Saved,
|
2019-04-17 18:30:13 +00:00
|
|
|
page: this.state.page,
|
|
|
|
limit: fetchLimit,
|
2019-04-08 05:19:02 +00:00
|
|
|
};
|
|
|
|
WebSocketService.Instance.getUserDetails(form);
|
|
|
|
}
|
|
|
|
|
2019-04-17 18:30:13 +00:00
|
|
|
handleSortChange(i: User, event: any) {
|
|
|
|
i.state.sort = Number(event.target.value);
|
|
|
|
i.state.page = 1;
|
|
|
|
i.setState(i.state);
|
2019-04-29 00:41:24 +00:00
|
|
|
i.updateUrl();
|
2019-04-17 18:30:13 +00:00
|
|
|
i.refetch();
|
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handleViewChange(i: User, event: any) {
|
|
|
|
i.state.view = Number(event.target.value);
|
2019-04-17 18:30:13 +00:00
|
|
|
i.state.page = 1;
|
2019-04-08 05:19:02 +00:00
|
|
|
i.setState(i.state);
|
2019-04-29 00:41:24 +00:00
|
|
|
i.updateUrl();
|
2019-04-17 18:30:13 +00:00
|
|
|
i.refetch();
|
2019-04-08 05:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parseMessage(msg: any) {
|
|
|
|
console.log(msg);
|
|
|
|
let op: UserOperation = msgOp(msg);
|
|
|
|
if (msg.error) {
|
|
|
|
alert(msg.error);
|
|
|
|
return;
|
|
|
|
} else if (op == UserOperation.GetUserDetails) {
|
|
|
|
let res: UserDetailsResponse = msg;
|
|
|
|
this.state.user = res.user;
|
|
|
|
this.state.comments = res.comments;
|
|
|
|
this.state.follows = res.follows;
|
|
|
|
this.state.moderates = res.moderates;
|
|
|
|
this.state.posts = res.posts;
|
2019-04-29 00:41:24 +00:00
|
|
|
this.state.loading = false;
|
2019-04-22 16:24:13 +00:00
|
|
|
document.title = `/u/${this.state.user.name} - Lemmy`;
|
2019-05-02 22:16:38 +00:00
|
|
|
window.scrollTo(0,0);
|
2019-04-08 05:19:02 +00:00
|
|
|
this.setState(this.state);
|
2019-04-22 18:32:50 +00:00
|
|
|
} else if (op == UserOperation.EditComment) {
|
|
|
|
let res: CommentResponse = msg;
|
|
|
|
|
|
|
|
let found = this.state.comments.find(c => c.id == res.comment.id);
|
|
|
|
found.content = res.comment.content;
|
|
|
|
found.updated = res.comment.updated;
|
|
|
|
found.removed = res.comment.removed;
|
2019-04-29 19:14:54 +00:00
|
|
|
found.deleted = res.comment.deleted;
|
2019-04-22 18:32:50 +00:00
|
|
|
found.upvotes = res.comment.upvotes;
|
|
|
|
found.downvotes = res.comment.downvotes;
|
|
|
|
found.score = res.comment.score;
|
|
|
|
|
|
|
|
this.setState(this.state);
|
|
|
|
} else if (op == UserOperation.CreateComment) {
|
|
|
|
// let res: CommentResponse = msg;
|
|
|
|
alert('Reply sent');
|
|
|
|
// this.state.comments.unshift(res.comment); // TODO do this right
|
|
|
|
// this.setState(this.state);
|
|
|
|
} else if (op == UserOperation.SaveComment) {
|
|
|
|
let res: CommentResponse = msg;
|
|
|
|
let found = this.state.comments.find(c => c.id == res.comment.id);
|
|
|
|
found.saved = res.comment.saved;
|
|
|
|
this.setState(this.state);
|
|
|
|
} else if (op == UserOperation.CreateCommentLike) {
|
|
|
|
let res: CommentResponse = msg;
|
|
|
|
let found: Comment = this.state.comments.find(c => c.id === res.comment.id);
|
|
|
|
found.score = res.comment.score;
|
|
|
|
found.upvotes = res.comment.upvotes;
|
|
|
|
found.downvotes = res.comment.downvotes;
|
|
|
|
if (res.comment.my_vote !== null)
|
|
|
|
found.my_vote = res.comment.my_vote;
|
|
|
|
this.setState(this.state);
|
|
|
|
}
|
2019-04-08 05:19:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|