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-10-21 04:21:54 +00:00
|
|
|
import { WebSocketService, UserService } from '../services';
|
2019-04-05 19:14:54 +00:00
|
|
|
import { PostListings } from './post-listings';
|
2019-10-21 00:49:13 +00:00
|
|
|
import { SortSelect } from './sort-select';
|
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,
|
2020-01-23 03:29:11 +00:00
|
|
|
toast,
|
2019-10-19 00:20:27 +00:00
|
|
|
} from '../utils';
|
2019-11-23 00:18:10 +00:00
|
|
|
import { T } from 'inferno-i18next';
|
|
|
|
import { i18n } from '../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)
|
2019-10-21 04:21:54 +00:00
|
|
|
: UserService.Instance.user
|
|
|
|
? UserService.Instance.user.default_sort_type
|
2019-10-19 00:20:27 +00:00
|
|
|
: 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;
|
2019-10-21 00:49:13 +00:00
|
|
|
this.handleSortChange = this.handleSortChange.bind(this);
|
2019-03-26 18:00:18 +00:00
|
|
|
|
|
|
|
this.subscription = WebSocketService.Instance.subject
|
2020-01-15 22:28:59 +00:00
|
|
|
.pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
|
2019-10-19 00:20:27 +00:00
|
|
|
.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) {
|
2019-10-21 00:49:13 +00:00
|
|
|
if (
|
|
|
|
nextProps.history.action == 'POP' ||
|
|
|
|
nextProps.history.action == 'PUSH'
|
|
|
|
) {
|
2019-04-29 00:19:04 +00:00
|
|
|
this.state.sort = this.getSortTypeFromProps(nextProps);
|
|
|
|
this.state.page = this.getPageFromProps(nextProps);
|
2019-10-21 00:49:13 +00:00
|
|
|
this.setState(this.state);
|
2019-04-29 00:19:04 +00:00
|
|
|
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()}
|
2020-01-20 00:48:34 +00:00
|
|
|
<PostListings posts={this.state.posts} />
|
2019-10-19 00:20:27 +00:00
|
|
|
{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 (
|
2019-10-21 00:49:13 +00:00
|
|
|
<div class="mb-2">
|
|
|
|
<SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
|
2019-12-02 01:21:19 +00:00
|
|
|
<a
|
2019-12-02 06:20:01 +00:00
|
|
|
href={`/feeds/c/${this.state.communityName}.xml?sort=${
|
|
|
|
SortType[this.state.sort]
|
|
|
|
}`}
|
|
|
|
target="_blank"
|
2019-12-02 01:21:19 +00:00
|
|
|
>
|
|
|
|
<svg class="icon mx-2 text-muted small">
|
|
|
|
<use xlinkHref="#icon-rss">#</use>
|
|
|
|
</svg>
|
|
|
|
</a>
|
2019-04-29 00:19:04 +00:00
|
|
|
</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>
|
|
|
|
)}
|
2020-01-19 21:47:54 +00:00
|
|
|
{this.state.posts.length == fetchLimit && (
|
|
|
|
<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
|
|
|
}
|
|
|
|
|
2019-10-21 00:49:13 +00:00
|
|
|
handleSortChange(val: SortType) {
|
|
|
|
this.state.sort = val;
|
|
|
|
this.state.page = 1;
|
|
|
|
this.state.loading = true;
|
|
|
|
this.setState(this.state);
|
|
|
|
this.updateUrl();
|
|
|
|
this.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) {
|
2020-01-23 03:29:11 +00:00
|
|
|
toast(i18n.t(msg.error), 'danger');
|
2019-11-23 00:18:10 +00:00
|
|
|
this.context.router.history.push('/');
|
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
|
|
|
}
|
|
|
|
}
|