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}
202 type_={this.state.dataType}
203 onChange={this.handleDataTypeChange}
207 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
210 href={`/feeds/c/${this.state.communityName}.xml?sort=${
211 SortType[this.state.sort]
216 <svg class="icon text-muted small">
217 <use xlinkHref="#icon-rss">#</use>
227 {this.state.page > 1 && (
229 class="btn btn-sm btn-secondary mr-1"
230 onClick={linkEvent(this, this.prevPage)}
235 {this.state.posts.length == fetchLimit && (
237 class="btn btn-sm btn-secondary"
238 onClick={linkEvent(this, this.nextPage)}
247 nextPage(i: Community) {
252 window.scrollTo(0, 0);
255 prevPage(i: Community) {
260 window.scrollTo(0, 0);
263 handleSortChange(val: SortType) {
264 this.state.sort = val;
266 this.state.loading = true;
267 this.setState(this.state);
270 window.scrollTo(0, 0);
273 handleDataTypeChange(val: DataType) {
274 this.state.dataType = val;
276 this.state.loading = true;
277 this.setState(this.state);
280 window.scrollTo(0, 0);
284 let dataTypeStr = DataType[this.state.dataType].toLowerCase();
285 let sortStr = SortType[this.state.sort].toLowerCase();
286 this.props.history.push(
287 `/c/${this.state.community.name}/data_type/${dataTypeStr}/sort/${sortStr}/page/${this.state.page}`
292 if (this.state.dataType == DataType.Post) {
293 let getPostsForm: GetPostsForm = {
294 page: this.state.page,
296 sort: SortType[this.state.sort],
297 type_: ListingType[ListingType.Community],
298 community_id: this.state.community.id,
300 WebSocketService.Instance.getPosts(getPostsForm);
302 let getCommentsForm: GetCommentsForm = {
303 page: this.state.page,
305 sort: SortType[this.state.sort],
306 type_: ListingType[ListingType.Community],
307 community_id: this.state.community.id,
309 WebSocketService.Instance.getComments(getCommentsForm);
313 parseMessage(msg: WebSocketJsonResponse) {
315 let res = wsJsonToRes(msg);
317 toast(i18n.t(msg.error), 'danger');
318 this.context.router.history.push('/');
320 } else if (msg.reconnect) {
322 } else if (res.op == UserOperation.GetCommunity) {
323 let data = res.data as GetCommunityResponse;
324 this.state.community = data.community;
325 this.state.moderators = data.moderators;
326 this.state.admins = data.admins;
327 this.state.online = data.online;
328 document.title = `/c/${this.state.community.name} - ${WebSocketService.Instance.site.name}`;
329 this.setState(this.state);
331 } else if (res.op == UserOperation.EditCommunity) {
332 let data = res.data as CommunityResponse;
333 this.state.community = data.community;
334 this.setState(this.state);
335 } else if (res.op == UserOperation.FollowCommunity) {
336 let data = res.data as CommunityResponse;
337 this.state.community.subscribed = data.community.subscribed;
338 this.state.community.number_of_subscribers =
339 data.community.number_of_subscribers;
340 this.setState(this.state);
341 } else if (res.op == UserOperation.GetPosts) {
342 let data = res.data as GetPostsResponse;
343 this.state.posts = data.posts;
344 this.state.loading = false;
345 this.setState(this.state);
347 } else if (res.op == UserOperation.EditPost) {
348 let data = res.data as PostResponse;
349 editPostFindRes(data, this.state.posts);
350 this.setState(this.state);
351 } else if (res.op == UserOperation.CreatePost) {
352 let data = res.data as PostResponse;
353 this.state.posts.unshift(data.post);
354 this.setState(this.state);
355 } else if (res.op == UserOperation.CreatePostLike) {
356 let data = res.data as PostResponse;
357 createPostLikeFindRes(data, this.state.posts);
358 this.setState(this.state);
359 } else if (res.op == UserOperation.AddModToCommunity) {
360 let data = res.data as AddModToCommunityResponse;
361 this.state.moderators = data.moderators;
362 this.setState(this.state);
363 } else if (res.op == UserOperation.BanFromCommunity) {
364 let data = res.data as BanFromCommunityResponse;
367 .filter(p => p.creator_id == data.user.id)
368 .forEach(p => (p.banned = data.banned));
370 this.setState(this.state);
371 } else if (res.op == UserOperation.GetComments) {
372 let data = res.data as GetCommentsResponse;
373 this.state.comments = data.comments;
374 this.state.loading = false;
375 this.setState(this.state);
376 } else if (res.op == UserOperation.EditComment) {
377 let data = res.data as CommentResponse;
378 editCommentRes(data, this.state.comments);
379 this.setState(this.state);
380 } else if (res.op == UserOperation.CreateComment) {
381 let data = res.data as CommentResponse;
383 // Necessary since it might be a user reply
384 if (data.recipient_ids.length == 0) {
385 this.state.comments.unshift(data.comment);
386 this.setState(this.state);
388 } else if (res.op == UserOperation.SaveComment) {
389 let data = res.data as CommentResponse;
390 saveCommentRes(data, this.state.comments);
391 this.setState(this.state);
392 } else if (res.op == UserOperation.CreateCommentLike) {
393 let data = res.data as CommentResponse;
394 createCommentLikeRes(data, this.state.comments);
395 this.setState(this.state);