1 import { Component, linkEvent } from 'inferno';
2 import { HtmlTags } from './html-tags';
3 import { Subscription } from 'rxjs';
7 ListCommunitiesResponse,
14 } from 'lemmy-js-client';
15 import { WebSocketService } from '../services';
28 import { CommunityLink } from './community-link';
29 import { i18n } from '../i18next';
30 import { InitialFetchRequest } from 'shared/interfaces';
32 const communityLimit = 100;
34 interface CommunitiesState {
35 communities: CommunityView[];
42 interface CommunitiesProps {
46 export class Communities extends Component<any, CommunitiesState> {
47 private subscription: Subscription;
48 private isoData = setIsoData(this.context);
49 private emptyState: CommunitiesState = {
52 page: getPageFromProps(this.props),
53 site_view: this.isoData.site_res.site_view,
57 constructor(props: any, context: any) {
58 super(props, context);
59 this.state = this.emptyState;
61 this.parseMessage = this.parseMessage.bind(this);
62 this.subscription = wsSubscribe(this.parseMessage);
64 // Only fetch the data if coming from another route
65 if (this.isoData.path == this.context.router.route.match.url) {
66 this.state.communities = this.isoData.routeData[0].communities;
67 this.state.communities.sort(
68 (a, b) => b.counts.subscribers - a.counts.subscribers
70 this.state.loading = false;
76 componentWillUnmount() {
78 this.subscription.unsubscribe();
82 static getDerivedStateFromProps(props: any): CommunitiesProps {
84 page: getPageFromProps(props),
88 componentDidUpdate(_: any, lastState: CommunitiesState) {
89 if (lastState.page !== this.state.page) {
90 this.setState({ loading: true });
95 get documentTitle(): string {
96 return `${i18n.t('communities')} - ${this.state.site_view.site.name}`;
101 <div class="container">
103 title={this.documentTitle}
104 path={this.context.router.route.match.url}
106 {this.state.loading ? (
108 <svg class="icon icon-spinner spin">
109 <use xlinkHref="#icon-spinner"></use>
115 <div class="col-md-6">
116 <h4>{i18n.t('list_of_communities')}</h4>
118 <div class="col-md-6">
119 <div class="float-md-right">{this.searchForm()}</div>
123 <div class="table-responsive">
124 <table id="community_table" class="table table-sm table-hover">
125 <thead class="pointer">
127 <th>{i18n.t('name')}</th>
128 <th>{i18n.t('category')}</th>
129 <th class="text-right">{i18n.t('subscribers')}</th>
130 <th class="text-right d-none d-lg-table-cell">
133 <th class="text-right d-none d-lg-table-cell">
140 {this.state.communities.map(cv => (
143 <CommunityLink community={cv.community} />
145 <td>{cv.category.name}</td>
146 <td class="text-right">{cv.counts.subscribers}</td>
147 <td class="text-right d-none d-lg-table-cell">
150 <td class="text-right d-none d-lg-table-cell">
153 <td class="text-right">
156 class="pointer btn-link"
159 this.handleUnsubscribe
162 {i18n.t('unsubscribe')}
166 class="pointer btn-link"
172 {i18n.t('subscribe')}
192 onSubmit={linkEvent(this, this.handleSearchSubmit)}
196 class="form-control mr-2 mb-2"
197 value={this.state.searchText}
198 placeholder={`${i18n.t('search')}...`}
199 onInput={linkEvent(this, this.handleSearchChange)}
203 <button type="submit" class="btn btn-secondary mr-2 mb-2">
204 <span>{i18n.t('search')}</span>
213 {this.state.page > 1 && (
215 class="btn btn-secondary mr-1"
216 onClick={linkEvent(this, this.prevPage)}
222 {this.state.communities.length > 0 && (
224 class="btn btn-secondary"
225 onClick={linkEvent(this, this.nextPage)}
234 updateUrl(paramUpdates: CommunitiesProps) {
235 const page = paramUpdates.page || this.state.page;
236 this.props.history.push(`/communities/page/${page}`);
239 nextPage(i: Communities) {
240 i.updateUrl({ page: i.state.page + 1 });
243 prevPage(i: Communities) {
244 i.updateUrl({ page: i.state.page - 1 });
247 handleUnsubscribe(communityId: number) {
248 let form: FollowCommunity = {
249 community_id: communityId,
253 WebSocketService.Instance.send(wsClient.followCommunity(form));
256 handleSubscribe(communityId: number) {
257 let form: FollowCommunity = {
258 community_id: communityId,
262 WebSocketService.Instance.send(wsClient.followCommunity(form));
265 handleSearchChange(i: Communities, event: any) {
266 i.setState({ searchText: event.target.value });
269 handleSearchSubmit(i: Communities) {
270 const searchParamEncoded = encodeURIComponent(i.state.searchText);
271 i.context.router.history.push(
272 `/search/q/${searchParamEncoded}/type/Communities/sort/TopAll/page/1`
277 let listCommunitiesForm: ListCommunities = {
278 type_: ListingType.All,
279 sort: SortType.TopAll,
280 limit: communityLimit,
281 page: this.state.page,
282 auth: authField(false),
285 WebSocketService.Instance.send(
286 wsClient.listCommunities(listCommunitiesForm)
290 static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
291 let pathSplit = req.path.split('/');
292 let page = pathSplit[3] ? Number(pathSplit[3]) : 1;
293 let listCommunitiesForm: ListCommunities = {
294 type_: ListingType.All,
295 sort: SortType.TopAll,
296 limit: communityLimit,
299 setOptionalAuth(listCommunitiesForm, req.auth);
301 return [req.client.listCommunities(listCommunitiesForm)];
304 parseMessage(msg: any) {
305 let op = wsUserOp(msg);
307 toast(i18n.t(msg.error), 'danger');
309 } else if (op == UserOperation.ListCommunities) {
310 let data = wsJsonToRes<ListCommunitiesResponse>(msg).data;
311 this.state.communities = data.communities;
312 this.state.communities.sort(
313 (a, b) => b.counts.subscribers - a.counts.subscribers
315 this.state.loading = false;
316 window.scrollTo(0, 0);
317 this.setState(this.state);
318 } else if (op == UserOperation.FollowCommunity) {
319 let data = wsJsonToRes<CommunityResponse>(msg).data;
320 let found = this.state.communities.find(
321 c => c.community.id == data.community_view.community.id
323 found.subscribed = data.community_view.subscribed;
324 found.counts.subscribers = data.community_view.counts.subscribers;
325 this.setState(this.state);