2019-04-29 00:19:04 +00:00
|
|
|
import { Component, linkEvent } from 'inferno';
|
2019-03-26 18:00:18 +00:00
|
|
|
import { Subscription } from "rxjs";
|
|
|
|
import { retryWhen, delay, take } from 'rxjs/operators';
|
2019-04-29 00:19:04 +00:00
|
|
|
import { UserOperation, Community as CommunityI, GetCommunityResponse, CommunityResponse, CommunityUser, UserView, SortType, Post, GetPostsForm, ListingType, GetPostsResponse, CreatePostLikeResponse } from '../interfaces';
|
2019-04-08 05:19:02 +00:00
|
|
|
import { WebSocketService } from '../services';
|
2019-04-05 19:14:54 +00:00
|
|
|
import { PostListings } from './post-listings';
|
2019-04-03 23:01:20 +00:00
|
|
|
import { Sidebar } from './sidebar';
|
2019-04-29 00:19:04 +00:00
|
|
|
import { msgOp, routeSortTypeToEnum, fetchLimit } from '../utils';
|
2019-03-26 18:00:18 +00:00
|
|
|
|
|
|
|
interface State {
|
|
|
|
community: CommunityI;
|
2019-04-05 19:14:54 +00:00
|
|
|
communityId: number;
|
2019-04-25 21:52:18 +00:00
|
|
|
communityName: string;
|
2019-04-04 20:53:32 +00:00
|
|
|
moderators: Array<CommunityUser>;
|
2019-04-21 20:52:55 +00:00
|
|
|
admins: Array<UserView>;
|
2019-04-08 21:46:09 +00:00
|
|
|
loading: boolean;
|
2019-04-29 00:19:04 +00:00
|
|
|
posts: Array<Post>;
|
|
|
|
sort: SortType;
|
|
|
|
page: number;
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class Community extends Component<any, State> {
|
|
|
|
|
|
|
|
private subscription: Subscription;
|
|
|
|
private emptyState: State = {
|
|
|
|
community: {
|
|
|
|
id: null,
|
|
|
|
name: null,
|
2019-04-03 23:01:20 +00:00
|
|
|
title: null,
|
|
|
|
category_id: null,
|
|
|
|
category_name: null,
|
|
|
|
creator_id: null,
|
|
|
|
creator_name: null,
|
|
|
|
number_of_subscribers: null,
|
|
|
|
number_of_posts: null,
|
2019-04-04 20:53:32 +00:00
|
|
|
number_of_comments: null,
|
2019-04-21 20:52:55 +00:00
|
|
|
published: null,
|
|
|
|
removed: null,
|
2019-05-06 16:26:21 +00:00
|
|
|
deleted: null,
|
2019-03-26 18:00:18 +00:00
|
|
|
},
|
2019-04-04 20:53:32 +00:00
|
|
|
moderators: [],
|
2019-04-21 20:52:55 +00:00
|
|
|
admins: [],
|
2019-04-08 21:46:09 +00:00
|
|
|
communityId: Number(this.props.match.params.id),
|
2019-04-25 21:52:18 +00:00
|
|
|
communityName: this.props.match.params.name,
|
2019-04-29 00:19:04 +00:00
|
|
|
loading: true,
|
|
|
|
posts: [],
|
|
|
|
sort: this.getSortTypeFromProps(this.props),
|
|
|
|
page: this.getPageFromProps(this.props),
|
|
|
|
}
|
|
|
|
|
|
|
|
getSortTypeFromProps(props: any): SortType {
|
|
|
|
return (props.match.params.sort) ?
|
|
|
|
routeSortTypeToEnum(props.match.params.sort) :
|
|
|
|
SortType.Hot;
|
|
|
|
}
|
|
|
|
|
|
|
|
getPageFromProps(props: any): number {
|
|
|
|
return (props.match.params.page) ? Number(props.match.params.page) : 1;
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
constructor(props: any, context: any) {
|
2019-03-26 18:00:18 +00:00
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.state = this.emptyState;
|
|
|
|
|
|
|
|
this.subscription = WebSocketService.Instance.subject
|
2019-04-08 21:46:09 +00:00
|
|
|
.pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
|
|
|
|
.subscribe(
|
|
|
|
(msg) => this.parseMessage(msg),
|
2019-03-26 18:00:18 +00:00
|
|
|
(err) => console.error(err),
|
|
|
|
() => console.log('complete')
|
2019-04-08 21:46:09 +00:00
|
|
|
);
|
2019-03-26 18:00:18 +00:00
|
|
|
|
2019-04-25 21:52:18 +00:00
|
|
|
if (this.state.communityId) {
|
|
|
|
WebSocketService.Instance.getCommunity(this.state.communityId);
|
|
|
|
} else if (this.state.communityName) {
|
|
|
|
WebSocketService.Instance.getCommunityByName(this.state.communityName);
|
|
|
|
}
|
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
|
2019-04-29 00:19:04 +00:00
|
|
|
// Necessary for back button for some reason
|
|
|
|
componentWillReceiveProps(nextProps: any) {
|
|
|
|
if (nextProps.history.action == 'POP') {
|
|
|
|
this.state = this.emptyState;
|
|
|
|
this.state.sort = this.getSortTypeFromProps(nextProps);
|
|
|
|
this.state.page = this.getPageFromProps(nextProps);
|
|
|
|
this.fetchPosts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div class="container">
|
2019-04-08 21:46:09 +00:00
|
|
|
{this.state.loading ?
|
2019-04-20 04:06:25 +00:00
|
|
|
<h5><svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg></h5> :
|
2019-03-26 18:00:18 +00:00
|
|
|
<div class="row">
|
2019-05-06 16:26:21 +00:00
|
|
|
<div class="col-12 col-md-8">
|
2019-04-20 04:06:25 +00:00
|
|
|
<h5>{this.state.community.title}
|
2019-04-15 23:12:06 +00:00
|
|
|
{this.state.community.removed &&
|
|
|
|
<small className="ml-2 text-muted font-italic">removed</small>
|
|
|
|
}
|
2019-04-20 04:06:25 +00:00
|
|
|
</h5>
|
2019-04-29 00:19:04 +00:00
|
|
|
{this.selects()}
|
|
|
|
<PostListings posts={this.state.posts} />
|
|
|
|
{this.paginator()}
|
2019-03-26 18:00:18 +00:00
|
|
|
</div>
|
2019-05-06 16:26:21 +00:00
|
|
|
<div class="col-12 col-md-4">
|
2019-04-21 20:52:55 +00:00
|
|
|
<Sidebar
|
|
|
|
community={this.state.community}
|
|
|
|
moderators={this.state.moderators}
|
|
|
|
admins={this.state.admins}
|
|
|
|
/>
|
2019-04-03 06:49:32 +00:00
|
|
|
</div>
|
2019-03-26 18:00:18 +00:00
|
|
|
</div>
|
2019-04-08 21:46:09 +00:00
|
|
|
}
|
2019-03-26 18:00:18 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-04-29 00:19:04 +00:00
|
|
|
selects() {
|
|
|
|
return (
|
|
|
|
<div className="mb-2">
|
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">
|
2019-04-29 00:19:04 +00:00
|
|
|
<option disabled>Sort Type</option>
|
|
|
|
<option value={SortType.Hot}>Hot</option>
|
|
|
|
<option value={SortType.New}>New</option>
|
|
|
|
<option disabled>──────────</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>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
nextPage(i: Community) {
|
|
|
|
i.state.page++;
|
|
|
|
i.setState(i.state);
|
|
|
|
i.updateUrl();
|
|
|
|
i.fetchPosts();
|
|
|
|
}
|
|
|
|
|
|
|
|
prevPage(i: Community) {
|
|
|
|
i.state.page--;
|
|
|
|
i.setState(i.state);
|
|
|
|
i.updateUrl();
|
|
|
|
i.fetchPosts();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSortChange(i: Community, event: any) {
|
|
|
|
i.state.sort = Number(event.target.value);
|
|
|
|
i.state.page = 1;
|
|
|
|
i.setState(i.state);
|
|
|
|
i.updateUrl();
|
|
|
|
i.fetchPosts();
|
|
|
|
}
|
|
|
|
|
|
|
|
updateUrl() {
|
|
|
|
let sortStr = SortType[this.state.sort].toLowerCase();
|
2019-04-29 02:05:11 +00:00
|
|
|
this.props.history.push(`/c/${this.state.community.name}/sort/${sortStr}/page/${this.state.page}`);
|
2019-04-29 00:19:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fetchPosts() {
|
|
|
|
let getPostsForm: GetPostsForm = {
|
|
|
|
page: this.state.page,
|
|
|
|
limit: fetchLimit,
|
|
|
|
sort: SortType[this.state.sort],
|
|
|
|
type_: ListingType[ListingType.Community],
|
|
|
|
community_id: this.state.community.id,
|
|
|
|
}
|
|
|
|
WebSocketService.Instance.getPosts(getPostsForm);
|
|
|
|
}
|
2019-04-03 06:49:32 +00:00
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
parseMessage(msg: any) {
|
|
|
|
console.log(msg);
|
|
|
|
let op: UserOperation = msgOp(msg);
|
|
|
|
if (msg.error) {
|
|
|
|
alert(msg.error);
|
|
|
|
return;
|
|
|
|
} else if (op == UserOperation.GetCommunity) {
|
2019-04-04 22:29:14 +00:00
|
|
|
let res: GetCommunityResponse = msg;
|
2019-03-26 18:00:18 +00:00
|
|
|
this.state.community = res.community;
|
2019-04-04 20:53:32 +00:00
|
|
|
this.state.moderators = res.moderators;
|
2019-04-21 20:52:55 +00:00
|
|
|
this.state.admins = res.admins;
|
2019-04-29 02:05:11 +00:00
|
|
|
document.title = `/c/${this.state.community.name} - Lemmy`;
|
2019-03-26 18:00:18 +00:00
|
|
|
this.setState(this.state);
|
2019-04-29 00:19:04 +00:00
|
|
|
this.fetchPosts();
|
2019-04-04 22:29:14 +00:00
|
|
|
} else if (op == UserOperation.EditCommunity) {
|
|
|
|
let res: CommunityResponse = msg;
|
|
|
|
this.state.community = res.community;
|
|
|
|
this.setState(this.state);
|
2019-04-05 06:26:38 +00:00
|
|
|
} else if (op == UserOperation.FollowCommunity) {
|
|
|
|
let res: CommunityResponse = msg;
|
|
|
|
this.state.community.subscribed = res.community.subscribed;
|
|
|
|
this.state.community.number_of_subscribers = res.community.number_of_subscribers;
|
|
|
|
this.setState(this.state);
|
2019-04-29 00:19:04 +00:00
|
|
|
} else if (op == UserOperation.GetPosts) {
|
|
|
|
let res: GetPostsResponse = msg;
|
|
|
|
this.state.posts = res.posts;
|
|
|
|
this.state.loading = false;
|
2019-05-02 22:16:38 +00:00
|
|
|
window.scrollTo(0,0);
|
2019-04-29 00:19:04 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
} else if (op == UserOperation.CreatePostLike) {
|
|
|
|
let res: CreatePostLikeResponse = msg;
|
|
|
|
let found = this.state.posts.find(c => c.id == res.post.id);
|
|
|
|
found.my_vote = res.post.my_vote;
|
|
|
|
found.score = res.post.score;
|
|
|
|
found.upvotes = res.post.upvotes;
|
|
|
|
found.downvotes = res.post.downvotes;
|
|
|
|
this.setState(this.state);
|
2019-04-03 06:49:32 +00:00
|
|
|
}
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-03 06:49:32 +00:00
|
|
|
|