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";
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 interface CommunitiesState {
34 listCommunitiesResponse: RequestState<ListCommunitiesResponse>;
35 siteRes: GetSiteResponse;
37 isIsomorphic: boolean;
40 interface CommunitiesProps {
41 listingType: ListingType;
45 function getListingTypeFromQuery(listingType?: string): ListingType {
46 return listingType ? (listingType as ListingType) : "Local";
49 export class Communities extends Component<any, CommunitiesState> {
50 private isoData = setIsoData(this.context);
51 state: CommunitiesState = {
52 listCommunitiesResponse: { state: "empty" },
53 siteRes: this.isoData.site_res,
58 constructor(props: any, context: any) {
59 super(props, context);
60 this.handlePageChange = this.handlePageChange.bind(this);
61 this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
63 // Only fetch the data if coming from another route
64 if (FirstLoadService.isFirstLoad) {
67 listCommunitiesResponse: this.isoData.routeData[0],
73 async componentDidMount() {
74 if (!this.state.isIsomorphic) {
79 get documentTitle(): string {
80 return `${i18n.t("communities")} - ${
81 this.state.siteRes.site_view.site.name
86 switch (this.state.listCommunitiesResponse.state) {
94 const { listingType, page } = this.getCommunitiesQueryParams();
98 <div className="col-md-6">
99 <h4>{i18n.t("list_of_communities")}</h4>
100 <span className="mb-2">
103 showLocal={showLocal(this.isoData)}
105 onChange={this.handleListingTypeChange}
109 <div className="col-md-6">
110 <div className="float-md-right">{this.searchForm()}</div>
114 <div className="table-responsive">
117 className="table table-sm table-hover"
119 <thead className="pointer">
121 <th>{i18n.t("name")}</th>
122 <th className="text-right">{i18n.t("subscribers")}</th>
123 <th className="text-right">
124 {i18n.t("users")} / {i18n.t("month")}
126 <th className="text-right d-none d-lg-table-cell">
129 <th className="text-right d-none d-lg-table-cell">
136 {this.state.listCommunitiesResponse.data.communities.map(
138 <tr key={cv.community.id}>
140 <CommunityLink community={cv.community} />
142 <td className="text-right">
143 {numToSI(cv.counts.subscribers)}
145 <td className="text-right">
146 {numToSI(cv.counts.users_active_month)}
148 <td className="text-right d-none d-lg-table-cell">
149 {numToSI(cv.counts.posts)}
151 <td className="text-right d-none d-lg-table-cell">
152 {numToSI(cv.counts.comments)}
154 <td className="text-right">
155 {cv.subscribed == "Subscribed" && (
157 className="btn btn-link d-inline-block"
161 communityId: cv.community.id,
167 {i18n.t("unsubscribe")}
170 {cv.subscribed === "NotSubscribed" && (
172 className="btn btn-link d-inline-block"
176 communityId: cv.community.id,
182 {i18n.t("subscribe")}
185 {cv.subscribed === "Pending" && (
186 <div className="text-warning d-inline-block">
187 {i18n.t("subscribe_pending")}
197 <Paginator page={page} onChange={this.handlePageChange} />
206 <div className="container-lg">
208 title={this.documentTitle}
209 path={this.context.router.route.match.url}
211 {this.renderListings()}
219 className="form-inline"
220 onSubmit={linkEvent(this, this.handleSearchSubmit)}
224 id="communities-search"
225 className="form-control mr-2 mb-2"
226 value={this.state.searchText}
227 placeholder={`${i18n.t("search")}...`}
228 onInput={linkEvent(this, this.handleSearchChange)}
232 <label className="sr-only" htmlFor="communities-search">
235 <button type="submit" className="btn btn-secondary mr-2 mb-2">
236 <span>{i18n.t("search")}</span>
242 async updateUrl({ listingType, page }: Partial<CommunitiesProps>) {
243 const { listingType: urlListingType, page: urlPage } =
244 this.getCommunitiesQueryParams();
246 const queryParams: QueryParams<CommunitiesProps> = {
247 listingType: listingType ?? urlListingType,
248 page: (page ?? urlPage)?.toString(),
251 this.props.history.push(`/communities${getQueryString(queryParams)}`);
253 await this.refetch();
256 handlePageChange(page: number) {
257 this.updateUrl({ page });
260 handleListingTypeChange(val: ListingType) {
267 handleSearchChange(i: Communities, event: any) {
268 i.setState({ searchText: event.target.value });
271 handleSearchSubmit(i: Communities, event: any) {
272 event.preventDefault();
273 const searchParamEncoded = encodeURIComponent(i.state.searchText);
274 i.context.router.history.push(`/search?q=${searchParamEncoded}`);
277 static fetchInitialData({
278 query: { listingType, page },
281 }: InitialFetchRequest<QueryParams<CommunitiesProps>>): Promise<
284 const listCommunitiesForm: ListCommunities = {
285 type_: getListingTypeFromQuery(listingType),
287 limit: communityLimit,
288 page: getPageFromString(page),
292 return [client.listCommunities(listCommunitiesForm)];
295 getCommunitiesQueryParams() {
296 return getQueryParams<CommunitiesProps>({
297 listingType: getListingTypeFromQuery,
298 page: getPageFromString,
302 async handleFollow(data: {
307 const res = await HttpService.client.followCommunity({
308 community_id: data.communityId,
310 auth: myAuthRequired(),
312 data.i.findAndUpdateCommunity(res);
316 this.setState({ listCommunitiesResponse: { state: "loading" } });
318 const { listingType, page } = this.getCommunitiesQueryParams();
321 listCommunitiesResponse: await HttpService.client.listCommunities({
324 limit: communityLimit,
330 window.scrollTo(0, 0);
333 findAndUpdateCommunity(res: RequestState<CommunityResponse>) {
336 s.listCommunitiesResponse.state == "success" &&
337 res.state == "success"
339 s.listCommunitiesResponse.data.communities = editCommunity(
340 res.data.community_view,
341 s.listCommunitiesResponse.data.communities