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;
55 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.loading = false;
68 componentWillUnmount() {
70 this.subscription.unsubscribe();
74 static getDerivedStateFromProps(props: any): CommunitiesProps {
76 page: getPageFromProps(props),
80 componentDidUpdate(_: any, lastState: CommunitiesState) {
81 if (lastState.page !== this.state.page) {
82 this.setState({ loading: true });
87 get documentTitle(): string {
88 return `${i18n.t('communities')} - ${this.state.site.name}`;
93 <div class="container">
94 <Helmet title={this.documentTitle} />
95 {this.state.loading ? (
97 <svg class="icon icon-spinner spin">
98 <use xlinkHref="#icon-spinner"></use>
103 <h5>{i18n.t('list_of_communities')}</h5>
104 <div class="table-responsive">
105 <table id="community_table" class="table table-sm table-hover">
106 <thead class="pointer">
108 <th>{i18n.t('name')}</th>
109 <th>{i18n.t('category')}</th>
110 <th class="text-right">{i18n.t('subscribers')}</th>
111 <th class="text-right d-none d-lg-table-cell">
114 <th class="text-right d-none d-lg-table-cell">
121 {this.state.communities.map(community => (
124 <CommunityLink community={community} />
126 <td>{community.category_name}</td>
127 <td class="text-right">
128 {community.number_of_subscribers}
130 <td class="text-right d-none d-lg-table-cell">
131 {community.number_of_posts}
133 <td class="text-right d-none d-lg-table-cell">
134 {community.number_of_comments}
136 <td class="text-right">
137 {community.subscribed ? (
139 class="pointer btn-link"
142 this.handleUnsubscribe
145 {i18n.t('unsubscribe')}
149 class="pointer btn-link"
155 {i18n.t('subscribe')}
174 {this.state.page > 1 && (
176 class="btn btn-secondary mr-1"
177 onClick={linkEvent(this, this.prevPage)}
183 {this.state.communities.length > 0 && (
185 class="btn btn-secondary"
186 onClick={linkEvent(this, this.nextPage)}
195 updateUrl(paramUpdates: CommunitiesProps) {
196 const page = paramUpdates.page || this.state.page;
197 this.props.history.push(`/communities/page/${page}`);
200 nextPage(i: Communities) {
201 i.updateUrl({ page: i.state.page + 1 });
204 prevPage(i: Communities) {
205 i.updateUrl({ page: i.state.page - 1 });
208 handleUnsubscribe(communityId: number) {
209 let form: FollowCommunityForm = {
210 community_id: communityId,
213 WebSocketService.Instance.followCommunity(form);
216 handleSubscribe(communityId: number) {
217 let form: FollowCommunityForm = {
218 community_id: communityId,
221 WebSocketService.Instance.followCommunity(form);
225 let listCommunitiesForm: ListCommunitiesForm = {
226 sort: SortType.TopAll,
227 limit: communityLimit,
228 page: this.state.page,
231 WebSocketService.Instance.listCommunities(listCommunitiesForm);
234 static fetchInitialData(auth: string, path: string): Promise<any>[] {
235 let pathSplit = path.split('/');
236 let page = pathSplit[3] ? Number(pathSplit[3]) : 1;
237 let listCommunitiesForm: ListCommunitiesForm = {
238 sort: SortType.TopAll,
239 limit: communityLimit,
242 setAuth(listCommunitiesForm, auth);
244 return [lemmyHttp.listCommunities(listCommunitiesForm)];
247 parseMessage(msg: WebSocketJsonResponse) {
249 let res = wsJsonToRes(msg);
251 toast(i18n.t(msg.error), 'danger');
253 } else if (res.op == UserOperation.ListCommunities) {
254 let data = res.data as ListCommunitiesResponse;
255 this.state.communities = data.communities;
256 this.state.communities.sort(
257 (a, b) => b.number_of_subscribers - a.number_of_subscribers
259 this.state.loading = false;
260 window.scrollTo(0, 0);
261 this.setState(this.state);
262 } else if (res.op == UserOperation.FollowCommunity) {
263 let data = res.data as CommunityResponse;
264 let found = this.state.communities.find(c => c.id == data.community.id);
265 found.subscribed = data.community.subscribed;
266 found.number_of_subscribers = data.community.number_of_subscribers;
267 this.setState(this.state);