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,
47 import { i18n } from '../i18next';
50 community: CommunityI;
52 communityName: string;
53 moderators: Array<CommunityUser>;
54 admins: Array<UserView>;
58 comments: Array<Comment>;
64 export class Community extends Component<any, State> {
65 private subscription: Subscription;
66 private emptyState: State = {
75 number_of_subscribers: null,
76 number_of_posts: null,
77 number_of_comments: null,
85 communityId: Number(this.props.match.params.id),
86 communityName: this.props.match.params.name,
91 dataType: getDataTypeFromProps(this.props),
92 sort: getSortTypeFromProps(this.props),
93 page: getPageFromProps(this.props),
96 constructor(props: any, context: any) {
97 super(props, context);
99 this.state = this.emptyState;
100 this.handleSortChange = this.handleSortChange.bind(this);
101 this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
103 this.subscription = WebSocketService.Instance.subject
104 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
106 msg => this.parseMessage(msg),
107 err => console.error(err),
108 () => console.log('complete')
111 let form: GetCommunityForm = {
112 id: this.state.communityId ? this.state.communityId : null,
113 name: this.state.communityName ? this.state.communityName : null,
115 WebSocketService.Instance.getCommunity(form);
118 componentWillUnmount() {
119 this.subscription.unsubscribe();
122 // Necessary for back button for some reason
123 componentWillReceiveProps(nextProps: any) {
125 nextProps.history.action == 'POP' ||
126 nextProps.history.action == 'PUSH'
128 this.state.dataType = getDataTypeFromProps(nextProps);
129 this.state.sort = getSortTypeFromProps(nextProps);
130 this.state.page = getPageFromProps(nextProps);
131 this.setState(this.state);
138 <div class="container">
139 {this.state.loading ? (
141 <svg class="icon icon-spinner spin">
142 <use xlinkHref="#icon-spinner"></use>
147 <div class="col-12 col-md-8">
149 {this.state.community.title}
150 {this.state.community.removed && (
151 <small className="ml-2 text-muted font-italic">
155 {this.state.community.nsfw && (
156 <small className="ml-2 text-muted font-italic">
165 <div class="col-12 col-md-4">
167 community={this.state.community}
168 moderators={this.state.moderators}
169 admins={this.state.admins}
170 online={this.state.online}
180 return this.state.dataType == DataType.Post ? (
182 posts={this.state.posts}
184 sort={this.state.sort}
188 nodes={commentsToFlatNodes(this.state.comments)}
190 sortType={this.state.sort}
199 type_={this.state.dataType}
200 onChange={this.handleDataTypeChange}
204 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
207 href={`/feeds/c/${this.state.communityName}.xml?sort=${
208 SortType[this.state.sort]
212 <svg class="icon mx-2 text-muted small">
213 <use xlinkHref="#icon-rss">#</use>
223 {this.state.page > 1 && (
225 class="btn btn-sm btn-secondary mr-1"
226 onClick={linkEvent(this, this.prevPage)}
231 {this.state.posts.length == fetchLimit && (
233 class="btn btn-sm btn-secondary"
234 onClick={linkEvent(this, this.nextPage)}
243 nextPage(i: Community) {
248 window.scrollTo(0, 0);
251 prevPage(i: Community) {
256 window.scrollTo(0, 0);
259 handleSortChange(val: SortType) {
260 this.state.sort = val;
262 this.state.loading = true;
263 this.setState(this.state);
266 window.scrollTo(0, 0);
269 handleDataTypeChange(val: DataType) {
270 this.state.dataType = val;
272 this.state.loading = true;
273 this.setState(this.state);
276 window.scrollTo(0, 0);
280 let dataTypeStr = DataType[this.state.dataType].toLowerCase();
281 let sortStr = SortType[this.state.sort].toLowerCase();
282 this.props.history.push(
283 `/c/${this.state.community.name}/data_type/${dataTypeStr}/sort/${sortStr}/page/${this.state.page}`
288 if (this.state.dataType == DataType.Post) {
289 let getPostsForm: GetPostsForm = {
290 page: this.state.page,
292 sort: SortType[this.state.sort],
293 type_: ListingType[ListingType.Community],
294 community_id: this.state.community.id,
296 WebSocketService.Instance.getPosts(getPostsForm);
298 let getCommentsForm: GetCommentsForm = {
299 page: this.state.page,
301 sort: SortType[this.state.sort],
302 type_: ListingType[ListingType.Community],
303 community_id: this.state.community.id,
305 WebSocketService.Instance.getComments(getCommentsForm);
309 parseMessage(msg: WebSocketJsonResponse) {
311 let res = wsJsonToRes(msg);
313 toast(i18n.t(msg.error), 'danger');
314 this.context.router.history.push('/');
316 } else if (msg.reconnect) {
318 } else if (res.op == UserOperation.GetCommunity) {
319 let data = res.data as GetCommunityResponse;
320 this.state.community = data.community;
321 this.state.moderators = data.moderators;
322 this.state.admins = data.admins;
323 this.state.online = data.online;
324 document.title = `/c/${this.state.community.name} - ${WebSocketService.Instance.site.name}`;
325 this.setState(this.state);
327 } else if (res.op == UserOperation.EditCommunity) {
328 let data = res.data as CommunityResponse;
329 this.state.community = data.community;
330 this.setState(this.state);
331 } else if (res.op == UserOperation.FollowCommunity) {
332 let data = res.data as CommunityResponse;
333 this.state.community.subscribed = data.community.subscribed;
334 this.state.community.number_of_subscribers =
335 data.community.number_of_subscribers;
336 this.setState(this.state);
337 } else if (res.op == UserOperation.GetPosts) {
338 let data = res.data as GetPostsResponse;
339 this.state.posts = data.posts;
340 this.state.loading = false;
341 this.setState(this.state);
342 } else if (res.op == UserOperation.EditPost) {
343 let data = res.data as PostResponse;
344 editPostFindRes(data, this.state.posts);
345 this.setState(this.state);
346 } else if (res.op == UserOperation.CreatePost) {
347 let data = res.data as PostResponse;
348 this.state.posts.unshift(data.post);
349 this.setState(this.state);
350 } else if (res.op == UserOperation.CreatePostLike) {
351 let data = res.data as PostResponse;
352 createPostLikeFindRes(data, this.state.posts);
353 this.setState(this.state);
354 } else if (res.op == UserOperation.AddModToCommunity) {
355 let data = res.data as AddModToCommunityResponse;
356 this.state.moderators = data.moderators;
357 this.setState(this.state);
358 } else if (res.op == UserOperation.BanFromCommunity) {
359 let data = res.data as BanFromCommunityResponse;
362 .filter(p => p.creator_id == data.user.id)
363 .forEach(p => (p.banned = data.banned));
365 this.setState(this.state);
366 } else if (res.op == UserOperation.GetComments) {
367 let data = res.data as GetCommentsResponse;
368 this.state.comments = data.comments;
369 this.state.loading = false;
370 this.setState(this.state);
371 } else if (res.op == UserOperation.EditComment) {
372 let data = res.data as CommentResponse;
373 editCommentRes(data, this.state.comments);
374 this.setState(this.state);
375 } else if (res.op == UserOperation.CreateComment) {
376 let data = res.data as CommentResponse;
378 // Necessary since it might be a user reply
379 if (data.recipient_ids.length == 0) {
380 this.state.comments.unshift(data.comment);
381 this.setState(this.state);
383 } else if (res.op == UserOperation.SaveComment) {
384 let data = res.data as CommentResponse;
385 saveCommentRes(data, this.state.comments);
386 this.setState(this.state);
387 } else if (res.op == UserOperation.CreateCommentLike) {
388 let data = res.data as CommentResponse;
389 createCommentLikeRes(data, this.state.comments);
390 this.setState(this.state);