]> Untitled Git - lemmy.git/blob - ui/src/components/community.tsx
Merge branch 'master' of https://github.com/AndreVallestero/lemmy into AndreVallester...
[lemmy.git] / ui / src / components / community.tsx
1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from 'rxjs';
3 import { retryWhen, delay, take } from 'rxjs/operators';
4 import {
5   UserOperation,
6   Community as CommunityI,
7   GetCommunityResponse,
8   CommunityResponse,
9   CommunityUser,
10   UserView,
11   SortType,
12   Post,
13   GetPostsForm,
14   GetCommunityForm,
15   ListingType,
16   DataType,
17   GetPostsResponse,
18   PostResponse,
19   AddModToCommunityResponse,
20   BanFromCommunityResponse,
21   Comment,
22   GetCommentsForm,
23   GetCommentsResponse,
24   CommentResponse,
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';
33 import {
34   wsJsonToRes,
35   fetchLimit,
36   toast,
37   getPageFromProps,
38   getSortTypeFromProps,
39   getDataTypeFromProps,
40   editCommentRes,
41   saveCommentRes,
42   createCommentLikeRes,
43   createPostLikeFindRes,
44   editPostFindRes,
45   commentsToFlatNodes,
46 } from '../utils';
47 import { i18n } from '../i18next';
48
49 interface State {
50   community: CommunityI;
51   communityId: number;
52   communityName: string;
53   moderators: Array<CommunityUser>;
54   admins: Array<UserView>;
55   online: number;
56   loading: boolean;
57   posts: Array<Post>;
58   comments: Array<Comment>;
59   dataType: DataType;
60   sort: SortType;
61   page: number;
62 }
63
64 export class Community extends Component<any, State> {
65   private subscription: Subscription;
66   private emptyState: State = {
67     community: {
68       id: null,
69       name: null,
70       title: null,
71       category_id: null,
72       category_name: null,
73       creator_id: null,
74       creator_name: null,
75       number_of_subscribers: null,
76       number_of_posts: null,
77       number_of_comments: null,
78       published: null,
79       removed: null,
80       nsfw: false,
81       deleted: null,
82     },
83     moderators: [],
84     admins: [],
85     communityId: Number(this.props.match.params.id),
86     communityName: this.props.match.params.name,
87     online: null,
88     loading: true,
89     posts: [],
90     comments: [],
91     dataType: getDataTypeFromProps(this.props),
92     sort: getSortTypeFromProps(this.props),
93     page: getPageFromProps(this.props),
94   };
95
96   constructor(props: any, context: any) {
97     super(props, context);
98
99     this.state = this.emptyState;
100     this.handleSortChange = this.handleSortChange.bind(this);
101     this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
102
103     this.subscription = WebSocketService.Instance.subject
104       .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
105       .subscribe(
106         msg => this.parseMessage(msg),
107         err => console.error(err),
108         () => console.log('complete')
109       );
110
111     let form: GetCommunityForm = {
112       id: this.state.communityId ? this.state.communityId : null,
113       name: this.state.communityName ? this.state.communityName : null,
114     };
115     WebSocketService.Instance.getCommunity(form);
116   }
117
118   componentWillUnmount() {
119     this.subscription.unsubscribe();
120   }
121
122   // Necessary for back button for some reason
123   componentWillReceiveProps(nextProps: any) {
124     if (
125       nextProps.history.action == 'POP' ||
126       nextProps.history.action == 'PUSH'
127     ) {
128       this.state.dataType = getDataTypeFromProps(nextProps);
129       this.state.sort = getSortTypeFromProps(nextProps);
130       this.state.page = getPageFromProps(nextProps);
131       this.setState(this.state);
132       this.fetchData();
133     }
134   }
135
136   render() {
137     return (
138       <div class="container">
139         {this.selects()}
140         {this.state.loading ? (
141           <h5>
142             <svg class="icon icon-spinner spin">
143               <use xlinkHref="#icon-spinner"></use>
144             </svg>
145           </h5>
146         ) : (
147           <div class="row">
148             <div class="col-12 col-md-8">
149               <h5>
150                 {this.state.community.title}
151                 {this.state.community.removed && (
152                   <small className="ml-2 text-muted font-italic">
153                     {i18n.t('removed')}
154                   </small>
155                 )}
156                 {this.state.community.nsfw && (
157                   <small className="ml-2 text-muted font-italic">
158                     {i18n.t('nsfw')}
159                   </small>
160                 )}
161               </h5>
162               {this.listings()}
163               {this.paginator()}
164             </div>
165             <div class="col-12 col-md-4">
166               <Sidebar
167                 community={this.state.community}
168                 moderators={this.state.moderators}
169                 admins={this.state.admins}
170                 online={this.state.online}
171               />
172             </div>
173           </div>
174         )}
175       </div>
176     );
177   }
178
179   listings() {
180     return this.state.dataType == DataType.Post ? (
181       <PostListings
182         posts={this.state.posts}
183         removeDuplicates
184         sort={this.state.sort}
185       />
186     ) : (
187       <CommentNodes
188         nodes={commentsToFlatNodes(this.state.comments)}
189         noIndent
190         sortType={this.state.sort}
191       />
192     );
193   }
194
195   selects() {
196     return (
197       <div class="mb-2">
198         <DataTypeSelect
199           type_={this.state.dataType}
200           onChange={this.handleDataTypeChange}
201         />
202
203         <span class="mx-3">
204           <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
205         </span>
206         <a
207           href={`/feeds/c/${this.state.communityName}.xml?sort=${
208             SortType[this.state.sort]
209           }`}
210           target="_blank"
211         >
212           <svg class="icon text-muted small">
213             <use xlinkHref="#icon-rss">#</use>
214           </svg>
215         </a>
216       </div>
217     );
218   }
219
220   paginator() {
221     return (
222       <div class="my-2">
223         {this.state.page > 1 && (
224           <button
225             class="btn btn-sm btn-secondary mr-1"
226             onClick={linkEvent(this, this.prevPage)}
227           >
228             {i18n.t('prev')}
229           </button>
230         )}
231         {this.state.posts.length == fetchLimit && (
232           <button
233             class="btn btn-sm btn-secondary"
234             onClick={linkEvent(this, this.nextPage)}
235           >
236             {i18n.t('next')}
237           </button>
238         )}
239       </div>
240     );
241   }
242
243   nextPage(i: Community) {
244     i.state.page++;
245     i.setState(i.state);
246     i.updateUrl();
247     i.fetchData();
248     window.scrollTo(0, 0);
249   }
250
251   prevPage(i: Community) {
252     i.state.page--;
253     i.setState(i.state);
254     i.updateUrl();
255     i.fetchData();
256     window.scrollTo(0, 0);
257   }
258
259   handleSortChange(val: SortType) {
260     this.state.sort = val;
261     this.state.page = 1;
262     this.state.loading = true;
263     this.setState(this.state);
264     this.updateUrl();
265     this.fetchData();
266     window.scrollTo(0, 0);
267   }
268
269   handleDataTypeChange(val: DataType) {
270     this.state.dataType = val;
271     this.state.page = 1;
272     this.state.loading = true;
273     this.setState(this.state);
274     this.updateUrl();
275     this.fetchData();
276     window.scrollTo(0, 0);
277   }
278
279   updateUrl() {
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}`
284     );
285   }
286
287   fetchData() {
288     if (this.state.dataType == DataType.Post) {
289       let getPostsForm: GetPostsForm = {
290         page: this.state.page,
291         limit: fetchLimit,
292         sort: SortType[this.state.sort],
293         type_: ListingType[ListingType.Community],
294         community_id: this.state.community.id,
295       };
296       WebSocketService.Instance.getPosts(getPostsForm);
297     } else {
298       let getCommentsForm: GetCommentsForm = {
299         page: this.state.page,
300         limit: fetchLimit,
301         sort: SortType[this.state.sort],
302         type_: ListingType[ListingType.Community],
303         community_id: this.state.community.id,
304       };
305       WebSocketService.Instance.getComments(getCommentsForm);
306     }
307   }
308
309   parseMessage(msg: WebSocketJsonResponse) {
310     console.log(msg);
311     let res = wsJsonToRes(msg);
312     if (msg.error) {
313       toast(i18n.t(msg.error), 'danger');
314       this.context.router.history.push('/');
315       return;
316     } else if (msg.reconnect) {
317       this.fetchData();
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);
326       this.fetchData();
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;
360
361       this.state.posts
362         .filter(p => p.creator_id == data.user.id)
363         .forEach(p => (p.banned = data.banned));
364
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;
377
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);
382       }
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);
391     }
392   }
393 }