1 import { Component, linkEvent } from 'inferno';
2 import { Helmet } from 'inferno-helmet';
3 import { Subscription } from 'rxjs';
4 import { retryWhen, delay, take } from 'rxjs/operators';
7 Community as CommunityI,
20 AddModToCommunityResponse,
21 BanFromCommunityResponse,
26 WebSocketJsonResponse,
29 } from '../interfaces';
30 import { WebSocketService } from '../services';
31 import { PostListings } from './post-listings';
32 import { CommentNodes } from './comment-nodes';
33 import { SortSelect } from './sort-select';
34 import { DataTypeSelect } from './data-type-select';
35 import { Sidebar } from './sidebar';
46 createPostLikeFindRes,
51 import { i18n } from '../i18next';
54 community: CommunityI;
56 communityName: string;
57 moderators: Array<CommunityUser>;
58 admins: Array<UserView>;
62 comments: Array<Comment>;
69 interface CommunityProps {
81 export class Community extends Component<any, State> {
82 private subscription: Subscription;
83 private emptyState: State = {
92 number_of_subscribers: null,
93 number_of_posts: null,
94 number_of_comments: null,
101 last_refreshed_at: null,
102 creator_actor_id: null,
107 communityId: Number(this.props.match.params.id),
108 communityName: this.props.match.params.name,
113 dataType: getDataTypeFromProps(this.props),
114 sort: getSortTypeFromProps(this.props),
115 page: getPageFromProps(this.props),
119 creator_id: undefined,
120 published: undefined,
121 creator_name: undefined,
122 number_of_users: undefined,
123 number_of_posts: undefined,
124 number_of_comments: undefined,
125 number_of_communities: undefined,
126 enable_downvotes: undefined,
127 open_registration: undefined,
128 enable_nsfw: undefined,
132 constructor(props: any, context: any) {
133 super(props, context);
135 this.state = this.emptyState;
136 this.handleSortChange = this.handleSortChange.bind(this);
137 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
139 this.subscription = WebSocketService.Instance.subject
140 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
142 msg => this.parseMessage(msg),
143 err => console.error(err),
144 () => console.log('complete')
147 let form: GetCommunityForm = {
148 id: this.state.communityId ? this.state.communityId : null,
149 name: this.state.communityName ? this.state.communityName : null,
151 WebSocketService.Instance.getCommunity(form);
152 WebSocketService.Instance.getSite();
155 componentWillUnmount() {
156 this.subscription.unsubscribe();
159 static getDerivedStateFromProps(props: any): CommunityProps {
161 dataType: getDataTypeFromProps(props),
162 sort: getSortTypeFromProps(props),
163 page: getPageFromProps(props),
167 componentDidUpdate(_: any, lastState: State) {
169 lastState.dataType !== this.state.dataType ||
170 lastState.sort !== this.state.sort ||
171 lastState.page !== this.state.page
173 this.setState({ loading: true });
178 get documentTitle(): string {
179 if (this.state.community.name) {
180 return `/c/${this.state.community.name} - ${this.state.site.name}`;
188 <div class="container">
189 <Helmet title={this.documentTitle} />
191 {this.state.loading ? (
193 <svg class="icon icon-spinner spin">
194 <use xlinkHref="#icon-spinner"></use>
199 <div class="col-12 col-md-8">
201 {this.state.community.title}
202 {this.state.community.removed && (
203 <small className="ml-2 text-muted font-italic">
207 {this.state.community.nsfw && (
208 <small className="ml-2 text-muted font-italic">
216 <div class="col-12 col-md-4">
218 community={this.state.community}
219 moderators={this.state.moderators}
220 admins={this.state.admins}
221 online={this.state.online}
222 enableNsfw={this.state.site.enable_nsfw}
232 return this.state.dataType == DataType.Post ? (
234 posts={this.state.posts}
236 sort={this.state.sort}
237 enableDownvotes={this.state.site.enable_downvotes}
238 enableNsfw={this.state.site.enable_nsfw}
242 nodes={commentsToFlatNodes(this.state.comments)}
244 sortType={this.state.sort}
246 enableDownvotes={this.state.site.enable_downvotes}
256 type_={this.state.dataType}
257 onChange={this.handleDataTypeChange}
261 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
264 href={`/feeds/c/${this.state.communityName}.xml?sort=${
265 SortType[this.state.sort]
271 <svg class="icon text-muted small">
272 <use xlinkHref="#icon-rss">#</use>
282 {this.state.page > 1 && (
284 class="btn btn-secondary mr-1"
285 onClick={linkEvent(this, this.prevPage)}
290 {this.state.posts.length > 0 && (
292 class="btn btn-secondary"
293 onClick={linkEvent(this, this.nextPage)}
302 nextPage(i: Community) {
303 i.updateUrl({ page: i.state.page + 1 });
304 window.scrollTo(0, 0);
307 prevPage(i: Community) {
308 i.updateUrl({ page: i.state.page - 1 });
309 window.scrollTo(0, 0);
312 handleSortChange(val: SortType) {
313 this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
314 window.scrollTo(0, 0);
317 handleDataTypeChange(val: DataType) {
318 this.updateUrl({ dataType: DataType[val].toLowerCase(), page: 1 });
319 window.scrollTo(0, 0);
322 updateUrl(paramUpdates: UrlParams) {
324 paramUpdates.dataType || DataType[this.state.dataType].toLowerCase();
326 paramUpdates.sort || SortType[this.state.sort].toLowerCase();
327 const page = paramUpdates.page || this.state.page;
328 this.props.history.push(
329 `/c/${this.state.community.name}/data_type/${dataTypeStr}/sort/${sortStr}/page/${page}`
334 if (this.state.dataType == DataType.Post) {
335 let getPostsForm: GetPostsForm = {
336 page: this.state.page,
338 sort: SortType[this.state.sort],
339 type_: ListingType[ListingType.Community],
340 community_id: this.state.community.id,
342 WebSocketService.Instance.getPosts(getPostsForm);
344 let getCommentsForm: GetCommentsForm = {
345 page: this.state.page,
347 sort: SortType[this.state.sort],
348 type_: ListingType[ListingType.Community],
349 community_id: this.state.community.id,
351 WebSocketService.Instance.getComments(getCommentsForm);
355 parseMessage(msg: WebSocketJsonResponse) {
357 let res = wsJsonToRes(msg);
359 toast(i18n.t(msg.error), 'danger');
360 this.context.router.history.push('/');
362 } else if (msg.reconnect) {
364 } else if (res.op == UserOperation.GetCommunity) {
365 let data = res.data as GetCommunityResponse;
366 this.state.community = data.community;
367 this.state.moderators = data.moderators;
368 this.state.online = data.online;
369 this.setState(this.state);
372 res.op == UserOperation.EditCommunity ||
373 res.op == UserOperation.DeleteCommunity ||
374 res.op == UserOperation.RemoveCommunity
376 let data = res.data as CommunityResponse;
377 this.state.community = data.community;
378 this.setState(this.state);
379 } else if (res.op == UserOperation.FollowCommunity) {
380 let data = res.data as CommunityResponse;
381 this.state.community.subscribed = data.community.subscribed;
382 this.state.community.number_of_subscribers =
383 data.community.number_of_subscribers;
384 this.setState(this.state);
385 } else if (res.op == UserOperation.GetPosts) {
386 let data = res.data as GetPostsResponse;
387 this.state.posts = data.posts;
388 this.state.loading = false;
389 this.setState(this.state);
392 res.op == UserOperation.EditPost ||
393 res.op == UserOperation.DeletePost ||
394 res.op == UserOperation.RemovePost ||
395 res.op == UserOperation.LockPost ||
396 res.op == UserOperation.StickyPost
398 let data = res.data as PostResponse;
399 editPostFindRes(data, this.state.posts);
400 this.setState(this.state);
401 } else if (res.op == UserOperation.CreatePost) {
402 let data = res.data as PostResponse;
403 this.state.posts.unshift(data.post);
404 this.setState(this.state);
405 } else if (res.op == UserOperation.CreatePostLike) {
406 let data = res.data as PostResponse;
407 createPostLikeFindRes(data, this.state.posts);
408 this.setState(this.state);
409 } else if (res.op == UserOperation.AddModToCommunity) {
410 let data = res.data as AddModToCommunityResponse;
411 this.state.moderators = data.moderators;
412 this.setState(this.state);
413 } else if (res.op == UserOperation.BanFromCommunity) {
414 let data = res.data as BanFromCommunityResponse;
417 .filter(p => p.creator_id == data.user.id)
418 .forEach(p => (p.banned = data.banned));
420 this.setState(this.state);
421 } else if (res.op == UserOperation.GetComments) {
422 let data = res.data as GetCommentsResponse;
423 this.state.comments = data.comments;
424 this.state.loading = false;
425 this.setState(this.state);
427 res.op == UserOperation.EditComment ||
428 res.op == UserOperation.DeleteComment ||
429 res.op == UserOperation.RemoveComment
431 let data = res.data as CommentResponse;
432 editCommentRes(data, this.state.comments);
433 this.setState(this.state);
434 } else if (res.op == UserOperation.CreateComment) {
435 let data = res.data as CommentResponse;
437 // Necessary since it might be a user reply
438 if (data.recipient_ids.length == 0) {
439 this.state.comments.unshift(data.comment);
440 this.setState(this.state);
442 } else if (res.op == UserOperation.SaveComment) {
443 let data = res.data as CommentResponse;
444 saveCommentRes(data, this.state.comments);
445 this.setState(this.state);
446 } else if (res.op == UserOperation.CreateCommentLike) {
447 let data = res.data as CommentResponse;
448 createCommentLikeRes(data, this.state.comments);
449 this.setState(this.state);
450 } else if (res.op == UserOperation.GetSite) {
451 let data = res.data as GetSiteResponse;
452 this.state.site = data.site;
453 this.state.admins = data.admins;
454 this.setState(this.state);