1 import { Component, linkEvent } from "inferno";
6 ListCommunitiesResponse,
8 } from "lemmy-js-client";
9 import { i18n } from "../../i18next";
10 import { InitialFetchRequest } from "../../interfaces";
11 import { FirstLoadService } from "../../services/FirstLoadService";
12 import { HttpService, RequestState } from "../../services/HttpService";
23 import { getQueryParams } from "../../utils/helpers/get-query-params";
24 import { getQueryString } from "../../utils/helpers/get-query-string";
25 import type { QueryParams } from "../../utils/types/query-params";
26 import { HtmlTags } from "../common/html-tags";
27 import { Spinner } from "../common/icon";
28 import { ListingTypeSelect } from "../common/listing-type-select";
29 import { Paginator } from "../common/paginator";
30 import { CommunityLink } from "./community-link";
32 const communityLimit = 50;
34 type CommunitiesData = RouteDataResponse<{
35 listCommunitiesResponse: ListCommunitiesResponse;
38 interface CommunitiesState {
39 listCommunitiesResponse: RequestState<ListCommunitiesResponse>;
40 siteRes: GetSiteResponse;
42 isIsomorphic: boolean;
45 interface CommunitiesProps {
46 listingType: ListingType;
50 function getListingTypeFromQuery(listingType?: string): ListingType {
51 return listingType ? (listingType as ListingType) : "Local";
54 export class Communities extends Component<any, CommunitiesState> {
55 private isoData = setIsoData<CommunitiesData>(this.context);
56 state: CommunitiesState = {
57 listCommunitiesResponse: { state: "empty" },
58 siteRes: this.isoData.site_res,
63 constructor(props: any, context: any) {
64 super(props, context);
65 this.handlePageChange = this.handlePageChange.bind(this);
66 this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
68 // Only fetch the data if coming from another route
69 if (FirstLoadService.isFirstLoad) {
70 const { listCommunitiesResponse } = this.isoData.routeData;
74 listCommunitiesResponse,
80 async componentDidMount() {
81 if (!this.state.isIsomorphic) {
86 get documentTitle(): string {
87 return `${i18n.t("communities")} - ${
88 this.state.siteRes.site_view.site.name
93 switch (this.state.listCommunitiesResponse.state) {
101 const { listingType, page } = this.getCommunitiesQueryParams();
104 <div className="row">
105 <div className="col-md-6">
106 <h4>{i18n.t("list_of_communities")}</h4>
107 <span className="mb-2">
110 showLocal={showLocal(this.isoData)}
112 onChange={this.handleListingTypeChange}
116 <div className="col-md-6">
117 <div className="float-md-right">{this.searchForm()}</div>
121 <div className="table-responsive">
124 className="table table-sm table-hover"
126 <thead className="pointer">
128 <th>{i18n.t("name")}</th>
129 <th className="text-right">{i18n.t("subscribers")}</th>
130 <th className="text-right">
131 {i18n.t("users")} / {i18n.t("month")}
133 <th className="text-right d-none d-lg-table-cell">
136 <th className="text-right d-none d-lg-table-cell">
143 {this.state.listCommunitiesResponse.data.communities.map(
145 <tr key={cv.community.id}>
147 <CommunityLink community={cv.community} />
149 <td className="text-right">
150 {numToSI(cv.counts.subscribers)}
152 <td className="text-right">
153 {numToSI(cv.counts.users_active_month)}
155 <td className="text-right d-none d-lg-table-cell">
156 {numToSI(cv.counts.posts)}
158 <td className="text-right d-none d-lg-table-cell">
159 {numToSI(cv.counts.comments)}
161 <td className="text-right">
162 {cv.subscribed == "Subscribed" && (
164 className="btn btn-link d-inline-block"
168 communityId: cv.community.id,
174 {i18n.t("unsubscribe")}
177 {cv.subscribed === "NotSubscribed" && (
179 className="btn btn-link d-inline-block"
183 communityId: cv.community.id,
189 {i18n.t("subscribe")}
192 {cv.subscribed === "Pending" && (
193 <div className="text-warning d-inline-block">
194 {i18n.t("subscribe_pending")}
204 <Paginator page={page} onChange={this.handlePageChange} />
213 <div className="container-lg">
215 title={this.documentTitle}
216 path={this.context.router.route.match.url}
218 {this.renderListings()}
226 className="form-inline"
227 onSubmit={linkEvent(this, this.handleSearchSubmit)}
231 id="communities-search"
232 className="form-control mr-2 mb-2"
233 value={this.state.searchText}
234 placeholder={`${i18n.t("search")}...`}
235 onInput={linkEvent(this, this.handleSearchChange)}
239 <label className="sr-only" htmlFor="communities-search">
242 <button type="submit" className="btn btn-secondary mr-2 mb-2">
243 <span>{i18n.t("search")}</span>
249 async updateUrl({ listingType, page }: Partial<CommunitiesProps>) {
250 const { listingType: urlListingType, page: urlPage } =
251 this.getCommunitiesQueryParams();
253 const queryParams: QueryParams<CommunitiesProps> = {
254 listingType: listingType ?? urlListingType,
255 page: (page ?? urlPage)?.toString(),
258 this.props.history.push(`/communities${getQueryString(queryParams)}`);
260 await this.refetch();
263 handlePageChange(page: number) {
264 this.updateUrl({ page });
267 handleListingTypeChange(val: ListingType) {
274 handleSearchChange(i: Communities, event: any) {
275 i.setState({ searchText: event.target.value });
278 handleSearchSubmit(i: Communities, event: any) {
279 event.preventDefault();
280 const searchParamEncoded = encodeURIComponent(i.state.searchText);
281 i.context.router.history.push(`/search?q=${searchParamEncoded}`);
284 static async fetchInitialData({
285 query: { listingType, page },
288 }: InitialFetchRequest<
289 QueryParams<CommunitiesProps>
290 >): Promise<CommunitiesData> {
291 const listCommunitiesForm: ListCommunities = {
292 type_: getListingTypeFromQuery(listingType),
294 limit: communityLimit,
295 page: getPageFromString(page),
300 listCommunitiesResponse: await client.listCommunities(
306 getCommunitiesQueryParams() {
307 return getQueryParams<CommunitiesProps>({
308 listingType: getListingTypeFromQuery,
309 page: getPageFromString,
313 async handleFollow(data: {
318 const res = await HttpService.client.followCommunity({
319 community_id: data.communityId,
321 auth: myAuthRequired(),
323 data.i.findAndUpdateCommunity(res);
327 this.setState({ listCommunitiesResponse: { state: "loading" } });
329 const { listingType, page } = this.getCommunitiesQueryParams();
332 listCommunitiesResponse: await HttpService.client.listCommunities({
335 limit: communityLimit,
341 window.scrollTo(0, 0);
344 findAndUpdateCommunity(res: RequestState<CommunityResponse>) {
347 s.listCommunitiesResponse.state == "success" &&
348 res.state == "success"
350 s.listCommunitiesResponse.data.communities = editCommunity(
351 res.data.community_view,
352 s.listCommunitiesResponse.data.communities