2019-04-29 00:19:04 +00:00
|
|
|
import { Component, linkEvent } from 'inferno';
|
2019-10-19 00:20:27 +00:00
|
|
|
import { Subscription } from 'rxjs';
|
2019-03-26 18:00:18 +00:00
|
|
|
import { retryWhen, delay, take } from 'rxjs/operators';
|
2019-10-19 00:20:27 +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-10-19 00:20:27 +00:00
|
|
|
import {
|
|
|
|
msgOp,
|
|
|
|
routeSortTypeToEnum,
|
|
|
|
fetchLimit,
|
|
|
|
postRefetchSeconds,
|
|
|
|
} from '../utils';
|
2019-08-20 02:37:32 +00:00
|
|
|
import { T, i18n } from 'inferno-i18next';
|
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;
|
2019-10-13 19:37:10 +00:00
|
|
|
private postFetcher: any;
|
2019-03-26 18:00:18 +00:00
|
|
|
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-08-14 02:52:43 +00:00
|
|
|
nsfw: false,
|
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),
|
2019-10-19 00:20:27 +00:00
|
|
|
};
|
2019-04-29 00:19:04 +00:00
|
|
|
|
|
|
|
getSortTypeFromProps(props: any): SortType {
|
2019-10-19 00:20:27 +00:00
|
|
|
return props.match.params.sort
|
|
|
|
? routeSortTypeToEnum(props.match.params.sort)
|
|
|
|
: SortType.Hot;
|
2019-04-29 00:19:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getPageFromProps(props: any): number {
|
2019-10-19 00:20:27 +00:00
|
|
|
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-10-19 00:20:27 +00:00
|
|
|
.pipe(
|
|
|
|
retryWhen(errors =>
|
|
|
|
errors.pipe(
|
|
|
|
delay(3000),
|
|
|
|
take(10)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.subscribe(
|
|
|
|
msg => this.parseMessage(msg),
|
|
|
|
err => console.error(err),
|
2019-03-26 18:00:18 +00:00
|
|
|
() => console.log('complete')
|
2019-10-19 00:20:27 +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-10-13 19:37:10 +00:00
|
|
|
clearInterval(this.postFetcher);
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
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-10-19 00:20:27 +00:00
|
|
|
{this.state.loading ? (
|
|
|
|
<h5>
|
|
|
|
<svg class="icon icon-spinner spin">
|
|
|
|
<use xlinkHref="#icon-spinner"></use>
|
|
|
|
</svg>
|
2019-04-20 04:06:25 +00:00
|
|
|
</h5>
|
2019-10-19 00:20:27 +00:00
|
|
|
) : (
|
|
|
|
<div class="row">
|
|
|
|
<div class="col-12 col-md-8">
|
|
|
|
<h5>
|
|
|
|
{this.state.community.title}
|
|
|
|
{this.state.community.removed && (
|
|
|
|
<small className="ml-2 text-muted font-italic">
|
|
|
|
<T i18nKey="removed">#</T>
|
|
|
|
</small>
|
|
|
|
)}
|
|
|
|
{this.state.community.nsfw && (
|
|
|
|
<small className="ml-2 text-muted font-italic">
|
|
|
|
<T i18nKey="nsfw">#</T>
|
|
|
|
</small>
|
|
|
|
)}
|
|
|
|
</h5>
|
|
|
|
{this.selects()}
|
|
|
|
<PostListings posts={this.state.posts} />
|
|
|
|
{this.paginator()}
|
|
|
|
</div>
|
|
|
|
<div class="col-12 col-md-4">
|
|
|
|
<Sidebar
|
|
|
|
community={this.state.community}
|
|
|
|
moderators={this.state.moderators}
|
|
|
|
admins={this.state.admins}
|
|
|
|
/>
|
|
|
|
</div>
|
2019-04-03 06:49:32 +00:00
|
|
|
</div>
|
2019-10-19 00:20:27 +00:00
|
|
|
)}
|
2019-03-26 18:00:18 +00:00
|
|
|
</div>
|
2019-10-19 00:20:27 +00:00
|
|
|
);
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-29 00:19:04 +00:00
|
|
|
selects() {
|
|
|
|
return (
|
|
|
|
<div className="mb-2">
|
2019-10-19 00:20:27 +00:00
|
|
|
<select
|
|
|
|
value={this.state.sort}
|
|
|
|
onChange={linkEvent(this, this.handleSortChange)}
|
|
|
|
class="custom-select custom-select-sm w-auto"
|
|
|
|
>
|
|
|
|
<option disabled>
|
|
|
|
<T i18nKey="sort_type">#</T>
|
|
|
|
</option>
|
|
|
|
<option value={SortType.Hot}>
|
|
|
|
<T i18nKey="hot">#</T>
|
|
|
|
</option>
|
|
|
|
<option value={SortType.New}>
|
|
|
|
<T i18nKey="new">#</T>
|
|
|
|
</option>
|
2019-08-20 02:37:32 +00:00
|
|
|
<option disabled>──────</option>
|
2019-10-19 00:20:27 +00:00
|
|
|
<option value={SortType.TopDay}>
|
|
|
|
<T i18nKey="top_day">#</T>
|
|
|
|
</option>
|
|
|
|
<option value={SortType.TopWeek}>
|
|
|
|
<T i18nKey="week">#</T>
|
|
|
|
</option>
|
|
|
|
<option value={SortType.TopMonth}>
|
|
|
|
<T i18nKey="month">#</T>
|
|
|
|
</option>
|
|
|
|
<option value={SortType.TopYear}>
|
|
|
|
<T i18nKey="year">#</T>
|
|
|
|
</option>
|
|
|
|
<option value={SortType.TopAll}>
|
|
|
|
<T i18nKey="all">#</T>
|
|
|
|
</option>
|
2019-04-29 00:19:04 +00:00
|
|
|
</select>
|
|
|
|
</div>
|
2019-10-19 00:20:27 +00:00
|
|
|
);
|
2019-04-29 00:19:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
paginator() {
|
|
|
|
return (
|
2019-08-20 21:40:51 +00:00
|
|
|
<div class="my-2">
|
2019-10-19 00:20:27 +00:00
|
|
|
{this.state.page > 1 && (
|
|
|
|
<button
|
|
|
|
class="btn btn-sm btn-secondary mr-1"
|
|
|
|
onClick={linkEvent(this, this.prevPage)}
|
|
|
|
>
|
|
|
|
<T i18nKey="prev">#</T>
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
<button
|
|
|
|
class="btn btn-sm btn-secondary"
|
|
|
|
onClick={linkEvent(this, this.nextPage)}
|
|
|
|
>
|
|
|
|
<T i18nKey="next">#</T>
|
|
|
|
</button>
|
2019-04-29 00:19:04 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-19 00:20:27 +00:00
|
|
|
nextPage(i: Community) {
|
2019-04-29 00:19:04 +00:00
|
|
|
i.state.page++;
|
|
|
|
i.setState(i.state);
|
|
|
|
i.updateUrl();
|
|
|
|
i.fetchPosts();
|
2019-10-19 00:20:27 +00:00
|
|
|
window.scrollTo(0, 0);
|
2019-04-29 00:19:04 +00:00
|
|
|
}
|
|
|
|
|
2019-10-19 00:20:27 +00:00
|
|
|
prevPage(i: Community) {
|
2019-04-29 00:19:04 +00:00
|
|
|
i.state.page--;
|
|
|
|
i.setState(i.state);
|
|
|
|
i.updateUrl();
|
|
|
|
i.fetchPosts();
|
2019-10-19 00:20:27 +00:00
|
|
|
window.scrollTo(0, 0);
|
2019-04-29 00:19:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleSortChange(i: Community, event: any) {
|
|
|
|
i.state.sort = Number(event.target.value);
|
|
|
|
i.state.page = 1;
|
|
|
|
i.setState(i.state);
|
|
|
|
i.updateUrl();
|
|
|
|
i.fetchPosts();
|
2019-10-19 00:20:27 +00:00
|
|
|
window.scrollTo(0, 0);
|
2019-04-29 00:19:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
updateUrl() {
|
|
|
|
let sortStr = SortType[this.state.sort].toLowerCase();
|
2019-10-19 00:20:27 +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
|
|
|
}
|
|
|
|
|
2019-10-13 19:37:10 +00:00
|
|
|
keepFetchingPosts() {
|
|
|
|
this.fetchPosts();
|
|
|
|
this.postFetcher = setInterval(() => this.fetchPosts(), postRefetchSeconds);
|
|
|
|
}
|
|
|
|
|
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,
|
2019-10-19 00:20:27 +00:00
|
|
|
};
|
2019-04-29 00:19:04 +00:00
|
|
|
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) {
|
2019-08-10 00:14:43 +00:00
|
|
|
alert(i18n.t(msg.error));
|
2019-03-26 18:00:18 +00:00
|
|
|
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-06-03 01:35:46 +00:00
|
|
|
document.title = `/c/${this.state.community.name} - ${WebSocketService.Instance.site.name}`;
|
2019-03-26 18:00:18 +00:00
|
|
|
this.setState(this.state);
|
2019-10-18 22:05:50 +00:00
|
|
|
this.keepFetchingPosts();
|
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;
|
2019-10-19 00:20:27 +00:00
|
|
|
this.state.community.number_of_subscribers =
|
|
|
|
res.community.number_of_subscribers;
|
2019-04-05 06:26:38 +00:00
|
|
|
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;
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|