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">
139 {this.state.loading ? (
141 <svg class="icon icon-spinner spin">
142 <use xlinkHref="#icon-spinner"></use>
147 <div class="col-12 col-md-8">
149 {this.state.community.title}
150 {this.state.community.removed && (
151 <small className="ml-2 text-muted font-italic">
155 {this.state.community.nsfw && (
156 <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 ? (
181 <PostListings posts={this.state.posts} removeDuplicates />
183 <CommentNodes nodes={commentsToFlatNodes(this.state.comments)} noIndent />
191 type_={this.state.dataType}
192 onChange={this.handleDataTypeChange}
196 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
199 href={`/feeds/c/${this.state.communityName}.xml?sort=${
200 SortType[this.state.sort]
204 <svg class="icon mx-2 text-muted small">
205 <use xlinkHref="#icon-rss">#</use>
215 {this.state.page > 1 && (
217 class="btn btn-sm btn-secondary mr-1"
218 onClick={linkEvent(this, this.prevPage)}
223 {this.state.posts.length == fetchLimit && (
225 class="btn btn-sm btn-secondary"
226 onClick={linkEvent(this, this.nextPage)}
235 nextPage(i: Community) {
240 window.scrollTo(0, 0);
243 prevPage(i: Community) {
248 window.scrollTo(0, 0);
251 handleSortChange(val: SortType) {
252 this.state.sort = val;
254 this.state.loading = true;
255 this.setState(this.state);
258 window.scrollTo(0, 0);
261 handleDataTypeChange(val: DataType) {
262 this.state.dataType = val;
264 this.state.loading = true;
265 this.setState(this.state);
268 window.scrollTo(0, 0);
272 let dataTypeStr = DataType[this.state.dataType].toLowerCase();
273 let sortStr = SortType[this.state.sort].toLowerCase();
274 this.props.history.push(
275 `/c/${this.state.community.name}/data_type/${dataTypeStr}/sort/${sortStr}/page/${this.state.page}`
280 if (this.state.dataType == DataType.Post) {
281 let getPostsForm: GetPostsForm = {
282 page: this.state.page,
284 sort: SortType[this.state.sort],
285 type_: ListingType[ListingType.Community],
286 community_id: this.state.community.id,
288 WebSocketService.Instance.getPosts(getPostsForm);
290 let getCommentsForm: GetCommentsForm = {
291 page: this.state.page,
293 sort: SortType[this.state.sort],
294 type_: ListingType[ListingType.Community],
295 community_id: this.state.community.id,
297 WebSocketService.Instance.getComments(getCommentsForm);
301 parseMessage(msg: WebSocketJsonResponse) {
303 let res = wsJsonToRes(msg);
305 toast(i18n.t(msg.error), 'danger');
306 this.context.router.history.push('/');
308 } else if (msg.reconnect) {
310 } else if (res.op == UserOperation.GetCommunity) {
311 let data = res.data as GetCommunityResponse;
312 this.state.community = data.community;
313 this.state.moderators = data.moderators;
314 this.state.admins = data.admins;
315 this.state.online = data.online;
316 document.title = `/c/${this.state.community.name} - ${WebSocketService.Instance.site.name}`;
317 this.setState(this.state);
319 } else if (res.op == UserOperation.EditCommunity) {
320 let data = res.data as CommunityResponse;
321 this.state.community = data.community;
322 this.setState(this.state);
323 } else if (res.op == UserOperation.FollowCommunity) {
324 let data = res.data as CommunityResponse;
325 this.state.community.subscribed = data.community.subscribed;
326 this.state.community.number_of_subscribers =
327 data.community.number_of_subscribers;
328 this.setState(this.state);
329 } else if (res.op == UserOperation.GetPosts) {
330 let data = res.data as GetPostsResponse;
331 this.state.posts = data.posts;
332 this.state.loading = false;
333 this.setState(this.state);
334 } else if (res.op == UserOperation.EditPost) {
335 let data = res.data as PostResponse;
336 editPostFindRes(data, this.state.posts);
337 this.setState(this.state);
338 } else if (res.op == UserOperation.CreatePost) {
339 let data = res.data as PostResponse;
340 this.state.posts.unshift(data.post);
341 this.setState(this.state);
342 } else if (res.op == UserOperation.CreatePostLike) {
343 let data = res.data as PostResponse;
344 createPostLikeFindRes(data, this.state.posts);
345 this.setState(this.state);
346 } else if (res.op == UserOperation.AddModToCommunity) {
347 let data = res.data as AddModToCommunityResponse;
348 this.state.moderators = data.moderators;
349 this.setState(this.state);
350 } else if (res.op == UserOperation.BanFromCommunity) {
351 let data = res.data as BanFromCommunityResponse;
354 .filter(p => p.creator_id == data.user.id)
355 .forEach(p => (p.banned = data.banned));
357 this.setState(this.state);
358 } else if (res.op == UserOperation.GetComments) {
359 let data = res.data as GetCommentsResponse;
360 this.state.comments = data.comments;
361 this.state.loading = false;
362 this.setState(this.state);
363 } else if (res.op == UserOperation.EditComment) {
364 let data = res.data as CommentResponse;
365 editCommentRes(data, this.state.comments);
366 this.setState(this.state);
367 } else if (res.op == UserOperation.CreateComment) {
368 let data = res.data as CommentResponse;
370 // Necessary since it might be a user reply
371 if (data.recipient_ids.length == 0) {
372 this.state.comments.unshift(data.comment);
373 this.setState(this.state);
375 } else if (res.op == UserOperation.SaveComment) {
376 let data = res.data as CommentResponse;
377 saveCommentRes(data, this.state.comments);
378 this.setState(this.state);
379 } else if (res.op == UserOperation.CreateCommentLike) {
380 let data = res.data as CommentResponse;
381 createCommentLikeRes(data, this.state.comments);
382 this.setState(this.state);