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,
|
2020-01-28 02:04:30 +00:00
|
|
|
GetCommunityForm,
|
2019-10-19 00:20:27 +00:00
|
|
|
ListingType,
|
|
|
|
GetPostsResponse,
|
|
|
|
CreatePostLikeResponse,
|
2020-01-19 05:38:45 +00:00
|
|
|
WebSocketJsonResponse,
|
2019-10-19 00:20:27 +00:00
|
|
|
} 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 {
|
2020-01-19 04:54:10 +00:00
|
|
|
wsJsonToRes,
|
2019-10-19 00:20:27 +00:00
|
|
|
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
|
|
|
|
2020-01-28 02:04:30 +00:00
|
|
|
let form: GetCommunityForm = {
|
|
|
|
id: this.state.communityId ? this.state.communityId : null,
|
|
|
|
name: this.state.communityName ? this.state.communityName : null,
|
|
|
|
};
|
|
|
|
WebSocketService.Instance.getCommunity(form);
|
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
|
|
|
|
2020-01-19 05:38:45 +00:00
|
|
|
parseMessage(msg: WebSocketJsonResponse) {
|
2019-03-26 18:00:18 +00:00
|
|
|
console.log(msg);
|
2020-01-19 04:54:10 +00:00
|
|
|
let res = wsJsonToRes(msg);
|
2020-01-25 14:58:53 +00:00
|
|
|
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;
|
2020-01-19 04:54:10 +00:00
|
|
|
} else if (res.op == UserOperation.GetCommunity) {
|
|
|
|
let data = res.data as GetCommunityResponse;
|
|
|
|
this.state.community = data.community;
|
|
|
|
this.state.moderators = data.moderators;
|
|
|
|
this.state.admins = data.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();
|
2020-01-19 04:54:10 +00:00
|
|
|
} else if (res.op == UserOperation.EditCommunity) {
|
|
|
|
let data = res.data as CommunityResponse;
|
|
|
|
this.state.community = data.community;
|
2019-04-04 22:29:14 +00:00
|
|
|
this.setState(this.state);
|
2020-01-19 04:54:10 +00:00
|
|
|
} else if (res.op == UserOperation.FollowCommunity) {
|
|
|
|
let data = res.data as CommunityResponse;
|
|
|
|
this.state.community.subscribed = data.community.subscribed;
|
2019-10-19 00:20:27 +00:00
|
|
|
this.state.community.number_of_subscribers =
|
2020-01-19 04:54:10 +00:00
|
|
|
data.community.number_of_subscribers;
|
2019-04-05 06:26:38 +00:00
|
|
|
this.setState(this.state);
|
2020-01-19 04:54:10 +00:00
|
|
|
} else if (res.op == UserOperation.GetPosts) {
|
|
|
|
let data = res.data as GetPostsResponse;
|
|
|
|
this.state.posts = data.posts;
|
2019-04-29 00:19:04 +00:00
|
|
|
this.state.loading = false;
|
|
|
|
this.setState(this.state);
|
2020-01-19 04:54:10 +00:00
|
|
|
} else if (res.op == UserOperation.CreatePostLike) {
|
|
|
|
let data = res.data as CreatePostLikeResponse;
|
|
|
|
let found = this.state.posts.find(c => c.id == data.post.id);
|
|
|
|
found.my_vote = data.post.my_vote;
|
|
|
|
found.score = data.post.score;
|
|
|
|
found.upvotes = data.post.upvotes;
|
|
|
|
found.downvotes = data.post.downvotes;
|
2019-04-29 00:19:04 +00:00
|
|
|
this.setState(this.state);
|
2019-04-03 06:49:32 +00:00
|
|
|
}
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
}
|