1 import { Component, linkEvent } from "inferno";
7 ListCommunitiesResponse,
12 } from "lemmy-js-client";
13 import { Subscription } from "rxjs";
14 import { InitialFetchRequest } from "shared/interfaces";
15 import { i18n } from "../../i18next";
16 import { WebSocketService } from "../../services";
19 getListingTypeFromPropsNoDefault,
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";
37 const communityLimit = 100;
39 interface CommunitiesState {
40 communities: CommunityView[];
45 listingType: ListingType;
48 interface CommunitiesProps {
49 listingType?: ListingType;
53 export class Communities extends Component<any, CommunitiesState> {
54 private subscription: Subscription;
55 private isoData = setIsoData(this.context);
56 private emptyState: CommunitiesState = {
59 page: getPageFromProps(this.props),
60 listingType: getListingTypeFromPropsNoDefault(this.props),
61 site_view: this.isoData.site_res.site_view,
65 constructor(props: any, context: any) {
66 super(props, context);
67 this.state = this.emptyState;
68 this.handlePageChange = this.handlePageChange.bind(this);
69 this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
71 this.parseMessage = this.parseMessage.bind(this);
72 this.subscription = wsSubscribe(this.parseMessage);
74 // Only fetch the data if coming from another route
75 if (this.isoData.path == this.context.router.route.match.url) {
76 this.state.communities = this.isoData.routeData[0].communities;
77 this.state.loading = false;
83 componentWillUnmount() {
85 this.subscription.unsubscribe();
89 static getDerivedStateFromProps(props: any): CommunitiesProps {
91 listingType: getListingTypeFromPropsNoDefault(props),
92 page: getPageFromProps(props),
96 componentDidUpdate(_: any, lastState: CommunitiesState) {
98 lastState.page !== this.state.page ||
99 lastState.listingType !== this.state.listingType
101 this.setState({ loading: true });
106 get documentTitle(): string {
107 return `${i18n.t("communities")} - ${this.state.site_view.site.name}`;
112 <div class="container">
114 title={this.documentTitle}
115 path={this.context.router.route.match.url}
117 {this.state.loading ? (
124 <div class="col-md-6">
125 <h4>{i18n.t("list_of_communities")}</h4>
128 type_={this.state.listingType}
129 showLocal={showLocal(this.isoData)}
130 onChange={this.handleListingTypeChange}
134 <div class="col-md-6">
135 <div class="float-md-right">{this.searchForm()}</div>
139 <div class="table-responsive">
140 <table id="community_table" class="table table-sm table-hover">
141 <thead class="pointer">
143 <th>{i18n.t("name")}</th>
144 <th class="text-right">{i18n.t("subscribers")}</th>
145 <th class="text-right">
146 {i18n.t("users")} / {i18n.t("month")}
148 <th class="text-right d-none d-lg-table-cell">
151 <th class="text-right d-none d-lg-table-cell">
158 {this.state.communities.map(cv => (
161 <CommunityLink community={cv.community} />
163 <td class="text-right">{cv.counts.subscribers}</td>
164 <td class="text-right">{cv.counts.users_active_month}</td>
165 <td class="text-right d-none d-lg-table-cell">
168 <td class="text-right d-none d-lg-table-cell">
171 <td class="text-right">
174 class="btn btn-link d-inline-block"
177 this.handleUnsubscribe
180 {i18n.t("unsubscribe")}
184 class="btn btn-link d-inline-block"
190 {i18n.t("subscribe")}
200 page={this.state.page}
201 onChange={this.handlePageChange}
213 onSubmit={linkEvent(this, this.handleSearchSubmit)}
217 id="communities-search"
218 class="form-control mr-2 mb-2"
219 value={this.state.searchText}
220 placeholder={`${i18n.t("search")}...`}
221 onInput={linkEvent(this, this.handleSearchChange)}
225 <label class="sr-only" htmlFor="communities-search">
228 <button type="submit" class="btn btn-secondary mr-2 mb-2">
229 <span>{i18n.t("search")}</span>
235 updateUrl(paramUpdates: CommunitiesProps) {
236 const page = paramUpdates.page || this.state.page;
237 const listingTypeStr = paramUpdates.listingType || this.state.listingType;
238 this.props.history.push(
239 `/communities/listing_type/${listingTypeStr}/page/${page}`
243 handlePageChange(page: number) {
244 this.updateUrl({ page });
247 handleListingTypeChange(val: ListingType) {
254 handleUnsubscribe(communityId: number) {
255 let form: FollowCommunity = {
256 community_id: communityId,
260 WebSocketService.Instance.send(wsClient.followCommunity(form));
263 handleSubscribe(communityId: number) {
264 let form: FollowCommunity = {
265 community_id: communityId,
269 WebSocketService.Instance.send(wsClient.followCommunity(form));
272 handleSearchChange(i: Communities, event: any) {
273 i.setState({ searchText: event.target.value });
276 handleSearchSubmit(i: Communities) {
277 const searchParamEncoded = encodeURIComponent(i.state.searchText);
278 i.context.router.history.push(
279 `/search/q/${searchParamEncoded}/type/Communities/sort/TopAll/listing_type/All/community_id/0/creator_id/0/page/1`
284 let listCommunitiesForm: ListCommunities = {
285 type_: this.state.listingType,
286 sort: SortType.TopMonth,
287 limit: communityLimit,
288 page: this.state.page,
289 auth: authField(false),
292 WebSocketService.Instance.send(
293 wsClient.listCommunities(listCommunitiesForm)
297 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
298 let pathSplit = req.path.split("/");
299 let type_: ListingType = pathSplit[3]
300 ? ListingType[pathSplit[3]]
302 let page = pathSplit[5] ? Number(pathSplit[5]) : 1;
303 let listCommunitiesForm: ListCommunities = {
305 sort: SortType.TopMonth,
306 limit: communityLimit,
309 setOptionalAuth(listCommunitiesForm, req.auth);
311 return [req.client.listCommunities(listCommunitiesForm)];
314 parseMessage(msg: any) {
315 let op = wsUserOp(msg);
318 toast(i18n.t(msg.error), "danger");
320 } else if (op == UserOperation.ListCommunities) {
321 let data = wsJsonToRes<ListCommunitiesResponse>(msg).data;
322 this.state.communities = data.communities;
323 this.state.loading = false;
324 window.scrollTo(0, 0);
325 this.setState(this.state);
326 } else if (op == UserOperation.FollowCommunity) {
327 let data = wsJsonToRes<CommunityResponse>(msg).data;
328 let found = this.state.communities.find(
329 c => c.community.id == data.community_view.community.id
331 found.subscribed = data.community_view.subscribed;
332 found.counts.subscribers = data.community_view.counts.subscribers;
333 this.setState(this.state);