1 import { Component, linkEvent } from 'inferno';
2 import { Helmet } from 'inferno-helmet';
3 import { Subscription } from 'rxjs';
7 ListCommunitiesResponse,
12 WebSocketJsonResponse,
14 } from 'lemmy-js-client';
15 import { WebSocketService } from '../services';
26 import { CommunityLink } from './community-link';
27 import { i18n } from '../i18next';
29 const communityLimit = 100;
31 interface CommunitiesState {
32 communities: Community[];
38 interface CommunitiesProps {
42 export class Communities extends Component<any, CommunitiesState> {
43 private subscription: Subscription;
44 private isoData = setIsoData(this.context);
45 private emptyState: CommunitiesState = {
48 page: getPageFromProps(this.props),
49 site: this.isoData.site.site,
52 constructor(props: any, context: any) {
53 super(props, context);
54 this.state = this.emptyState;
56 this.parseMessage = this.parseMessage.bind(this);
57 this.subscription = wsSubscribe(this.parseMessage);
59 // Only fetch the data if coming from another route
60 if (this.isoData.path == this.context.router.route.match.url) {
61 this.state.communities = this.isoData.routeData[0].communities;
62 this.state.communities.sort(
63 (a, b) => b.number_of_subscribers - a.number_of_subscribers
65 this.state.loading = false;
71 componentWillUnmount() {
73 this.subscription.unsubscribe();
77 static getDerivedStateFromProps(props: any): CommunitiesProps {
79 page: getPageFromProps(props),
83 componentDidUpdate(_: any, lastState: CommunitiesState) {
84 if (lastState.page !== this.state.page) {
85 this.setState({ loading: true });
90 get documentTitle(): string {
91 return `${i18n.t('communities')} - ${this.state.site.name}`;
96 <div class="container">
97 <Helmet title={this.documentTitle} />
98 {this.state.loading ? (
100 <svg class="icon icon-spinner spin">
101 <use xlinkHref="#icon-spinner"></use>
106 <h5>{i18n.t('list_of_communities')}</h5>
107 <div class="table-responsive">
108 <table id="community_table" class="table table-sm table-hover">
109 <thead class="pointer">
111 <th>{i18n.t('name')}</th>
112 <th>{i18n.t('category')}</th>
113 <th class="text-right">{i18n.t('subscribers')}</th>
114 <th class="text-right d-none d-lg-table-cell">
117 <th class="text-right d-none d-lg-table-cell">
124 {this.state.communities.map(community => (
127 <CommunityLink community={community} />
129 <td>{community.category_name}</td>
130 <td class="text-right">
131 {community.number_of_subscribers}
133 <td class="text-right d-none d-lg-table-cell">
134 {community.number_of_posts}
136 <td class="text-right d-none d-lg-table-cell">
137 {community.number_of_comments}
139 <td class="text-right">
140 {community.subscribed ? (
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: FollowCommunityForm = {
213 community_id: communityId,
216 WebSocketService.Instance.followCommunity(form);
219 handleSubscribe(communityId: number) {
220 let form: FollowCommunityForm = {
221 community_id: communityId,
224 WebSocketService.Instance.followCommunity(form);
228 let listCommunitiesForm: ListCommunitiesForm = {
229 sort: SortType.TopAll,
230 limit: communityLimit,
231 page: this.state.page,
234 WebSocketService.Instance.listCommunities(listCommunitiesForm);
237 static fetchInitialData(auth: string, path: string): Promise<any>[] {
238 let pathSplit = path.split('/');
239 let page = pathSplit[3] ? Number(pathSplit[3]) : 1;
240 let listCommunitiesForm: ListCommunitiesForm = {
241 sort: SortType.TopAll,
242 limit: communityLimit,
245 setAuth(listCommunitiesForm, auth);
247 return [lemmyHttp.listCommunities(listCommunitiesForm)];
250 parseMessage(msg: WebSocketJsonResponse) {
252 let res = wsJsonToRes(msg);
254 toast(i18n.t(msg.error), 'danger');
256 } else if (res.op == UserOperation.ListCommunities) {
257 let data = res.data as ListCommunitiesResponse;
258 this.state.communities = data.communities;
259 this.state.communities.sort(
260 (a, b) => b.number_of_subscribers - a.number_of_subscribers
262 this.state.loading = false;
263 window.scrollTo(0, 0);
264 this.setState(this.state);
265 } else if (res.op == UserOperation.FollowCommunity) {
266 let data = res.data as CommunityResponse;
267 let found = this.state.communities.find(c => c.id == data.community.id);
268 found.subscribed = data.community.subscribed;
269 found.number_of_subscribers = data.community.number_of_subscribers;
270 this.setState(this.state);