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}
199 type_={this.state.dataType}
200 onChange={this.handleDataTypeChange}
204 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
207 href={`/feeds/c/${this.state.communityName}.xml?sort=${
208 SortType[this.state.sort]
213 <svg class="icon text-muted small">
214 <use xlinkHref="#icon-rss">#</use>
224 {this.state.page > 1 && (
226 class="btn btn-sm btn-secondary mr-1"
227 onClick={linkEvent(this, this.prevPage)}
232 {this.state.posts.length == fetchLimit && (
234 class="btn btn-sm btn-secondary"
235 onClick={linkEvent(this, this.nextPage)}
244 nextPage(i: Community) {
249 window.scrollTo(0, 0);
252 prevPage(i: Community) {
257 window.scrollTo(0, 0);
260 handleSortChange(val: SortType) {
261 this.state.sort = val;
263 this.state.loading = true;
264 this.setState(this.state);
267 window.scrollTo(0, 0);
270 handleDataTypeChange(val: DataType) {
271 this.state.dataType = val;
273 this.state.loading = true;
274 this.setState(this.state);
277 window.scrollTo(0, 0);
281 let dataTypeStr = DataType[this.state.dataType].toLowerCase();
282 let sortStr = SortType[this.state.sort].toLowerCase();
283 this.props.history.push(
284 `/c/${this.state.community.name}/data_type/${dataTypeStr}/sort/${sortStr}/page/${this.state.page}`
289 if (this.state.dataType == DataType.Post) {
290 let getPostsForm: GetPostsForm = {
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.getPosts(getPostsForm);
299 let getCommentsForm: GetCommentsForm = {
300 page: this.state.page,
302 sort: SortType[this.state.sort],
303 type_: ListingType[ListingType.Community],
304 community_id: this.state.community.id,
306 WebSocketService.Instance.getComments(getCommentsForm);
310 parseMessage(msg: WebSocketJsonResponse) {
312 let res = wsJsonToRes(msg);
314 toast(i18n.t(msg.error), 'danger');
315 this.context.router.history.push('/');
317 } else if (msg.reconnect) {
319 } else if (res.op == UserOperation.GetCommunity) {
320 let data = res.data as GetCommunityResponse;
321 this.state.community = data.community;
322 this.state.moderators = data.moderators;
323 this.state.admins = data.admins;
324 this.state.online = data.online;
325 document.title = `/c/${this.state.community.name} - ${WebSocketService.Instance.site.name}`;
326 this.setState(this.state);
328 } else if (res.op == UserOperation.EditCommunity) {
329 let data = res.data as CommunityResponse;
330 this.state.community = data.community;
331 this.setState(this.state);
332 } else if (res.op == UserOperation.FollowCommunity) {
333 let data = res.data as CommunityResponse;
334 this.state.community.subscribed = data.community.subscribed;
335 this.state.community.number_of_subscribers =
336 data.community.number_of_subscribers;
337 this.setState(this.state);
338 } else if (res.op == UserOperation.GetPosts) {
339 let data = res.data as GetPostsResponse;
340 this.state.posts = data.posts;
341 this.state.loading = false;
342 this.setState(this.state);
343 } else if (res.op == UserOperation.EditPost) {
344 let data = res.data as PostResponse;
345 editPostFindRes(data, this.state.posts);
346 this.setState(this.state);
347 } else if (res.op == UserOperation.CreatePost) {
348 let data = res.data as PostResponse;
349 this.state.posts.unshift(data.post);
350 this.setState(this.state);
351 } else if (res.op == UserOperation.CreatePostLike) {
352 let data = res.data as PostResponse;
353 createPostLikeFindRes(data, this.state.posts);
354 this.setState(this.state);
355 } else if (res.op == UserOperation.AddModToCommunity) {
356 let data = res.data as AddModToCommunityResponse;
357 this.state.moderators = data.moderators;
358 this.setState(this.state);
359 } else if (res.op == UserOperation.BanFromCommunity) {
360 let data = res.data as BanFromCommunityResponse;
363 .filter(p => p.creator_id == data.user.id)
364 .forEach(p => (p.banned = data.banned));
366 this.setState(this.state);
367 } else if (res.op == UserOperation.GetComments) {
368 let data = res.data as GetCommentsResponse;
369 this.state.comments = data.comments;
370 this.state.loading = false;
371 this.setState(this.state);
372 } else if (res.op == UserOperation.EditComment) {
373 let data = res.data as CommentResponse;
374 editCommentRes(data, this.state.comments);
375 this.setState(this.state);
376 } else if (res.op == UserOperation.CreateComment) {
377 let data = res.data as CommentResponse;
379 // Necessary since it might be a user reply
380 if (data.recipient_ids.length == 0) {
381 this.state.comments.unshift(data.comment);
382 this.setState(this.state);
384 } else if (res.op == UserOperation.SaveComment) {
385 let data = res.data as CommentResponse;
386 saveCommentRes(data, this.state.comments);
387 this.setState(this.state);
388 } else if (res.op == UserOperation.CreateCommentLike) {
389 let data = res.data as CommentResponse;
390 createCommentLikeRes(data, this.state.comments);
391 this.setState(this.state);