1 import { Component, linkEvent } from 'inferno';
2 import { HtmlTags } from './html-tags';
3 import { Subscription } from 'rxjs';
7 ListCommunitiesResponse,
12 WebSocketJsonResponse,
14 } from 'lemmy-js-client';
15 import { WebSocketService } from '../services';
25 import { CommunityLink } from './community-link';
26 import { i18n } from '../i18next';
27 import { InitialFetchRequest } from 'shared/interfaces';
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">
98 title={this.documentTitle}
99 path={this.context.router.route.match.url}
101 {this.state.loading ? (
103 <svg class="icon icon-spinner spin">
104 <use xlinkHref="#icon-spinner"></use>
109 <h5>{i18n.t('list_of_communities')}</h5>
110 <div class="table-responsive">
111 <table id="community_table" class="table table-sm table-hover">
112 <thead class="pointer">
114 <th>{i18n.t('name')}</th>
115 <th>{i18n.t('category')}</th>
116 <th class="text-right">{i18n.t('subscribers')}</th>
117 <th class="text-right d-none d-lg-table-cell">
120 <th class="text-right d-none d-lg-table-cell">
127 {this.state.communities.map(community => (
130 <CommunityLink community={community} />
132 <td>{community.category_name}</td>
133 <td class="text-right">
134 {community.number_of_subscribers}
136 <td class="text-right d-none d-lg-table-cell">
137 {community.number_of_posts}
139 <td class="text-right d-none d-lg-table-cell">
140 {community.number_of_comments}
142 <td class="text-right">
143 {community.subscribed ? (
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: FollowCommunityForm = {
216 community_id: communityId,
219 WebSocketService.Instance.followCommunity(form);
222 handleSubscribe(communityId: number) {
223 let form: FollowCommunityForm = {
224 community_id: communityId,
227 WebSocketService.Instance.followCommunity(form);
231 let listCommunitiesForm: ListCommunitiesForm = {
232 sort: SortType.TopAll,
233 limit: communityLimit,
234 page: this.state.page,
237 WebSocketService.Instance.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: ListCommunitiesForm = {
244 sort: SortType.TopAll,
245 limit: communityLimit,
248 setAuth(listCommunitiesForm, req.auth);
250 return [req.client.listCommunities(listCommunitiesForm)];
253 parseMessage(msg: WebSocketJsonResponse) {
255 let res = wsJsonToRes(msg);
257 toast(i18n.t(msg.error), 'danger');
259 } else if (res.op == UserOperation.ListCommunities) {
260 let data = res.data as ListCommunitiesResponse;
261 this.state.communities = data.communities;
262 this.state.communities.sort(
263 (a, b) => b.number_of_subscribers - a.number_of_subscribers
265 this.state.loading = false;
266 window.scrollTo(0, 0);
267 this.setState(this.state);
268 } else if (res.op == UserOperation.FollowCommunity) {
269 let data = res.data as CommunityResponse;
270 let found = this.state.communities.find(c => c.id == data.community.id);
271 found.subscribed = data.community.subscribed;
272 found.number_of_subscribers = data.community.number_of_subscribers;
273 this.setState(this.state);