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.admins = data.admins;
359 this.state.online = data.online;
360 document.title = `/c/${this.state.community.name} - ${this.state.site.name}`;
361 this.setState(this.state);
364 res.op == UserOperation.EditCommunity ||
365 res.op == UserOperation.DeleteCommunity ||
366 res.op == UserOperation.RemoveCommunity
368 let data = res.data as CommunityResponse;
369 this.state.community = data.community;
370 this.setState(this.state);
371 } else if (res.op == UserOperation.FollowCommunity) {
372 let data = res.data as CommunityResponse;
373 this.state.community.subscribed = data.community.subscribed;
374 this.state.community.number_of_subscribers =
375 data.community.number_of_subscribers;
376 this.setState(this.state);
377 } else if (res.op == UserOperation.GetPosts) {
378 let data = res.data as GetPostsResponse;
379 this.state.posts = data.posts;
380 this.state.loading = false;
381 this.setState(this.state);
383 } else if (res.op == UserOperation.EditPost) {
384 let data = res.data as PostResponse;
385 editPostFindRes(data, this.state.posts);
386 this.setState(this.state);
387 } else if (res.op == UserOperation.CreatePost) {
388 let data = res.data as PostResponse;
389 this.state.posts.unshift(data.post);
390 this.setState(this.state);
391 } else if (res.op == UserOperation.CreatePostLike) {
392 let data = res.data as PostResponse;
393 createPostLikeFindRes(data, this.state.posts);
394 this.setState(this.state);
395 } else if (res.op == UserOperation.AddModToCommunity) {
396 let data = res.data as AddModToCommunityResponse;
397 this.state.moderators = data.moderators;
398 this.setState(this.state);
399 } else if (res.op == UserOperation.BanFromCommunity) {
400 let data = res.data as BanFromCommunityResponse;
403 .filter(p => p.creator_id == data.user.id)
404 .forEach(p => (p.banned = data.banned));
406 this.setState(this.state);
407 } else if (res.op == UserOperation.GetComments) {
408 let data = res.data as GetCommentsResponse;
409 this.state.comments = data.comments;
410 this.state.loading = false;
411 this.setState(this.state);
413 res.op == UserOperation.EditComment ||
414 res.op == UserOperation.DeleteComment ||
415 res.op == UserOperation.RemoveComment
417 let data = res.data as CommentResponse;
418 editCommentRes(data, this.state.comments);
419 this.setState(this.state);
420 } else if (res.op == UserOperation.CreateComment) {
421 let data = res.data as CommentResponse;
423 // Necessary since it might be a user reply
424 if (data.recipient_ids.length == 0) {
425 this.state.comments.unshift(data.comment);
426 this.setState(this.state);
428 } else if (res.op == UserOperation.SaveComment) {
429 let data = res.data as CommentResponse;
430 saveCommentRes(data, this.state.comments);
431 this.setState(this.state);
432 } else if (res.op == UserOperation.CreateCommentLike) {
433 let data = res.data as CommentResponse;
434 createCommentLikeRes(data, this.state.comments);
435 this.setState(this.state);
436 } else if (res.op == UserOperation.GetSite) {
437 let data = res.data as GetSiteResponse;
438 this.state.site = data.site;
439 this.setState(this.state);