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.title) {
180 return `${this.state.community.title} - ${this.state.site.name}`;
188 <div class="container">
189 <Helmet title={this.documentTitle} />
190 {this.state.loading ? (
192 <svg class="icon icon-spinner spin">
193 <use xlinkHref="#icon-spinner"></use>
198 <div class="col-12 col-md-8">
203 <div class="col-12 col-md-4">
205 community={this.state.community}
206 moderators={this.state.moderators}
207 admins={this.state.admins}
208 online={this.state.online}
209 enableNsfw={this.state.site.enable_nsfw}
219 return this.state.dataType == DataType.Post ? (
221 posts={this.state.posts}
223 sort={this.state.sort}
224 enableDownvotes={this.state.site.enable_downvotes}
225 enableNsfw={this.state.site.enable_nsfw}
229 nodes={commentsToFlatNodes(this.state.comments)}
231 sortType={this.state.sort}
233 enableDownvotes={this.state.site.enable_downvotes}
243 type_={this.state.dataType}
244 onChange={this.handleDataTypeChange}
248 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
251 href={`/feeds/c/${this.state.communityName}.xml?sort=${
252 SortType[this.state.sort]
258 <svg class="icon text-muted small">
259 <use xlinkHref="#icon-rss">#</use>
269 {this.state.page > 1 && (
271 class="btn btn-secondary mr-1"
272 onClick={linkEvent(this, this.prevPage)}
277 {this.state.posts.length > 0 && (
279 class="btn btn-secondary"
280 onClick={linkEvent(this, this.nextPage)}
289 nextPage(i: Community) {
290 i.updateUrl({ page: i.state.page + 1 });
291 window.scrollTo(0, 0);
294 prevPage(i: Community) {
295 i.updateUrl({ page: i.state.page - 1 });
296 window.scrollTo(0, 0);
299 handleSortChange(val: SortType) {
300 this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
301 window.scrollTo(0, 0);
304 handleDataTypeChange(val: DataType) {
305 this.updateUrl({ dataType: DataType[val].toLowerCase(), page: 1 });
306 window.scrollTo(0, 0);
309 updateUrl(paramUpdates: UrlParams) {
311 paramUpdates.dataType || DataType[this.state.dataType].toLowerCase();
313 paramUpdates.sort || SortType[this.state.sort].toLowerCase();
314 const page = paramUpdates.page || this.state.page;
315 this.props.history.push(
316 `/c/${this.state.community.name}/data_type/${dataTypeStr}/sort/${sortStr}/page/${page}`
321 if (this.state.dataType == DataType.Post) {
322 let getPostsForm: GetPostsForm = {
323 page: this.state.page,
325 sort: SortType[this.state.sort],
326 type_: ListingType[ListingType.Community],
327 community_id: this.state.community.id,
329 WebSocketService.Instance.getPosts(getPostsForm);
331 let getCommentsForm: GetCommentsForm = {
332 page: this.state.page,
334 sort: SortType[this.state.sort],
335 type_: ListingType[ListingType.Community],
336 community_id: this.state.community.id,
338 WebSocketService.Instance.getComments(getCommentsForm);
342 parseMessage(msg: WebSocketJsonResponse) {
344 let res = wsJsonToRes(msg);
346 toast(i18n.t(msg.error), 'danger');
347 this.context.router.history.push('/');
349 } else if (msg.reconnect) {
351 } else if (res.op == UserOperation.GetCommunity) {
352 let data = res.data as GetCommunityResponse;
353 this.state.community = data.community;
354 this.state.moderators = data.moderators;
355 this.state.online = data.online;
356 this.setState(this.state);
359 res.op == UserOperation.EditCommunity ||
360 res.op == UserOperation.DeleteCommunity ||
361 res.op == UserOperation.RemoveCommunity
363 let data = res.data as CommunityResponse;
364 this.state.community = data.community;
365 this.setState(this.state);
366 } else if (res.op == UserOperation.FollowCommunity) {
367 let data = res.data as CommunityResponse;
368 this.state.community.subscribed = data.community.subscribed;
369 this.state.community.number_of_subscribers =
370 data.community.number_of_subscribers;
371 this.setState(this.state);
372 } else if (res.op == UserOperation.GetPosts) {
373 let data = res.data as GetPostsResponse;
374 this.state.posts = data.posts;
375 this.state.loading = false;
376 this.setState(this.state);
379 res.op == UserOperation.EditPost ||
380 res.op == UserOperation.DeletePost ||
381 res.op == UserOperation.RemovePost ||
382 res.op == UserOperation.LockPost ||
383 res.op == UserOperation.StickyPost
385 let data = res.data as PostResponse;
386 editPostFindRes(data, this.state.posts);
387 this.setState(this.state);
388 } else if (res.op == UserOperation.CreatePost) {
389 let data = res.data as PostResponse;
390 this.state.posts.unshift(data.post);
391 this.setState(this.state);
392 } else if (res.op == UserOperation.CreatePostLike) {
393 let data = res.data as PostResponse;
394 createPostLikeFindRes(data, this.state.posts);
395 this.setState(this.state);
396 } else if (res.op == UserOperation.AddModToCommunity) {
397 let data = res.data as AddModToCommunityResponse;
398 this.state.moderators = data.moderators;
399 this.setState(this.state);
400 } else if (res.op == UserOperation.BanFromCommunity) {
401 let data = res.data as BanFromCommunityResponse;
404 .filter(p => p.creator_id == data.user.id)
405 .forEach(p => (p.banned = data.banned));
407 this.setState(this.state);
408 } else if (res.op == UserOperation.GetComments) {
409 let data = res.data as GetCommentsResponse;
410 this.state.comments = data.comments;
411 this.state.loading = false;
412 this.setState(this.state);
414 res.op == UserOperation.EditComment ||
415 res.op == UserOperation.DeleteComment ||
416 res.op == UserOperation.RemoveComment
418 let data = res.data as CommentResponse;
419 editCommentRes(data, this.state.comments);
420 this.setState(this.state);
421 } else if (res.op == UserOperation.CreateComment) {
422 let data = res.data as CommentResponse;
424 // Necessary since it might be a user reply
425 if (data.recipient_ids.length == 0) {
426 this.state.comments.unshift(data.comment);
427 this.setState(this.state);
429 } else if (res.op == UserOperation.SaveComment) {
430 let data = res.data as CommentResponse;
431 saveCommentRes(data, this.state.comments);
432 this.setState(this.state);
433 } else if (res.op == UserOperation.CreateCommentLike) {
434 let data = res.data as CommentResponse;
435 createCommentLikeRes(data, this.state.comments);
436 this.setState(this.state);
437 } else if (res.op == UserOperation.GetSite) {
438 let data = res.data as GetSiteResponse;
439 this.state.site = data.site;
440 this.state.admins = data.admins;
441 this.setState(this.state);