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,
48 import { i18n } from '../i18next';
51 community: CommunityI;
53 communityName: string;
54 moderators: Array<CommunityUser>;
55 admins: Array<UserView>;
59 comments: Array<Comment>;
65 export class Community extends Component<any, State> {
66 private subscription: Subscription;
67 private emptyState: State = {
76 number_of_subscribers: null,
77 number_of_posts: null,
78 number_of_comments: null,
86 communityId: Number(this.props.match.params.id),
87 communityName: this.props.match.params.name,
92 dataType: getDataTypeFromProps(this.props),
93 sort: getSortTypeFromProps(this.props),
94 page: getPageFromProps(this.props),
97 constructor(props: any, context: any) {
98 super(props, context);
100 this.state = this.emptyState;
101 this.handleSortChange = this.handleSortChange.bind(this);
102 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
104 this.subscription = WebSocketService.Instance.subject
105 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
107 msg => this.parseMessage(msg),
108 err => console.error(err),
109 () => console.log('complete')
112 let form: GetCommunityForm = {
113 id: this.state.communityId ? this.state.communityId : null,
114 name: this.state.communityName ? this.state.communityName : null,
116 WebSocketService.Instance.getCommunity(form);
119 componentWillUnmount() {
120 this.subscription.unsubscribe();
123 // Necessary for back button for some reason
124 componentWillReceiveProps(nextProps: any) {
126 nextProps.history.action == 'POP' ||
127 nextProps.history.action == 'PUSH'
129 this.state.dataType = getDataTypeFromProps(nextProps);
130 this.state.sort = getSortTypeFromProps(nextProps);
131 this.state.page = getPageFromProps(nextProps);
132 this.setState(this.state);
139 <div class="container">
141 {this.state.loading ? (
143 <svg class="icon icon-spinner spin">
144 <use xlinkHref="#icon-spinner"></use>
149 <div class="col-12 col-md-8">
151 {this.state.community.title}
152 {this.state.community.removed && (
153 <small className="ml-2 text-muted font-italic">
157 {this.state.community.nsfw && (
158 <small className="ml-2 text-muted font-italic">
166 <div class="col-12 col-md-4">
168 community={this.state.community}
169 moderators={this.state.moderators}
170 admins={this.state.admins}
171 online={this.state.online}
181 return this.state.dataType == DataType.Post ? (
183 posts={this.state.posts}
185 sort={this.state.sort}
189 nodes={commentsToFlatNodes(this.state.comments)}
191 sortType={this.state.sort}
201 type_={this.state.dataType}
202 onChange={this.handleDataTypeChange}
206 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
209 href={`/feeds/c/${this.state.communityName}.xml?sort=${
210 SortType[this.state.sort]
215 <svg class="icon text-muted small">
216 <use xlinkHref="#icon-rss">#</use>
226 {this.state.page > 1 && (
228 class="btn btn-sm btn-secondary mr-1"
229 onClick={linkEvent(this, this.prevPage)}
234 {this.state.posts.length == fetchLimit && (
236 class="btn btn-sm btn-secondary"
237 onClick={linkEvent(this, this.nextPage)}
246 nextPage(i: Community) {
251 window.scrollTo(0, 0);
254 prevPage(i: Community) {
259 window.scrollTo(0, 0);
262 handleSortChange(val: SortType) {
263 this.state.sort = val;
265 this.state.loading = true;
266 this.setState(this.state);
269 window.scrollTo(0, 0);
272 handleDataTypeChange(val: DataType) {
273 this.state.dataType = val;
275 this.state.loading = true;
276 this.setState(this.state);
279 window.scrollTo(0, 0);
283 let dataTypeStr = DataType[this.state.dataType].toLowerCase();
284 let sortStr = SortType[this.state.sort].toLowerCase();
285 this.props.history.push(
286 `/c/${this.state.community.name}/data_type/${dataTypeStr}/sort/${sortStr}/page/${this.state.page}`
291 if (this.state.dataType == DataType.Post) {
292 let getPostsForm: GetPostsForm = {
293 page: this.state.page,
295 sort: SortType[this.state.sort],
296 type_: ListingType[ListingType.Community],
297 community_id: this.state.community.id,
299 WebSocketService.Instance.getPosts(getPostsForm);
301 let getCommentsForm: GetCommentsForm = {
302 page: this.state.page,
304 sort: SortType[this.state.sort],
305 type_: ListingType[ListingType.Community],
306 community_id: this.state.community.id,
308 WebSocketService.Instance.getComments(getCommentsForm);
312 parseMessage(msg: WebSocketJsonResponse) {
314 let res = wsJsonToRes(msg);
316 toast(i18n.t(msg.error), 'danger');
317 this.context.router.history.push('/');
319 } else if (msg.reconnect) {
321 } else if (res.op == UserOperation.GetCommunity) {
322 let data = res.data as GetCommunityResponse;
323 this.state.community = data.community;
324 this.state.moderators = data.moderators;
325 this.state.admins = data.admins;
326 this.state.online = data.online;
327 document.title = `/c/${this.state.community.name} - ${WebSocketService.Instance.site.name}`;
328 this.setState(this.state);
330 } else if (res.op == UserOperation.EditCommunity) {
331 let data = res.data as CommunityResponse;
332 this.state.community = data.community;
333 this.setState(this.state);
334 } else if (res.op == UserOperation.FollowCommunity) {
335 let data = res.data as CommunityResponse;
336 this.state.community.subscribed = data.community.subscribed;
337 this.state.community.number_of_subscribers =
338 data.community.number_of_subscribers;
339 this.setState(this.state);
340 } else if (res.op == UserOperation.GetPosts) {
341 let data = res.data as GetPostsResponse;
342 this.state.posts = data.posts;
343 this.state.loading = false;
344 this.setState(this.state);
346 } else if (res.op == UserOperation.EditPost) {
347 let data = res.data as PostResponse;
348 editPostFindRes(data, this.state.posts);
349 this.setState(this.state);
350 } else if (res.op == UserOperation.CreatePost) {
351 let data = res.data as PostResponse;
352 this.state.posts.unshift(data.post);
353 this.setState(this.state);
354 } else if (res.op == UserOperation.CreatePostLike) {
355 let data = res.data as PostResponse;
356 createPostLikeFindRes(data, this.state.posts);
357 this.setState(this.state);
358 } else if (res.op == UserOperation.AddModToCommunity) {
359 let data = res.data as AddModToCommunityResponse;
360 this.state.moderators = data.moderators;
361 this.setState(this.state);
362 } else if (res.op == UserOperation.BanFromCommunity) {
363 let data = res.data as BanFromCommunityResponse;
366 .filter(p => p.creator_id == data.user.id)
367 .forEach(p => (p.banned = data.banned));
369 this.setState(this.state);
370 } else if (res.op == UserOperation.GetComments) {
371 let data = res.data as GetCommentsResponse;
372 this.state.comments = data.comments;
373 this.state.loading = false;
374 this.setState(this.state);
375 } else if (res.op == UserOperation.EditComment) {
376 let data = res.data as CommentResponse;
377 editCommentRes(data, this.state.comments);
378 this.setState(this.state);
379 } else if (res.op == UserOperation.CreateComment) {
380 let data = res.data as CommentResponse;
382 // Necessary since it might be a user reply
383 if (data.recipient_ids.length == 0) {
384 this.state.comments.unshift(data.comment);
385 this.setState(this.state);
387 } else if (res.op == UserOperation.SaveComment) {
388 let data = res.data as CommentResponse;
389 saveCommentRes(data, this.state.comments);
390 this.setState(this.state);
391 } else if (res.op == UserOperation.CreateCommentLike) {
392 let data = res.data as CommentResponse;
393 createCommentLikeRes(data, this.state.comments);
394 this.setState(this.state);