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[];
40 interface CommunitiesProps {
44 export class Communities extends Component<any, CommunitiesState> {
45 private subscription: Subscription;
46 private isoData = setIsoData(this.context);
47 private emptyState: CommunitiesState = {
50 page: getPageFromProps(this.props),
51 site_view: this.isoData.site_res.site_view,
54 constructor(props: any, context: any) {
55 super(props, context);
56 this.state = this.emptyState;
58 this.parseMessage = this.parseMessage.bind(this);
59 this.subscription = wsSubscribe(this.parseMessage);
61 // Only fetch the data if coming from another route
62 if (this.isoData.path == this.context.router.route.match.url) {
63 this.state.communities = this.isoData.routeData[0].communities;
64 this.state.communities.sort(
65 (a, b) => b.counts.subscribers - a.counts.subscribers
67 this.state.loading = false;
73 componentWillUnmount() {
75 this.subscription.unsubscribe();
79 static getDerivedStateFromProps(props: any): CommunitiesProps {
81 page: getPageFromProps(props),
85 componentDidUpdate(_: any, lastState: CommunitiesState) {
86 if (lastState.page !== this.state.page) {
87 this.setState({ loading: true });
92 get documentTitle(): string {
93 return `${i18n.t('communities')} - ${this.state.site_view.site.name}`;
98 <div class="container">
100 title={this.documentTitle}
101 path={this.context.router.route.match.url}
103 {this.state.loading ? (
105 <svg class="icon icon-spinner spin">
106 <use xlinkHref="#icon-spinner"></use>
111 <h5>{i18n.t('list_of_communities')}</h5>
112 <div class="table-responsive">
113 <table id="community_table" class="table table-sm table-hover">
114 <thead class="pointer">
116 <th>{i18n.t('name')}</th>
117 <th>{i18n.t('category')}</th>
118 <th class="text-right">{i18n.t('subscribers')}</th>
119 <th class="text-right d-none d-lg-table-cell">
122 <th class="text-right d-none d-lg-table-cell">
129 {this.state.communities.map(cv => (
132 <CommunityLink community={cv.community} />
134 <td>{cv.category.name}</td>
135 <td class="text-right">{cv.counts.subscribers}</td>
136 <td class="text-right d-none d-lg-table-cell">
139 <td class="text-right d-none d-lg-table-cell">
142 <td class="text-right">
145 class="pointer btn-link"
148 this.handleUnsubscribe
151 {i18n.t('unsubscribe')}
155 class="pointer btn-link"
161 {i18n.t('subscribe')}
180 {this.state.page > 1 && (
182 class="btn btn-secondary mr-1"
183 onClick={linkEvent(this, this.prevPage)}
189 {this.state.communities.length > 0 && (
191 class="btn btn-secondary"
192 onClick={linkEvent(this, this.nextPage)}
201 updateUrl(paramUpdates: CommunitiesProps) {
202 const page = paramUpdates.page || this.state.page;
203 this.props.history.push(`/communities/page/${page}`);
206 nextPage(i: Communities) {
207 i.updateUrl({ page: i.state.page + 1 });
210 prevPage(i: Communities) {
211 i.updateUrl({ page: i.state.page - 1 });
214 handleUnsubscribe(communityId: number) {
215 let form: FollowCommunity = {
216 community_id: communityId,
220 WebSocketService.Instance.send(wsClient.followCommunity(form));
223 handleSubscribe(communityId: number) {
224 let form: FollowCommunity = {
225 community_id: communityId,
229 WebSocketService.Instance.send(wsClient.followCommunity(form));
233 let listCommunitiesForm: ListCommunities = {
234 sort: SortType.TopAll,
235 limit: communityLimit,
236 page: this.state.page,
237 auth: authField(false),
240 WebSocketService.Instance.send(
241 wsClient.listCommunities(listCommunitiesForm)
245 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
246 let pathSplit = req.path.split('/');
247 let page = pathSplit[3] ? Number(pathSplit[3]) : 1;
248 let listCommunitiesForm: ListCommunities = {
249 sort: SortType.TopAll,
250 limit: communityLimit,
253 setOptionalAuth(listCommunitiesForm, req.auth);
255 return [req.client.listCommunities(listCommunitiesForm)];
258 parseMessage(msg: any) {
259 let op = wsUserOp(msg);
261 toast(i18n.t(msg.error), 'danger');
263 } else if (op == UserOperation.ListCommunities) {
264 let data = wsJsonToRes<ListCommunitiesResponse>(msg).data;
265 this.state.communities = data.communities;
266 this.state.communities.sort(
267 (a, b) => b.counts.subscribers - a.counts.subscribers
269 this.state.loading = false;
270 window.scrollTo(0, 0);
271 this.setState(this.state);
272 } else if (op == UserOperation.FollowCommunity) {
273 let data = wsJsonToRes<CommunityResponse>(msg).data;
274 let found = this.state.communities.find(
275 c => c.community.id == data.community_view.community.id
277 found.subscribed = data.community_view.subscribed;
278 found.counts.subscribers = data.community_view.counts.subscribers;
279 this.setState(this.state);