]> Untitled Git - lemmy-ui.git/blob - src/shared/components/community/communities.tsx
Removing monads. Fixes #884 (#886)
[lemmy-ui.git] / src / shared / components / community / communities.tsx
1 import { Component, linkEvent } from "inferno";
2 import {
3   CommunityResponse,
4   FollowCommunity,
5   GetSiteResponse,
6   ListCommunities,
7   ListCommunitiesResponse,
8   ListingType,
9   SortType,
10   SubscribedType,
11   UserOperation,
12   wsJsonToRes,
13   wsUserOp,
14 } from "lemmy-js-client";
15 import { Subscription } from "rxjs";
16 import { InitialFetchRequest } from "shared/interfaces";
17 import { i18n } from "../../i18next";
18 import { WebSocketService } from "../../services";
19 import {
20   getListingTypeFromPropsNoDefault,
21   getPageFromProps,
22   isBrowser,
23   myAuth,
24   numToSI,
25   setIsoData,
26   showLocal,
27   toast,
28   wsClient,
29   wsSubscribe,
30 } from "../../utils";
31 import { HtmlTags } from "../common/html-tags";
32 import { Spinner } from "../common/icon";
33 import { ListingTypeSelect } from "../common/listing-type-select";
34 import { Paginator } from "../common/paginator";
35 import { CommunityLink } from "./community-link";
36
37 const communityLimit = 50;
38
39 interface CommunitiesState {
40   listCommunitiesResponse?: ListCommunitiesResponse;
41   page: number;
42   loading: boolean;
43   siteRes: GetSiteResponse;
44   searchText: string;
45   listingType: ListingType;
46 }
47
48 interface CommunitiesProps {
49   listingType?: ListingType;
50   page?: number;
51 }
52
53 export class Communities extends Component<any, CommunitiesState> {
54   private subscription?: Subscription;
55   private isoData = setIsoData(this.context);
56   state: CommunitiesState = {
57     loading: true,
58     page: getPageFromProps(this.props),
59     listingType: getListingTypeFromPropsNoDefault(this.props),
60     siteRes: this.isoData.site_res,
61     searchText: "",
62   };
63
64   constructor(props: any, context: any) {
65     super(props, context);
66     this.handlePageChange = this.handlePageChange.bind(this);
67     this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
68
69     this.parseMessage = this.parseMessage.bind(this);
70     this.subscription = wsSubscribe(this.parseMessage);
71
72     // Only fetch the data if coming from another route
73     if (this.isoData.path == this.context.router.route.match.url) {
74       let listRes = this.isoData.routeData[0] as ListCommunitiesResponse;
75       this.state = {
76         ...this.state,
77         listCommunitiesResponse: listRes,
78         loading: false,
79       };
80     } else {
81       this.refetch();
82     }
83   }
84
85   componentWillUnmount() {
86     if (isBrowser()) {
87       this.subscription?.unsubscribe();
88     }
89   }
90
91   static getDerivedStateFromProps(props: any): CommunitiesProps {
92     return {
93       listingType: getListingTypeFromPropsNoDefault(props),
94       page: getPageFromProps(props),
95     };
96   }
97
98   componentDidUpdate(_: any, lastState: CommunitiesState) {
99     if (
100       lastState.page !== this.state.page ||
101       lastState.listingType !== this.state.listingType
102     ) {
103       this.setState({ loading: true });
104       this.refetch();
105     }
106   }
107
108   get documentTitle(): string {
109     return `${i18n.t("communities")} - ${
110       this.state.siteRes.site_view.site.name
111     }`;
112   }
113
114   render() {
115     return (
116       <div className="container-lg">
117         <HtmlTags
118           title={this.documentTitle}
119           path={this.context.router.route.match.url}
120         />
121         {this.state.loading ? (
122           <h5>
123             <Spinner large />
124           </h5>
125         ) : (
126           <div>
127             <div className="row">
128               <div className="col-md-6">
129                 <h4>{i18n.t("list_of_communities")}</h4>
130                 <span className="mb-2">
131                   <ListingTypeSelect
132                     type_={this.state.listingType}
133                     showLocal={showLocal(this.isoData)}
134                     showSubscribed
135                     onChange={this.handleListingTypeChange}
136                   />
137                 </span>
138               </div>
139               <div className="col-md-6">
140                 <div className="float-md-right">{this.searchForm()}</div>
141               </div>
142             </div>
143
144             <div className="table-responsive">
145               <table
146                 id="community_table"
147                 className="table table-sm table-hover"
148               >
149                 <thead className="pointer">
150                   <tr>
151                     <th>{i18n.t("name")}</th>
152                     <th className="text-right">{i18n.t("subscribers")}</th>
153                     <th className="text-right">
154                       {i18n.t("users")} / {i18n.t("month")}
155                     </th>
156                     <th className="text-right d-none d-lg-table-cell">
157                       {i18n.t("posts")}
158                     </th>
159                     <th className="text-right d-none d-lg-table-cell">
160                       {i18n.t("comments")}
161                     </th>
162                     <th></th>
163                   </tr>
164                 </thead>
165                 <tbody>
166                   {this.state.listCommunitiesResponse?.communities.map(cv => (
167                     <tr key={cv.community.id}>
168                       <td>
169                         <CommunityLink community={cv.community} />
170                       </td>
171                       <td className="text-right">
172                         {numToSI(cv.counts.subscribers)}
173                       </td>
174                       <td className="text-right">
175                         {numToSI(cv.counts.users_active_month)}
176                       </td>
177                       <td className="text-right d-none d-lg-table-cell">
178                         {numToSI(cv.counts.posts)}
179                       </td>
180                       <td className="text-right d-none d-lg-table-cell">
181                         {numToSI(cv.counts.comments)}
182                       </td>
183                       <td className="text-right">
184                         {cv.subscribed == SubscribedType.Subscribed && (
185                           <button
186                             className="btn btn-link d-inline-block"
187                             onClick={linkEvent(
188                               cv.community.id,
189                               this.handleUnsubscribe
190                             )}
191                           >
192                             {i18n.t("unsubscribe")}
193                           </button>
194                         )}
195                         {cv.subscribed == SubscribedType.NotSubscribed && (
196                           <button
197                             className="btn btn-link d-inline-block"
198                             onClick={linkEvent(
199                               cv.community.id,
200                               this.handleSubscribe
201                             )}
202                           >
203                             {i18n.t("subscribe")}
204                           </button>
205                         )}
206                         {cv.subscribed == SubscribedType.Pending && (
207                           <div className="text-warning d-inline-block">
208                             {i18n.t("subscribe_pending")}
209                           </div>
210                         )}
211                       </td>
212                     </tr>
213                   ))}
214                 </tbody>
215               </table>
216             </div>
217             <Paginator
218               page={this.state.page}
219               onChange={this.handlePageChange}
220             />
221           </div>
222         )}
223       </div>
224     );
225   }
226
227   searchForm() {
228     return (
229       <form
230         className="form-inline"
231         onSubmit={linkEvent(this, this.handleSearchSubmit)}
232       >
233         <input
234           type="text"
235           id="communities-search"
236           className="form-control mr-2 mb-2"
237           value={this.state.searchText}
238           placeholder={`${i18n.t("search")}...`}
239           onInput={linkEvent(this, this.handleSearchChange)}
240           required
241           minLength={3}
242         />
243         <label className="sr-only" htmlFor="communities-search">
244           {i18n.t("search")}
245         </label>
246         <button type="submit" className="btn btn-secondary mr-2 mb-2">
247           <span>{i18n.t("search")}</span>
248         </button>
249       </form>
250     );
251   }
252
253   updateUrl(paramUpdates: CommunitiesProps) {
254     const page = paramUpdates.page || this.state.page;
255     const listingTypeStr = paramUpdates.listingType || this.state.listingType;
256     this.props.history.push(
257       `/communities/listing_type/${listingTypeStr}/page/${page}`
258     );
259   }
260
261   handlePageChange(page: number) {
262     this.updateUrl({ page });
263   }
264
265   handleListingTypeChange(val: ListingType) {
266     this.updateUrl({
267       listingType: val,
268       page: 1,
269     });
270   }
271
272   handleUnsubscribe(communityId: number) {
273     let auth = myAuth();
274     if (auth) {
275       let form: FollowCommunity = {
276         community_id: communityId,
277         follow: false,
278         auth,
279       };
280       WebSocketService.Instance.send(wsClient.followCommunity(form));
281     }
282   }
283
284   handleSubscribe(communityId: number) {
285     let auth = myAuth();
286     if (auth) {
287       let form: FollowCommunity = {
288         community_id: communityId,
289         follow: true,
290         auth,
291       };
292       WebSocketService.Instance.send(wsClient.followCommunity(form));
293     }
294   }
295
296   handleSearchChange(i: Communities, event: any) {
297     i.setState({ searchText: event.target.value });
298   }
299
300   handleSearchSubmit(i: Communities) {
301     const searchParamEncoded = encodeURIComponent(i.state.searchText);
302     i.context.router.history.push(
303       `/search/q/${searchParamEncoded}/type/Communities/sort/TopAll/listing_type/All/community_id/0/creator_id/0/page/1`
304     );
305   }
306
307   refetch() {
308     let listCommunitiesForm: ListCommunities = {
309       type_: this.state.listingType,
310       sort: SortType.TopMonth,
311       limit: communityLimit,
312       page: this.state.page,
313       auth: myAuth(false),
314     };
315
316     WebSocketService.Instance.send(
317       wsClient.listCommunities(listCommunitiesForm)
318     );
319   }
320
321   static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
322     let pathSplit = req.path.split("/");
323     let type_: ListingType = pathSplit[3]
324       ? ListingType[pathSplit[3]]
325       : ListingType.Local;
326     let page = pathSplit[5] ? Number(pathSplit[5]) : 1;
327     let listCommunitiesForm: ListCommunities = {
328       type_,
329       sort: SortType.TopMonth,
330       limit: communityLimit,
331       page,
332       auth: req.auth,
333     };
334
335     return [req.client.listCommunities(listCommunitiesForm)];
336   }
337
338   parseMessage(msg: any) {
339     let op = wsUserOp(msg);
340     console.log(msg);
341     if (msg.error) {
342       toast(i18n.t(msg.error), "danger");
343       return;
344     } else if (op == UserOperation.ListCommunities) {
345       let data = wsJsonToRes<ListCommunitiesResponse>(msg);
346       this.setState({ listCommunitiesResponse: data, loading: false });
347       window.scrollTo(0, 0);
348     } else if (op == UserOperation.FollowCommunity) {
349       let data = wsJsonToRes<CommunityResponse>(msg);
350       let res = this.state.listCommunitiesResponse;
351       let found = res?.communities.find(
352         c => c.community.id == data.community_view.community.id
353       );
354       if (found) {
355         found.subscribed = data.community_view.subscribed;
356         found.counts.subscribers = data.community_view.counts.subscribers;
357         this.setState(this.state);
358       }
359     }
360   }
361 }