1 import { Component, linkEvent } from 'inferno';
2 import { HtmlTags } from './html-tags';
3 import { Subscription } from 'rxjs';
7 ListCommunitiesResponse,
13 } from 'lemmy-js-client';
14 import { WebSocketService } from '../services';
27 import { CommunityLink } from './community-link';
28 import { i18n } from '../i18next';
29 import { InitialFetchRequest } from 'shared/interfaces';
31 const communityLimit = 100;
33 interface CommunitiesState {
34 communities: CommunityView[];
41 interface CommunitiesProps {
45 export class Communities extends Component<any, CommunitiesState> {
46 private subscription: Subscription;
47 private isoData = setIsoData(this.context);
48 private emptyState: CommunitiesState = {
51 page: getPageFromProps(this.props),
52 site_view: this.isoData.site_res.site_view,
56 constructor(props: any, context: any) {
57 super(props, context);
58 this.state = this.emptyState;
60 this.parseMessage = this.parseMessage.bind(this);
61 this.subscription = wsSubscribe(this.parseMessage);
63 // Only fetch the data if coming from another route
64 if (this.isoData.path == this.context.router.route.match.url) {
65 this.state.communities = this.isoData.routeData[0].communities;
66 this.state.communities.sort(
67 (a, b) => b.counts.subscribers - a.counts.subscribers
69 this.state.loading = false;
75 componentWillUnmount() {
77 this.subscription.unsubscribe();
81 static getDerivedStateFromProps(props: any): CommunitiesProps {
83 page: getPageFromProps(props),
87 componentDidUpdate(_: any, lastState: CommunitiesState) {
88 if (lastState.page !== this.state.page) {
89 this.setState({ loading: true });
94 get documentTitle(): string {
95 return `${i18n.t('communities')} - ${this.state.site_view.site.name}`;
100 <div class="container">
102 title={this.documentTitle}
103 path={this.context.router.route.match.url}
105 {this.state.loading ? (
107 <svg class="icon icon-spinner spin">
108 <use xlinkHref="#icon-spinner"></use>
114 <div class="col-md-6">
115 <h4>{i18n.t('list_of_communities')}</h4>
117 <div class="col-md-6">
118 <div class="float-md-right">{this.searchForm()}</div>
122 <div class="table-responsive">
123 <table id="community_table" class="table table-sm table-hover">
124 <thead class="pointer">
126 <th>{i18n.t('name')}</th>
127 <th>{i18n.t('category')}</th>
128 <th class="text-right">{i18n.t('subscribers')}</th>
129 <th class="text-right d-none d-lg-table-cell">
132 <th class="text-right d-none d-lg-table-cell">
139 {this.state.communities.map(cv => (
142 <CommunityLink community={cv.community} />
144 <td>{cv.category.name}</td>
145 <td class="text-right">{cv.counts.subscribers}</td>
146 <td class="text-right d-none d-lg-table-cell">
149 <td class="text-right d-none d-lg-table-cell">
152 <td class="text-right">
155 class="pointer btn-link"
158 this.handleUnsubscribe
161 {i18n.t('unsubscribe')}
165 class="pointer btn-link"
171 {i18n.t('subscribe')}
191 onSubmit={linkEvent(this, this.handleSearchSubmit)}
195 class="form-control mr-2 mb-2"
196 value={this.state.searchText}
197 placeholder={`${i18n.t('search')}...`}
198 onInput={linkEvent(this, this.handleSearchChange)}
202 <button type="submit" class="btn btn-secondary mr-2 mb-2">
203 <span>{i18n.t('search')}</span>
212 {this.state.page > 1 && (
214 class="btn btn-secondary mr-1"
215 onClick={linkEvent(this, this.prevPage)}
221 {this.state.communities.length > 0 && (
223 class="btn btn-secondary"
224 onClick={linkEvent(this, this.nextPage)}
233 updateUrl(paramUpdates: CommunitiesProps) {
234 const page = paramUpdates.page || this.state.page;
235 this.props.history.push(`/communities/page/${page}`);
238 nextPage(i: Communities) {
239 i.updateUrl({ page: i.state.page + 1 });
242 prevPage(i: Communities) {
243 i.updateUrl({ page: i.state.page - 1 });
246 handleUnsubscribe(communityId: number) {
247 let form: FollowCommunity = {
248 community_id: communityId,
252 WebSocketService.Instance.send(wsClient.followCommunity(form));
255 handleSubscribe(communityId: number) {
256 let form: FollowCommunity = {
257 community_id: communityId,
261 WebSocketService.Instance.send(wsClient.followCommunity(form));
264 handleSearchChange(i: Communities, event: any) {
265 i.setState({ searchText: event.target.value });
268 handleSearchSubmit(i: Communities) {
269 const searchParamEncoded = encodeURIComponent(i.state.searchText);
270 i.context.router.history.push(
271 `/search/q/${searchParamEncoded}/type/Communities/sort/TopAll/page/1`
276 let listCommunitiesForm: ListCommunities = {
277 sort: SortType.TopAll,
278 limit: communityLimit,
279 page: this.state.page,
280 auth: authField(false),
283 WebSocketService.Instance.send(
284 wsClient.listCommunities(listCommunitiesForm)
288 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
289 let pathSplit = req.path.split('/');
290 let page = pathSplit[3] ? Number(pathSplit[3]) : 1;
291 let listCommunitiesForm: ListCommunities = {
292 sort: SortType.TopAll,
293 limit: communityLimit,
296 setOptionalAuth(listCommunitiesForm, req.auth);
298 return [req.client.listCommunities(listCommunitiesForm)];
301 parseMessage(msg: any) {
302 let op = wsUserOp(msg);
304 toast(i18n.t(msg.error), 'danger');
306 } else if (op == UserOperation.ListCommunities) {
307 let data = wsJsonToRes<ListCommunitiesResponse>(msg).data;
308 this.state.communities = data.communities;
309 this.state.communities.sort(
310 (a, b) => b.counts.subscribers - a.counts.subscribers
312 this.state.loading = false;
313 window.scrollTo(0, 0);
314 this.setState(this.state);
315 } else if (op == UserOperation.FollowCommunity) {
316 let data = wsJsonToRes<CommunityResponse>(msg).data;
317 let found = this.state.communities.find(
318 c => c.community.id == data.community_view.community.id
320 found.subscribed = data.community_view.subscribed;
321 found.counts.subscribers = data.community_view.counts.subscribers;
322 this.setState(this.state);