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]
222 <svg class="icon text-muted small">
223 <use xlinkHref="#icon-rss">#</use>
233 {this.state.page > 1 && (
235 class="btn btn-sm btn-secondary mr-1"
236 onClick={linkEvent(this, this.prevPage)}
241 {this.state.posts.length == fetchLimit && (
243 class="btn btn-sm btn-secondary"
244 onClick={linkEvent(this, this.nextPage)}
253 nextPage(i: Community) {
258 window.scrollTo(0, 0);
261 prevPage(i: Community) {
266 window.scrollTo(0, 0);
269 handleSortChange(val: SortType) {
270 this.state.sort = val;
272 this.state.loading = true;
273 this.setState(this.state);
276 window.scrollTo(0, 0);
279 handleDataTypeChange(val: DataType) {
280 this.state.dataType = val;
282 this.state.loading = true;
283 this.setState(this.state);
286 window.scrollTo(0, 0);
290 let dataTypeStr = DataType[this.state.dataType].toLowerCase();
291 let sortStr = SortType[this.state.sort].toLowerCase();
292 this.props.history.push(
293 `/c/${this.state.community.name}/data_type/${dataTypeStr}/sort/${sortStr}/page/${this.state.page}`
298 if (this.state.dataType == DataType.Post) {
299 let getPostsForm: GetPostsForm = {
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.getPosts(getPostsForm);
308 let getCommentsForm: GetCommentsForm = {
309 page: this.state.page,
311 sort: SortType[this.state.sort],
312 type_: ListingType[ListingType.Community],
313 community_id: this.state.community.id,
315 WebSocketService.Instance.getComments(getCommentsForm);
319 parseMessage(msg: WebSocketJsonResponse) {
321 let res = wsJsonToRes(msg);
323 toast(i18n.t(msg.error), 'danger');
324 this.context.router.history.push('/');
326 } else if (msg.reconnect) {
328 } else if (res.op == UserOperation.GetCommunity) {
329 let data = res.data as GetCommunityResponse;
330 this.state.community = data.community;
331 this.state.moderators = data.moderators;
332 this.state.admins = data.admins;
333 this.state.online = data.online;
334 document.title = `/c/${this.state.community.name} - ${WebSocketService.Instance.site.name}`;
335 this.setState(this.state);
337 } else if (res.op == UserOperation.EditCommunity) {
338 let data = res.data as CommunityResponse;
339 this.state.community = data.community;
340 this.setState(this.state);
341 } else if (res.op == UserOperation.FollowCommunity) {
342 let data = res.data as CommunityResponse;
343 this.state.community.subscribed = data.community.subscribed;
344 this.state.community.number_of_subscribers =
345 data.community.number_of_subscribers;
346 this.setState(this.state);
347 } else if (res.op == UserOperation.GetPosts) {
348 let data = res.data as GetPostsResponse;
349 this.state.posts = data.posts;
350 this.state.loading = false;
351 this.setState(this.state);
353 } else if (res.op == UserOperation.EditPost) {
354 let data = res.data as PostResponse;
355 editPostFindRes(data, this.state.posts);
356 this.setState(this.state);
357 } else if (res.op == UserOperation.CreatePost) {
358 let data = res.data as PostResponse;
359 this.state.posts.unshift(data.post);
360 this.setState(this.state);
361 } else if (res.op == UserOperation.CreatePostLike) {
362 let data = res.data as PostResponse;
363 createPostLikeFindRes(data, this.state.posts);
364 this.setState(this.state);
365 } else if (res.op == UserOperation.AddModToCommunity) {
366 let data = res.data as AddModToCommunityResponse;
367 this.state.moderators = data.moderators;
368 this.setState(this.state);
369 } else if (res.op == UserOperation.BanFromCommunity) {
370 let data = res.data as BanFromCommunityResponse;
373 .filter(p => p.creator_id == data.user.id)
374 .forEach(p => (p.banned = data.banned));
376 this.setState(this.state);
377 } else if (res.op == UserOperation.GetComments) {
378 let data = res.data as GetCommentsResponse;
379 this.state.comments = data.comments;
380 this.state.loading = false;
381 this.setState(this.state);
382 } else if (res.op == UserOperation.EditComment) {
383 let data = res.data as CommentResponse;
384 editCommentRes(data, this.state.comments);
385 this.setState(this.state);
386 } else if (res.op == UserOperation.CreateComment) {
387 let data = res.data as CommentResponse;
389 // Necessary since it might be a user reply
390 if (data.recipient_ids.length == 0) {
391 this.state.comments.unshift(data.comment);
392 this.setState(this.state);
394 } else if (res.op == UserOperation.SaveComment) {
395 let data = res.data as CommentResponse;
396 saveCommentRes(data, this.state.comments);
397 this.setState(this.state);
398 } else if (res.op == UserOperation.CreateCommentLike) {
399 let data = res.data as CommentResponse;
400 createCommentLikeRes(data, this.state.comments);
401 this.setState(this.state);