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,
85 last_refreshed_at: null,
86 creator_actor_id: null,
91 communityId: Number(this.props.match.params.id),
92 communityName: this.props.match.params.name,
97 dataType: getDataTypeFromProps(this.props),
98 sort: getSortTypeFromProps(this.props),
99 page: getPageFromProps(this.props),
102 constructor(props: any, context: any) {
103 super(props, context);
105 this.state = this.emptyState;
106 this.handleSortChange = this.handleSortChange.bind(this);
107 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
109 this.subscription = WebSocketService.Instance.subject
110 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
112 msg => this.parseMessage(msg),
113 err => console.error(err),
114 () => console.log('complete')
117 let form: GetCommunityForm = {
118 id: this.state.communityId ? this.state.communityId : null,
119 name: this.state.communityName ? this.state.communityName : null,
121 WebSocketService.Instance.getCommunity(form);
124 componentWillUnmount() {
125 this.subscription.unsubscribe();
128 // Necessary for back button for some reason
129 componentWillReceiveProps(nextProps: any) {
131 nextProps.history.action == 'POP' ||
132 nextProps.history.action == 'PUSH'
134 this.state.dataType = getDataTypeFromProps(nextProps);
135 this.state.sort = getSortTypeFromProps(nextProps);
136 this.state.page = getPageFromProps(nextProps);
137 this.setState(this.state);
144 <div class="container">
146 {this.state.loading ? (
148 <svg class="icon icon-spinner spin">
149 <use xlinkHref="#icon-spinner"></use>
154 <div class="col-12 col-md-8">
156 {this.state.community.title}
157 {this.state.community.removed && (
158 <small className="ml-2 text-muted font-italic">
162 {this.state.community.nsfw && (
163 <small className="ml-2 text-muted font-italic">
171 <div class="col-12 col-md-4">
173 community={this.state.community}
174 moderators={this.state.moderators}
175 admins={this.state.admins}
176 online={this.state.online}
186 return this.state.dataType == DataType.Post ? (
188 posts={this.state.posts}
190 sort={this.state.sort}
194 nodes={commentsToFlatNodes(this.state.comments)}
196 sortType={this.state.sort}
207 type_={this.state.dataType}
208 onChange={this.handleDataTypeChange}
212 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
215 href={`/feeds/c/${this.state.communityName}.xml?sort=${
216 SortType[this.state.sort]
221 <svg class="icon text-muted small">
222 <use xlinkHref="#icon-rss">#</use>
232 {this.state.page > 1 && (
234 class="btn btn-sm btn-secondary mr-1"
235 onClick={linkEvent(this, this.prevPage)}
240 {this.state.posts.length == fetchLimit && (
242 class="btn btn-sm btn-secondary"
243 onClick={linkEvent(this, this.nextPage)}
252 nextPage(i: Community) {
257 window.scrollTo(0, 0);
260 prevPage(i: Community) {
265 window.scrollTo(0, 0);
268 handleSortChange(val: SortType) {
269 this.state.sort = val;
271 this.state.loading = true;
272 this.setState(this.state);
275 window.scrollTo(0, 0);
278 handleDataTypeChange(val: DataType) {
279 this.state.dataType = val;
281 this.state.loading = true;
282 this.setState(this.state);
285 window.scrollTo(0, 0);
289 let dataTypeStr = DataType[this.state.dataType].toLowerCase();
290 let sortStr = SortType[this.state.sort].toLowerCase();
291 this.props.history.push(
292 `/c/${this.state.community.name}/data_type/${dataTypeStr}/sort/${sortStr}/page/${this.state.page}`
297 if (this.state.dataType == DataType.Post) {
298 let getPostsForm: GetPostsForm = {
299 page: this.state.page,
301 sort: SortType[this.state.sort],
302 type_: ListingType[ListingType.Community],
303 community_id: this.state.community.id,
305 WebSocketService.Instance.getPosts(getPostsForm);
307 let getCommentsForm: GetCommentsForm = {
308 page: this.state.page,
310 sort: SortType[this.state.sort],
311 type_: ListingType[ListingType.Community],
312 community_id: this.state.community.id,
314 WebSocketService.Instance.getComments(getCommentsForm);
318 parseMessage(msg: WebSocketJsonResponse) {
320 let res = wsJsonToRes(msg);
322 toast(i18n.t(msg.error), 'danger');
323 this.context.router.history.push('/');
325 } else if (msg.reconnect) {
327 } else if (res.op == UserOperation.GetCommunity) {
328 let data = res.data as GetCommunityResponse;
329 this.state.community = data.community;
330 this.state.moderators = data.moderators;
331 this.state.admins = data.admins;
332 this.state.online = data.online;
333 document.title = `/c/${this.state.community.name} - ${WebSocketService.Instance.site.name}`;
334 this.setState(this.state);
336 } else if (res.op == UserOperation.EditCommunity) {
337 let data = res.data as CommunityResponse;
338 this.state.community = data.community;
339 this.setState(this.state);
340 } else if (res.op == UserOperation.FollowCommunity) {
341 let data = res.data as CommunityResponse;
342 this.state.community.subscribed = data.community.subscribed;
343 this.state.community.number_of_subscribers =
344 data.community.number_of_subscribers;
345 this.setState(this.state);
346 } else if (res.op == UserOperation.GetPosts) {
347 let data = res.data as GetPostsResponse;
348 this.state.posts = data.posts;
349 this.state.loading = false;
350 this.setState(this.state);
352 } else if (res.op == UserOperation.EditPost) {
353 let data = res.data as PostResponse;
354 editPostFindRes(data, this.state.posts);
355 this.setState(this.state);
356 } else if (res.op == UserOperation.CreatePost) {
357 let data = res.data as PostResponse;
358 this.state.posts.unshift(data.post);
359 this.setState(this.state);
360 } else if (res.op == UserOperation.CreatePostLike) {
361 let data = res.data as PostResponse;
362 createPostLikeFindRes(data, this.state.posts);
363 this.setState(this.state);
364 } else if (res.op == UserOperation.AddModToCommunity) {
365 let data = res.data as AddModToCommunityResponse;
366 this.state.moderators = data.moderators;
367 this.setState(this.state);
368 } else if (res.op == UserOperation.BanFromCommunity) {
369 let data = res.data as BanFromCommunityResponse;
372 .filter(p => p.creator_id == data.user.id)
373 .forEach(p => (p.banned = data.banned));
375 this.setState(this.state);
376 } else if (res.op == UserOperation.GetComments) {
377 let data = res.data as GetCommentsResponse;
378 this.state.comments = data.comments;
379 this.state.loading = false;
380 this.setState(this.state);
381 } else if (res.op == UserOperation.EditComment) {
382 let data = res.data as CommentResponse;
383 editCommentRes(data, this.state.comments);
384 this.setState(this.state);
385 } else if (res.op == UserOperation.CreateComment) {
386 let data = res.data as CommentResponse;
388 // Necessary since it might be a user reply
389 if (data.recipient_ids.length == 0) {
390 this.state.comments.unshift(data.comment);
391 this.setState(this.state);
393 } else if (res.op == UserOperation.SaveComment) {
394 let data = res.data as CommentResponse;
395 saveCommentRes(data, this.state.comments);
396 this.setState(this.state);
397 } else if (res.op == UserOperation.CreateCommentLike) {
398 let data = res.data as CommentResponse;
399 createCommentLikeRes(data, this.state.comments);
400 this.setState(this.state);