1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from 'rxjs';
3 import { retryWhen, delay, take } from 'rxjs/operators';
6 Community as CommunityI,
19 AddModToCommunityResponse,
20 BanFromCommunityResponse,
25 WebSocketJsonResponse,
26 } from '../interfaces';
27 import { WebSocketService } from '../services';
28 import { PostListings } from './post-listings';
29 import { CommentNodes } from './comment-nodes';
30 import { SortSelect } from './sort-select';
31 import { DataTypeSelect } from './data-type-select';
32 import { Sidebar } from './sidebar';
43 createPostLikeFindRes,
47 import { i18n } from '../i18next';
50 community: CommunityI;
52 communityName: string;
53 moderators: Array<CommunityUser>;
54 admins: Array<UserView>;
58 comments: Array<Comment>;
64 export class Community extends Component<any, State> {
65 private subscription: Subscription;
66 private emptyState: State = {
75 number_of_subscribers: null,
76 number_of_posts: null,
77 number_of_comments: null,
85 communityId: Number(this.props.match.params.id),
86 communityName: this.props.match.params.name,
91 dataType: getDataTypeFromProps(this.props),
92 sort: getSortTypeFromProps(this.props),
93 page: getPageFromProps(this.props),
96 constructor(props: any, context: any) {
97 super(props, context);
99 this.state = this.emptyState;
100 this.handleSortChange = this.handleSortChange.bind(this);
101 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
103 this.subscription = WebSocketService.Instance.subject
104 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
106 msg => this.parseMessage(msg),
107 err => console.error(err),
108 () => console.log('complete')
111 let form: GetCommunityForm = {
112 id: this.state.communityId ? this.state.communityId : null,
113 name: this.state.communityName ? this.state.communityName : null,
115 WebSocketService.Instance.getCommunity(form);
118 componentWillUnmount() {
119 this.subscription.unsubscribe();
122 // Necessary for back button for some reason
123 componentWillReceiveProps(nextProps: any) {
125 nextProps.history.action == 'POP' ||
126 nextProps.history.action == 'PUSH'
128 this.state.dataType = getDataTypeFromProps(nextProps);
129 this.state.sort = getSortTypeFromProps(nextProps);
130 this.state.page = getPageFromProps(nextProps);
131 this.setState(this.state);
138 <div class="container">
140 {this.state.loading ? (
142 <svg class="icon icon-spinner spin">
143 <use xlinkHref="#icon-spinner"></use>
148 <div class="col-12 col-md-8">
150 {this.state.community.title}
151 {this.state.community.removed && (
152 <small className="ml-2 text-muted font-italic">
156 {this.state.community.nsfw && (
157 <small className="ml-2 text-muted font-italic">
165 <div class="col-12 col-md-4">
167 community={this.state.community}
168 moderators={this.state.moderators}
169 admins={this.state.admins}
170 online={this.state.online}
180 return this.state.dataType == DataType.Post ? (
182 posts={this.state.posts}
184 sort={this.state.sort}
188 nodes={commentsToFlatNodes(this.state.comments)}
190 sortType={this.state.sort}
200 type_={this.state.dataType}
201 onChange={this.handleDataTypeChange}
205 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
208 href={`/feeds/c/${this.state.communityName}.xml?sort=${
209 SortType[this.state.sort]
214 <svg class="icon text-muted small">
215 <use xlinkHref="#icon-rss">#</use>
225 {this.state.page > 1 && (
227 class="btn btn-sm btn-secondary mr-1"
228 onClick={linkEvent(this, this.prevPage)}
233 {this.state.posts.length == fetchLimit && (
235 class="btn btn-sm btn-secondary"
236 onClick={linkEvent(this, this.nextPage)}
245 nextPage(i: Community) {
250 window.scrollTo(0, 0);
253 prevPage(i: Community) {
258 window.scrollTo(0, 0);
261 handleSortChange(val: SortType) {
262 this.state.sort = val;
264 this.state.loading = true;
265 this.setState(this.state);
268 window.scrollTo(0, 0);
271 handleDataTypeChange(val: DataType) {
272 this.state.dataType = val;
274 this.state.loading = true;
275 this.setState(this.state);
278 window.scrollTo(0, 0);
282 let dataTypeStr = DataType[this.state.dataType].toLowerCase();
283 let sortStr = SortType[this.state.sort].toLowerCase();
284 this.props.history.push(
285 `/c/${this.state.community.name}/data_type/${dataTypeStr}/sort/${sortStr}/page/${this.state.page}`
290 if (this.state.dataType == DataType.Post) {
291 let getPostsForm: GetPostsForm = {
292 page: this.state.page,
294 sort: SortType[this.state.sort],
295 type_: ListingType[ListingType.Community],
296 community_id: this.state.community.id,
298 WebSocketService.Instance.getPosts(getPostsForm);
300 let getCommentsForm: GetCommentsForm = {
301 page: this.state.page,
303 sort: SortType[this.state.sort],
304 type_: ListingType[ListingType.Community],
305 community_id: this.state.community.id,
307 WebSocketService.Instance.getComments(getCommentsForm);
311 parseMessage(msg: WebSocketJsonResponse) {
313 let res = wsJsonToRes(msg);
315 toast(i18n.t(msg.error), 'danger');
316 this.context.router.history.push('/');
318 } else if (msg.reconnect) {
320 } else if (res.op == UserOperation.GetCommunity) {
321 let data = res.data as GetCommunityResponse;
322 this.state.community = data.community;
323 this.state.moderators = data.moderators;
324 this.state.admins = data.admins;
325 this.state.online = data.online;
326 document.title = `/c/${this.state.community.name} - ${WebSocketService.Instance.site.name}`;
327 this.setState(this.state);
329 } else if (res.op == UserOperation.EditCommunity) {
330 let data = res.data as CommunityResponse;
331 this.state.community = data.community;
332 this.setState(this.state);
333 } else if (res.op == UserOperation.FollowCommunity) {
334 let data = res.data as CommunityResponse;
335 this.state.community.subscribed = data.community.subscribed;
336 this.state.community.number_of_subscribers =
337 data.community.number_of_subscribers;
338 this.setState(this.state);
339 } else if (res.op == UserOperation.GetPosts) {
340 let data = res.data as GetPostsResponse;
341 this.state.posts = data.posts;
342 this.state.loading = false;
343 this.setState(this.state);
344 } else if (res.op == UserOperation.EditPost) {
345 let data = res.data as PostResponse;
346 editPostFindRes(data, this.state.posts);
347 this.setState(this.state);
348 } else if (res.op == UserOperation.CreatePost) {
349 let data = res.data as PostResponse;
350 this.state.posts.unshift(data.post);
351 this.setState(this.state);
352 } else if (res.op == UserOperation.CreatePostLike) {
353 let data = res.data as PostResponse;
354 createPostLikeFindRes(data, this.state.posts);
355 this.setState(this.state);
356 } else if (res.op == UserOperation.AddModToCommunity) {
357 let data = res.data as AddModToCommunityResponse;
358 this.state.moderators = data.moderators;
359 this.setState(this.state);
360 } else if (res.op == UserOperation.BanFromCommunity) {
361 let data = res.data as BanFromCommunityResponse;
364 .filter(p => p.creator_id == data.user.id)
365 .forEach(p => (p.banned = data.banned));
367 this.setState(this.state);
368 } else if (res.op == UserOperation.GetComments) {
369 let data = res.data as GetCommentsResponse;
370 this.state.comments = data.comments;
371 this.state.loading = false;
372 this.setState(this.state);
373 } else if (res.op == UserOperation.EditComment) {
374 let data = res.data as CommentResponse;
375 editCommentRes(data, this.state.comments);
376 this.setState(this.state);
377 } else if (res.op == UserOperation.CreateComment) {
378 let data = res.data as CommentResponse;
380 // Necessary since it might be a user reply
381 if (data.recipient_ids.length == 0) {
382 this.state.comments.unshift(data.comment);
383 this.setState(this.state);
385 } else if (res.op == UserOperation.SaveComment) {
386 let data = res.data as CommentResponse;
387 saveCommentRes(data, this.state.comments);
388 this.setState(this.state);
389 } else if (res.op == UserOperation.CreateCommentLike) {
390 let data = res.data as CommentResponse;
391 createCommentLikeRes(data, this.state.comments);
392 this.setState(this.state);