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 { UserService, WebSocketService } from '../services';
24 import { CommunityLink } from './community-link';
25 import { i18n } from '../i18next';
26 import { InitialFetchRequest } from 'shared/interfaces';
28 const communityLimit = 100;
30 interface CommunitiesState {
31 communities: CommunityView[];
37 interface CommunitiesProps {
41 export class Communities extends Component<any, CommunitiesState> {
42 private subscription: Subscription;
43 private isoData = setIsoData(this.context);
44 private emptyState: CommunitiesState = {
47 page: getPageFromProps(this.props),
48 site_view: this.isoData.site_res.site_view,
51 constructor(props: any, context: any) {
52 super(props, context);
53 this.state = this.emptyState;
55 this.parseMessage = this.parseMessage.bind(this);
56 this.subscription = wsSubscribe(this.parseMessage);
58 // Only fetch the data if coming from another route
59 if (this.isoData.path == this.context.router.route.match.url) {
60 this.state.communities = this.isoData.routeData[0].communities;
61 this.state.communities.sort(
62 (a, b) => b.counts.subscribers - a.counts.subscribers
64 this.state.loading = false;
70 componentWillUnmount() {
72 this.subscription.unsubscribe();
76 static getDerivedStateFromProps(props: any): CommunitiesProps {
78 page: getPageFromProps(props),
82 componentDidUpdate(_: any, lastState: CommunitiesState) {
83 if (lastState.page !== this.state.page) {
84 this.setState({ loading: true });
89 get documentTitle(): string {
90 return `${i18n.t('communities')} - ${this.state.site_view.site.name}`;
95 <div class="container">
97 title={this.documentTitle}
98 path={this.context.router.route.match.url}
100 {this.state.loading ? (
102 <svg class="icon icon-spinner spin">
103 <use xlinkHref="#icon-spinner"></use>
108 <h5>{i18n.t('list_of_communities')}</h5>
109 <div class="table-responsive">
110 <table id="community_table" class="table table-sm table-hover">
111 <thead class="pointer">
113 <th>{i18n.t('name')}</th>
114 <th>{i18n.t('category')}</th>
115 <th class="text-right">{i18n.t('subscribers')}</th>
116 <th class="text-right d-none d-lg-table-cell">
119 <th class="text-right d-none d-lg-table-cell">
126 {this.state.communities.map(cv => (
129 <CommunityLink community={cv.community} />
131 <td>{cv.category.name}</td>
132 <td class="text-right">{cv.counts.subscribers}</td>
133 <td class="text-right d-none d-lg-table-cell">
136 <td class="text-right d-none d-lg-table-cell">
139 <td class="text-right">
142 class="pointer btn-link"
145 this.handleUnsubscribe
148 {i18n.t('unsubscribe')}
152 class="pointer btn-link"
158 {i18n.t('subscribe')}
177 {this.state.page > 1 && (
179 class="btn btn-secondary mr-1"
180 onClick={linkEvent(this, this.prevPage)}
186 {this.state.communities.length > 0 && (
188 class="btn btn-secondary"
189 onClick={linkEvent(this, this.nextPage)}
198 updateUrl(paramUpdates: CommunitiesProps) {
199 const page = paramUpdates.page || this.state.page;
200 this.props.history.push(`/communities/page/${page}`);
203 nextPage(i: Communities) {
204 i.updateUrl({ page: i.state.page + 1 });
207 prevPage(i: Communities) {
208 i.updateUrl({ page: i.state.page - 1 });
211 handleUnsubscribe(communityId: number) {
212 let form: FollowCommunity = {
213 community_id: communityId,
215 auth: UserService.Instance.authField(),
217 WebSocketService.Instance.client.followCommunity(form);
220 handleSubscribe(communityId: number) {
221 let form: FollowCommunity = {
222 community_id: communityId,
224 auth: UserService.Instance.authField(),
226 WebSocketService.Instance.client.followCommunity(form);
230 let listCommunitiesForm: ListCommunities = {
231 sort: SortType.TopAll,
232 limit: communityLimit,
233 page: this.state.page,
234 auth: UserService.Instance.authField(false),
237 WebSocketService.Instance.client.listCommunities(listCommunitiesForm);
240 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
241 let pathSplit = req.path.split('/');
242 let page = pathSplit[3] ? Number(pathSplit[3]) : 1;
243 let listCommunitiesForm: ListCommunities = {
244 sort: SortType.TopAll,
245 limit: communityLimit,
250 return [req.client.listCommunities(listCommunitiesForm)];
253 parseMessage(msg: any) {
254 let op = wsUserOp(msg);
256 toast(i18n.t(msg.error), 'danger');
258 } else if (op == UserOperation.ListCommunities) {
259 let data = wsJsonToRes<ListCommunitiesResponse>(msg).data;
260 this.state.communities = data.communities;
261 this.state.communities.sort(
262 (a, b) => b.counts.subscribers - a.counts.subscribers
264 this.state.loading = false;
265 window.scrollTo(0, 0);
266 this.setState(this.state);
267 } else if (op == UserOperation.FollowCommunity) {
268 let data = wsJsonToRes<CommunityResponse>(msg).data;
269 let found = this.state.communities.find(
270 c => c.community.id == data.community_view.community.id
272 found.subscribed = data.community_view.subscribed;
273 found.counts.subscribers = data.community_view.counts.subscribers;
274 this.setState(this.state);