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";
22 import { getQueryParams } from "../../utils/helpers/get-query-params";
23 import { getQueryString } from "../../utils/helpers/get-query-string";
24 import type { QueryParams } from "../../utils/types/query-params";
25 import { HtmlTags } from "../common/html-tags";
26 import { Spinner } from "../common/icon";
27 import { ListingTypeSelect } from "../common/listing-type-select";
28 import { Paginator } from "../common/paginator";
29 import { CommunityLink } from "./community-link";
31 const communityLimit = 50;
33 type CommunitiesData = RouteDataResponse<{
34 listCommunitiesResponse: ListCommunitiesResponse;
37 interface CommunitiesState {
38 listCommunitiesResponse: RequestState<ListCommunitiesResponse>;
39 siteRes: GetSiteResponse;
41 isIsomorphic: boolean;
44 interface CommunitiesProps {
45 listingType: ListingType;
49 function getListingTypeFromQuery(listingType?: string): ListingType {
50 return listingType ? (listingType as ListingType) : "Local";
53 export class Communities extends Component<any, CommunitiesState> {
54 private isoData = setIsoData<CommunitiesData>(this.context);
55 state: CommunitiesState = {
56 listCommunitiesResponse: { state: "empty" },
57 siteRes: this.isoData.site_res,
62 constructor(props: any, context: any) {
63 super(props, context);
64 this.handlePageChange = this.handlePageChange.bind(this);
65 this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
67 // Only fetch the data if coming from another route
68 if (FirstLoadService.isFirstLoad) {
69 const { listCommunitiesResponse } = this.isoData.routeData;
73 listCommunitiesResponse,
79 async componentDidMount() {
80 if (!this.state.isIsomorphic) {
85 get documentTitle(): string {
86 return `${i18n.t("communities")} - ${
87 this.state.siteRes.site_view.site.name
92 switch (this.state.listCommunitiesResponse.state) {
100 const { listingType, page } = this.getCommunitiesQueryParams();
103 <div className="row">
104 <div className="col-md-6">
105 <h4>{i18n.t("list_of_communities")}</h4>
106 <span className="mb-2">
109 showLocal={showLocal(this.isoData)}
111 onChange={this.handleListingTypeChange}
115 <div className="col-md-6">
116 <div className="float-md-right">{this.searchForm()}</div>
120 <div className="table-responsive">
123 className="table table-sm table-hover"
125 <thead className="pointer">
127 <th>{i18n.t("name")}</th>
128 <th className="text-right">{i18n.t("subscribers")}</th>
129 <th className="text-right">
130 {i18n.t("users")} / {i18n.t("month")}
132 <th className="text-right d-none d-lg-table-cell">
135 <th className="text-right d-none d-lg-table-cell">
142 {this.state.listCommunitiesResponse.data.communities.map(
144 <tr key={cv.community.id}>
146 <CommunityLink community={cv.community} />
148 <td className="text-right">
149 {numToSI(cv.counts.subscribers)}
151 <td className="text-right">
152 {numToSI(cv.counts.users_active_month)}
154 <td className="text-right d-none d-lg-table-cell">
155 {numToSI(cv.counts.posts)}
157 <td className="text-right d-none d-lg-table-cell">
158 {numToSI(cv.counts.comments)}
160 <td className="text-right">
161 {cv.subscribed == "Subscribed" && (
163 className="btn btn-link d-inline-block"
167 communityId: cv.community.id,
173 {i18n.t("unsubscribe")}
176 {cv.subscribed === "NotSubscribed" && (
178 className="btn btn-link d-inline-block"
182 communityId: cv.community.id,
188 {i18n.t("subscribe")}
191 {cv.subscribed === "Pending" && (
192 <div className="text-warning d-inline-block">
193 {i18n.t("subscribe_pending")}
203 <Paginator page={page} onChange={this.handlePageChange} />
212 <div className="container-lg">
214 title={this.documentTitle}
215 path={this.context.router.route.match.url}
217 {this.renderListings()}
225 className="form-inline"
226 onSubmit={linkEvent(this, this.handleSearchSubmit)}
230 id="communities-search"
231 className="form-control mr-2 mb-2"
232 value={this.state.searchText}
233 placeholder={`${i18n.t("search")}...`}
234 onInput={linkEvent(this, this.handleSearchChange)}
238 <label className="sr-only" htmlFor="communities-search">
241 <button type="submit" className="btn btn-secondary mr-2 mb-2">
242 <span>{i18n.t("search")}</span>
248 async updateUrl({ listingType, page }: Partial<CommunitiesProps>) {
249 const { listingType: urlListingType, page: urlPage } =
250 this.getCommunitiesQueryParams();
252 const queryParams: QueryParams<CommunitiesProps> = {
253 listingType: listingType ?? urlListingType,
254 page: (page ?? urlPage)?.toString(),
257 this.props.history.push(`/communities${getQueryString(queryParams)}`);
259 await this.refetch();
262 handlePageChange(page: number) {
263 this.updateUrl({ page });
266 handleListingTypeChange(val: ListingType) {
273 handleSearchChange(i: Communities, event: any) {
274 i.setState({ searchText: event.target.value });
277 handleSearchSubmit(i: Communities, event: any) {
278 event.preventDefault();
279 const searchParamEncoded = encodeURIComponent(i.state.searchText);
280 i.context.router.history.push(`/search?q=${searchParamEncoded}`);
283 static async fetchInitialData({
284 query: { listingType, page },
287 }: InitialFetchRequest<
288 QueryParams<CommunitiesProps>
289 >): Promise<CommunitiesData> {
290 const listCommunitiesForm: ListCommunities = {
291 type_: getListingTypeFromQuery(listingType),
293 limit: communityLimit,
294 page: getPageFromString(page),
299 listCommunitiesResponse: await client.listCommunities(
305 getCommunitiesQueryParams() {
306 return getQueryParams<CommunitiesProps>({
307 listingType: getListingTypeFromQuery,
308 page: getPageFromString,
312 async handleFollow(data: {
317 const res = await HttpService.client.followCommunity({
318 community_id: data.communityId,
320 auth: myAuthRequired(),
322 data.i.findAndUpdateCommunity(res);
326 this.setState({ listCommunitiesResponse: { state: "loading" } });
328 const { listingType, page } = this.getCommunitiesQueryParams();
331 listCommunitiesResponse: await HttpService.client.listCommunities({
334 limit: communityLimit,
340 window.scrollTo(0, 0);
343 findAndUpdateCommunity(res: RequestState<CommunityResponse>) {
346 s.listCommunitiesResponse.state == "success" &&
347 res.state == "success"
349 s.listCommunitiesResponse.data.communities = editCommunity(
350 res.data.community_view,
351 s.listCommunitiesResponse.data.communities