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,
28 } from '../interfaces';
29 import { WebSocketService } from '../services';
30 import { PostListings } from './post-listings';
31 import { CommentNodes } from './comment-nodes';
32 import { SortSelect } from './sort-select';
33 import { DataTypeSelect } from './data-type-select';
34 import { Sidebar } from './sidebar';
45 createPostLikeFindRes,
50 import { i18n } from '../i18next';
53 community: CommunityI;
55 communityName: string;
56 moderators: Array<CommunityUser>;
57 admins: Array<UserView>;
61 comments: Array<Comment>;
68 interface CommunityProps {
80 export class Community extends Component<any, State> {
81 private subscription: Subscription;
82 private emptyState: State = {
91 number_of_subscribers: null,
92 number_of_posts: null,
93 number_of_comments: null,
100 last_refreshed_at: null,
101 creator_actor_id: null,
106 communityId: Number(this.props.match.params.id),
107 communityName: this.props.match.params.name,
112 dataType: getDataTypeFromProps(this.props),
113 sort: getSortTypeFromProps(this.props),
114 page: getPageFromProps(this.props),
118 creator_id: undefined,
119 published: undefined,
120 creator_name: undefined,
121 number_of_users: undefined,
122 number_of_posts: undefined,
123 number_of_comments: undefined,
124 number_of_communities: undefined,
125 enable_downvotes: undefined,
126 open_registration: undefined,
127 enable_nsfw: undefined,
131 constructor(props: any, context: any) {
132 super(props, context);
134 this.state = this.emptyState;
135 this.handleSortChange = this.handleSortChange.bind(this);
136 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
138 this.subscription = WebSocketService.Instance.subject
139 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
141 msg => this.parseMessage(msg),
142 err => console.error(err),
143 () => console.log('complete')
146 let form: GetCommunityForm = {
147 id: this.state.communityId ? this.state.communityId : null,
148 name: this.state.communityName ? this.state.communityName : null,
150 WebSocketService.Instance.getCommunity(form);
151 WebSocketService.Instance.getSite();
154 componentWillUnmount() {
155 this.subscription.unsubscribe();
158 static getDerivedStateFromProps(props: any): CommunityProps {
160 dataType: getDataTypeFromProps(props),
161 sort: getSortTypeFromProps(props),
162 page: getPageFromProps(props),
166 componentDidUpdate(_: any, lastState: State) {
168 lastState.dataType !== this.state.dataType ||
169 lastState.sort !== this.state.sort ||
170 lastState.page !== this.state.page
172 this.setState({ loading: true });
179 <div class="container">
181 {this.state.loading ? (
183 <svg class="icon icon-spinner spin">
184 <use xlinkHref="#icon-spinner"></use>
189 <div class="col-12 col-md-8">
191 {this.state.community.title}
192 {this.state.community.removed && (
193 <small className="ml-2 text-muted font-italic">
197 {this.state.community.nsfw && (
198 <small className="ml-2 text-muted font-italic">
206 <div class="col-12 col-md-4">
208 community={this.state.community}
209 moderators={this.state.moderators}
210 admins={this.state.admins}
211 online={this.state.online}
212 enableNsfw={this.state.site.enable_nsfw}
222 return this.state.dataType == DataType.Post ? (
224 posts={this.state.posts}
226 sort={this.state.sort}
227 enableDownvotes={this.state.site.enable_downvotes}
228 enableNsfw={this.state.site.enable_nsfw}
232 nodes={commentsToFlatNodes(this.state.comments)}
234 sortType={this.state.sort}
236 enableDownvotes={this.state.site.enable_downvotes}
246 type_={this.state.dataType}
247 onChange={this.handleDataTypeChange}
251 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
254 href={`/feeds/c/${this.state.communityName}.xml?sort=${
255 SortType[this.state.sort]
261 <svg class="icon text-muted small">
262 <use xlinkHref="#icon-rss">#</use>
272 {this.state.page > 1 && (
274 class="btn btn-sm btn-secondary mr-1"
275 onClick={linkEvent(this, this.prevPage)}
280 {this.state.posts.length > 0 && (
282 class="btn btn-sm btn-secondary"
283 onClick={linkEvent(this, this.nextPage)}
292 nextPage(i: Community) {
293 i.updateUrl({ page: i.state.page + 1 });
294 window.scrollTo(0, 0);
297 prevPage(i: Community) {
298 i.updateUrl({ page: i.state.page - 1 });
299 window.scrollTo(0, 0);
302 handleSortChange(val: SortType) {
303 this.updateUrl({ sort: SortType[val].toLowerCase(), page: 1 });
304 window.scrollTo(0, 0);
307 handleDataTypeChange(val: DataType) {
308 this.updateUrl({ dataType: DataType[val].toLowerCase(), page: 1 });
309 window.scrollTo(0, 0);
312 updateUrl(paramUpdates: UrlParams) {
314 paramUpdates.dataType || DataType[this.state.dataType].toLowerCase();
316 paramUpdates.sort || SortType[this.state.sort].toLowerCase();
317 const page = paramUpdates.page || this.state.page;
318 this.props.history.push(
319 `/c/${this.state.community.name}/data_type/${dataTypeStr}/sort/${sortStr}/page/${page}`
324 if (this.state.dataType == DataType.Post) {
325 let getPostsForm: GetPostsForm = {
326 page: this.state.page,
328 sort: SortType[this.state.sort],
329 type_: ListingType[ListingType.Community],
330 community_id: this.state.community.id,
332 WebSocketService.Instance.getPosts(getPostsForm);
334 let getCommentsForm: GetCommentsForm = {
335 page: this.state.page,
337 sort: SortType[this.state.sort],
338 type_: ListingType[ListingType.Community],
339 community_id: this.state.community.id,
341 WebSocketService.Instance.getComments(getCommentsForm);
345 parseMessage(msg: WebSocketJsonResponse) {
347 let res = wsJsonToRes(msg);
349 toast(i18n.t(msg.error), 'danger');
350 this.context.router.history.push('/');
352 } else if (msg.reconnect) {
354 } else if (res.op == UserOperation.GetCommunity) {
355 let data = res.data as GetCommunityResponse;
356 this.state.community = data.community;
357 this.state.moderators = data.moderators;
358 this.state.online = data.online;
359 document.title = `/c/${this.state.community.name} - ${this.state.site.name}`;
360 this.setState(this.state);
363 res.op == UserOperation.EditCommunity ||
364 res.op == UserOperation.DeleteCommunity ||
365 res.op == UserOperation.RemoveCommunity
367 let data = res.data as CommunityResponse;
368 this.state.community = data.community;
369 this.setState(this.state);
370 } else if (res.op == UserOperation.FollowCommunity) {
371 let data = res.data as CommunityResponse;
372 this.state.community.subscribed = data.community.subscribed;
373 this.state.community.number_of_subscribers =
374 data.community.number_of_subscribers;
375 this.setState(this.state);
376 } else if (res.op == UserOperation.GetPosts) {
377 let data = res.data as GetPostsResponse;
378 this.state.posts = data.posts;
379 this.state.loading = false;
380 this.setState(this.state);
383 res.op == UserOperation.EditPost ||
384 res.op == UserOperation.DeletePost ||
385 res.op == UserOperation.RemovePost ||
386 res.op == UserOperation.LockPost ||
387 res.op == UserOperation.StickyPost
389 let data = res.data as PostResponse;
390 editPostFindRes(data, this.state.posts);
391 this.setState(this.state);
392 } else if (res.op == UserOperation.CreatePost) {
393 let data = res.data as PostResponse;
394 this.state.posts.unshift(data.post);
395 this.setState(this.state);
396 } else if (res.op == UserOperation.CreatePostLike) {
397 let data = res.data as PostResponse;
398 createPostLikeFindRes(data, this.state.posts);
399 this.setState(this.state);
400 } else if (res.op == UserOperation.AddModToCommunity) {
401 let data = res.data as AddModToCommunityResponse;
402 this.state.moderators = data.moderators;
403 this.setState(this.state);
404 } else if (res.op == UserOperation.BanFromCommunity) {
405 let data = res.data as BanFromCommunityResponse;
408 .filter(p => p.creator_id == data.user.id)
409 .forEach(p => (p.banned = data.banned));
411 this.setState(this.state);
412 } else if (res.op == UserOperation.GetComments) {
413 let data = res.data as GetCommentsResponse;
414 this.state.comments = data.comments;
415 this.state.loading = false;
416 this.setState(this.state);
418 res.op == UserOperation.EditComment ||
419 res.op == UserOperation.DeleteComment ||
420 res.op == UserOperation.RemoveComment
422 let data = res.data as CommentResponse;
423 editCommentRes(data, this.state.comments);
424 this.setState(this.state);
425 } else if (res.op == UserOperation.CreateComment) {
426 let data = res.data as CommentResponse;
428 // Necessary since it might be a user reply
429 if (data.recipient_ids.length == 0) {
430 this.state.comments.unshift(data.comment);
431 this.setState(this.state);
433 } else if (res.op == UserOperation.SaveComment) {
434 let data = res.data as CommentResponse;
435 saveCommentRes(data, this.state.comments);
436 this.setState(this.state);
437 } else if (res.op == UserOperation.CreateCommentLike) {
438 let data = res.data as CommentResponse;
439 createCommentLikeRes(data, this.state.comments);
440 this.setState(this.state);
441 } else if (res.op == UserOperation.GetSite) {
442 let data = res.data as GetSiteResponse;
443 this.state.site = data.site;
444 this.state.admins = data.admins;
445 this.setState(this.state);