]> Untitled Git - lemmy.git/blob - ui/src/components/community.tsx
Add new comments views to main and community pages. Fixes #480
[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 } from '../utils';
41 import { i18n } from '../i18next';
42
43 interface State {
44   community: CommunityI;
45   communityId: number;
46   communityName: string;
47   moderators: Array<CommunityUser>;
48   admins: Array<UserView>;
49   online: number;
50   loading: boolean;
51   posts: Array<Post>;
52   comments: Array<Comment>;
53   dataType: DataType;
54   sort: SortType;
55   page: number;
56 }
57
58 export class Community extends Component<any, State> {
59   private subscription: Subscription;
60   private emptyState: State = {
61     community: {
62       id: null,
63       name: null,
64       title: null,
65       category_id: null,
66       category_name: null,
67       creator_id: null,
68       creator_name: null,
69       number_of_subscribers: null,
70       number_of_posts: null,
71       number_of_comments: null,
72       published: null,
73       removed: null,
74       nsfw: false,
75       deleted: null,
76     },
77     moderators: [],
78     admins: [],
79     communityId: Number(this.props.match.params.id),
80     communityName: this.props.match.params.name,
81     online: null,
82     loading: true,
83     posts: [],
84     comments: [],
85     dataType: getDataTypeFromProps(this.props),
86     sort: getSortTypeFromProps(this.props),
87     page: getPageFromProps(this.props),
88   };
89
90   constructor(props: any, context: any) {
91     super(props, context);
92
93     this.state = this.emptyState;
94     this.handleSortChange = this.handleSortChange.bind(this);
95     this.handleDataTypeChange = this.handleDataTypeChange.bind(this);
96
97     this.subscription = WebSocketService.Instance.subject
98       .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
99       .subscribe(
100         msg => this.parseMessage(msg),
101         err => console.error(err),
102         () => console.log('complete')
103       );
104
105     let form: GetCommunityForm = {
106       id: this.state.communityId ? this.state.communityId : null,
107       name: this.state.communityName ? this.state.communityName : null,
108     };
109     WebSocketService.Instance.getCommunity(form);
110   }
111
112   componentWillUnmount() {
113     this.subscription.unsubscribe();
114   }
115
116   // Necessary for back button for some reason
117   componentWillReceiveProps(nextProps: any) {
118     if (
119       nextProps.history.action == 'POP' ||
120       nextProps.history.action == 'PUSH'
121     ) {
122       this.state.dataType = getDataTypeFromProps(nextProps);
123       this.state.sort = getSortTypeFromProps(nextProps);
124       this.state.page = getPageFromProps(nextProps);
125       this.setState(this.state);
126       this.fetchData();
127     }
128   }
129
130   render() {
131     return (
132       <div class="container">
133         {this.state.loading ? (
134           <h5>
135             <svg class="icon icon-spinner spin">
136               <use xlinkHref="#icon-spinner"></use>
137             </svg>
138           </h5>
139         ) : (
140           <div class="row">
141             <div class="col-12 col-md-8">
142               <h5>
143                 {this.state.community.title}
144                 {this.state.community.removed && (
145                   <small className="ml-2 text-muted font-italic">
146                     {i18n.t('removed')}
147                   </small>
148                 )}
149                 {this.state.community.nsfw && (
150                   <small className="ml-2 text-muted font-italic">
151                     {i18n.t('nsfw')}
152                   </small>
153                 )}
154               </h5>
155               {this.selects()}
156               {this.listings()}
157               {this.paginator()}
158             </div>
159             <div class="col-12 col-md-4">
160               <Sidebar
161                 community={this.state.community}
162                 moderators={this.state.moderators}
163                 admins={this.state.admins}
164                 online={this.state.online}
165               />
166             </div>
167           </div>
168         )}
169       </div>
170     );
171   }
172
173   listings() {
174     return this.state.dataType == DataType.Post ? (
175       <PostListings posts={this.state.posts} removeDuplicates />
176     ) : (
177       this.state.comments.map(comment => (
178         <div class="row">
179           <div class="col-12">
180             <CommentNodes nodes={[{ comment: comment }]} noIndent />
181           </div>
182         </div>
183       ))
184     );
185   }
186
187   selects() {
188     return (
189       <div class="mb-2">
190         <DataTypeSelect
191           type_={this.state.dataType}
192           onChange={this.handleDataTypeChange}
193         />
194
195         <span class="mx-2">
196           <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
197         </span>
198         <a
199           href={`/feeds/c/${this.state.communityName}.xml?sort=${
200             SortType[this.state.sort]
201           }`}
202           target="_blank"
203         >
204           <svg class="icon mx-2 text-muted small">
205             <use xlinkHref="#icon-rss">#</use>
206           </svg>
207         </a>
208       </div>
209     );
210   }
211
212   paginator() {
213     return (
214       <div class="my-2">
215         {this.state.page > 1 && (
216           <button
217             class="btn btn-sm btn-secondary mr-1"
218             onClick={linkEvent(this, this.prevPage)}
219           >
220             {i18n.t('prev')}
221           </button>
222         )}
223         {this.state.posts.length == fetchLimit && (
224           <button
225             class="btn btn-sm btn-secondary"
226             onClick={linkEvent(this, this.nextPage)}
227           >
228             {i18n.t('next')}
229           </button>
230         )}
231       </div>
232     );
233   }
234
235   nextPage(i: Community) {
236     i.state.page++;
237     i.setState(i.state);
238     i.updateUrl();
239     i.fetchData();
240     window.scrollTo(0, 0);
241   }
242
243   prevPage(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   handleSortChange(val: SortType) {
252     this.state.sort = val;
253     this.state.page = 1;
254     this.state.loading = true;
255     this.setState(this.state);
256     this.updateUrl();
257     this.fetchData();
258     window.scrollTo(0, 0);
259   }
260
261   handleDataTypeChange(val: DataType) {
262     this.state.dataType = val;
263     this.state.page = 1;
264     this.state.loading = true;
265     this.setState(this.state);
266     this.updateUrl();
267     this.fetchData();
268     window.scrollTo(0, 0);
269   }
270
271   updateUrl() {
272     let dataTypeStr = DataType[this.state.dataType].toLowerCase();
273     let sortStr = SortType[this.state.sort].toLowerCase();
274     this.props.history.push(
275       `/c/${this.state.community.name}/data_type/${dataTypeStr}/sort/${sortStr}/page/${this.state.page}`
276     );
277   }
278
279   fetchData() {
280     if (this.state.dataType == DataType.Post) {
281       let getPostsForm: GetPostsForm = {
282         page: this.state.page,
283         limit: fetchLimit,
284         sort: SortType[this.state.sort],
285         type_: ListingType[ListingType.Community],
286         community_id: this.state.community.id,
287       };
288       WebSocketService.Instance.getPosts(getPostsForm);
289     } else {
290       let getCommentsForm: GetCommentsForm = {
291         page: this.state.page,
292         limit: fetchLimit,
293         sort: SortType[this.state.sort],
294         type_: ListingType[ListingType.Community],
295         community_id: this.state.community.id,
296       };
297       WebSocketService.Instance.getComments(getCommentsForm);
298     }
299   }
300
301   parseMessage(msg: WebSocketJsonResponse) {
302     console.log(msg);
303     let res = wsJsonToRes(msg);
304     if (msg.error) {
305       toast(i18n.t(msg.error), 'danger');
306       this.context.router.history.push('/');
307       return;
308     } else if (msg.reconnect) {
309       this.fetchData();
310     } else if (res.op == UserOperation.GetCommunity) {
311       let data = res.data as GetCommunityResponse;
312       this.state.community = data.community;
313       this.state.moderators = data.moderators;
314       this.state.admins = data.admins;
315       this.state.online = data.online;
316       document.title = `/c/${this.state.community.name} - ${WebSocketService.Instance.site.name}`;
317       this.setState(this.state);
318       this.fetchData();
319     } else if (res.op == UserOperation.EditCommunity) {
320       let data = res.data as CommunityResponse;
321       this.state.community = data.community;
322       this.setState(this.state);
323     } else if (res.op == UserOperation.FollowCommunity) {
324       let data = res.data as CommunityResponse;
325       this.state.community.subscribed = data.community.subscribed;
326       this.state.community.number_of_subscribers =
327         data.community.number_of_subscribers;
328       this.setState(this.state);
329     } else if (res.op == UserOperation.GetPosts) {
330       let data = res.data as GetPostsResponse;
331       this.state.posts = data.posts;
332       this.state.loading = false;
333       this.setState(this.state);
334     } else if (res.op == UserOperation.EditPost) {
335       let data = res.data as PostResponse;
336       let found = this.state.posts.find(c => c.id == data.post.id);
337       if (found) {
338         found.url = data.post.url;
339         found.name = data.post.name;
340         found.nsfw = data.post.nsfw;
341         this.setState(this.state);
342       }
343     } else if (res.op == UserOperation.CreatePost) {
344       let data = res.data as PostResponse;
345       this.state.posts.unshift(data.post);
346       this.setState(this.state);
347     } else if (res.op == UserOperation.CreatePostLike) {
348       let data = res.data as PostResponse;
349       let found = this.state.posts.find(c => c.id == data.post.id);
350       if (found) {
351         found.score = data.post.score;
352         found.upvotes = data.post.upvotes;
353         found.downvotes = data.post.downvotes;
354         if (data.post.my_vote !== null) {
355           found.my_vote = data.post.my_vote;
356           found.upvoteLoading = false;
357           found.downvoteLoading = false;
358         }
359       }
360       this.setState(this.state);
361     } else if (res.op == UserOperation.AddModToCommunity) {
362       let data = res.data as AddModToCommunityResponse;
363       this.state.moderators = data.moderators;
364       this.setState(this.state);
365     } else if (res.op == UserOperation.BanFromCommunity) {
366       let data = res.data as BanFromCommunityResponse;
367
368       this.state.posts
369         .filter(p => p.creator_id == data.user.id)
370         .forEach(p => (p.banned = data.banned));
371
372       this.setState(this.state);
373     } else if (res.op == UserOperation.GetComments) {
374       let data = res.data as GetCommentsResponse;
375       this.state.comments = data.comments;
376       this.state.loading = false;
377       this.setState(this.state);
378     } else if (res.op == UserOperation.EditComment) {
379       let data = res.data as CommentResponse;
380
381       let found = this.state.comments.find(c => c.id == data.comment.id);
382       if (found) {
383         found.content = data.comment.content;
384         found.updated = data.comment.updated;
385         found.removed = data.comment.removed;
386         found.deleted = data.comment.deleted;
387         found.upvotes = data.comment.upvotes;
388         found.downvotes = data.comment.downvotes;
389         found.score = data.comment.score;
390         this.setState(this.state);
391       }
392     } else if (res.op == UserOperation.CreateComment) {
393       let data = res.data as CommentResponse;
394
395       // Necessary since it might be a user reply
396       if (data.recipient_ids.length == 0) {
397         this.state.comments.unshift(data.comment);
398         this.setState(this.state);
399       }
400     } else if (res.op == UserOperation.SaveComment) {
401       let data = res.data as CommentResponse;
402       let found = this.state.comments.find(c => c.id == data.comment.id);
403       found.saved = data.comment.saved;
404       this.setState(this.state);
405     } else if (res.op == UserOperation.CreateCommentLike) {
406       let data = res.data as CommentResponse;
407       let found: Comment = this.state.comments.find(
408         c => c.id === data.comment.id
409       );
410       found.score = data.comment.score;
411       found.upvotes = data.comment.upvotes;
412       found.downvotes = data.comment.downvotes;
413       if (data.comment.my_vote !== null) found.my_vote = data.comment.my_vote;
414       this.setState(this.state);
415     }
416   }
417 }