2019-04-04 20:00:19 +00:00
|
|
|
import { Component, linkEvent } from 'inferno';
|
2019-10-19 00:20:27 +00:00
|
|
|
import { Subscription } from 'rxjs';
|
2019-04-04 20:00:19 +00:00
|
|
|
import { retryWhen, delay, take } from 'rxjs/operators';
|
2019-10-19 00:20:27 +00:00
|
|
|
import {
|
|
|
|
UserOperation,
|
|
|
|
Community,
|
|
|
|
ListCommunitiesResponse,
|
|
|
|
CommunityResponse,
|
|
|
|
FollowCommunityForm,
|
|
|
|
ListCommunitiesForm,
|
|
|
|
SortType,
|
2020-01-19 05:38:45 +00:00
|
|
|
WebSocketJsonResponse,
|
2020-07-08 05:02:14 +00:00
|
|
|
GetSiteResponse,
|
2019-10-19 00:20:27 +00:00
|
|
|
} from '../interfaces';
|
2019-04-08 05:19:02 +00:00
|
|
|
import { WebSocketService } from '../services';
|
2020-07-13 03:17:56 +00:00
|
|
|
import { wsJsonToRes, toast, getPageFromProps } from '../utils';
|
2020-04-14 23:18:13 +00:00
|
|
|
import { CommunityLink } from './community-link';
|
2019-08-10 00:14:43 +00:00
|
|
|
import { i18n } from '../i18next';
|
2019-04-04 20:00:19 +00:00
|
|
|
|
2019-04-05 00:01:10 +00:00
|
|
|
declare const Sortable: any;
|
|
|
|
|
2020-01-19 21:47:54 +00:00
|
|
|
const communityLimit = 100;
|
|
|
|
|
2019-04-04 20:00:19 +00:00
|
|
|
interface CommunitiesState {
|
|
|
|
communities: Array<Community>;
|
2019-04-30 13:49:01 +00:00
|
|
|
page: number;
|
2019-04-08 21:46:09 +00:00
|
|
|
loading: boolean;
|
2019-04-04 20:00:19 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 05:13:43 +00:00
|
|
|
interface CommunitiesProps {
|
|
|
|
page: number;
|
|
|
|
}
|
|
|
|
|
2019-04-04 20:00:19 +00:00
|
|
|
export class Communities extends Component<any, CommunitiesState> {
|
|
|
|
private subscription: Subscription;
|
|
|
|
private emptyState: CommunitiesState = {
|
2019-04-08 21:46:09 +00:00
|
|
|
communities: [],
|
2019-04-30 13:49:01 +00:00
|
|
|
loading: true,
|
2020-07-13 03:17:56 +00:00
|
|
|
page: getPageFromProps(this.props),
|
2019-10-19 00:20:27 +00:00
|
|
|
};
|
2019-04-04 20:00:19 +00:00
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
constructor(props: any, context: any) {
|
2019-04-04 20:00:19 +00:00
|
|
|
super(props, context);
|
|
|
|
this.state = this.emptyState;
|
|
|
|
this.subscription = WebSocketService.Instance.subject
|
2020-01-19 21:47:54 +00:00
|
|
|
.pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
|
2019-08-10 00:14:43 +00:00
|
|
|
.subscribe(
|
2019-10-19 00:20:27 +00:00
|
|
|
msg => this.parseMessage(msg),
|
|
|
|
err => console.error(err),
|
2019-04-04 20:00:19 +00:00
|
|
|
() => console.log('complete')
|
2019-08-10 00:14:43 +00:00
|
|
|
);
|
2019-04-10 06:19:12 +00:00
|
|
|
|
2019-04-30 13:49:01 +00:00
|
|
|
this.refetch();
|
2020-07-08 05:02:14 +00:00
|
|
|
WebSocketService.Instance.getSite();
|
2019-04-30 13:49:01 +00:00
|
|
|
}
|
2019-04-05 06:26:38 +00:00
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
|
2020-07-14 05:13:43 +00:00
|
|
|
static getDerivedStateFromProps(props: any): CommunitiesProps {
|
2020-07-13 03:17:56 +00:00
|
|
|
return {
|
|
|
|
page: getPageFromProps(props),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-07-14 05:13:43 +00:00
|
|
|
componentDidUpdate(_: any, lastState: CommunitiesState) {
|
2020-07-13 03:17:56 +00:00
|
|
|
if (lastState.page !== this.state.page) {
|
|
|
|
this.setState({ loading: true });
|
2019-04-30 13:49:01 +00:00
|
|
|
this.refetch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-04 20:00:19 +00:00
|
|
|
render() {
|
|
|
|
return (
|
2019-04-09 21:21:19 +00:00
|
|
|
<div class="container">
|
2019-10-19 00:20:27 +00:00
|
|
|
{this.state.loading ? (
|
2020-02-02 18:50:44 +00:00
|
|
|
<h5 class="">
|
2019-10-19 00:20:27 +00:00
|
|
|
<svg class="icon icon-spinner spin">
|
|
|
|
<use xlinkHref="#icon-spinner"></use>
|
|
|
|
</svg>
|
2020-02-02 18:50:44 +00:00
|
|
|
</h5>
|
2019-10-19 00:20:27 +00:00
|
|
|
) : (
|
|
|
|
<div>
|
2020-02-02 18:50:44 +00:00
|
|
|
<h5>{i18n.t('list_of_communities')}</h5>
|
2019-10-19 00:20:27 +00:00
|
|
|
<div class="table-responsive">
|
|
|
|
<table id="community_table" class="table table-sm table-hover">
|
|
|
|
<thead class="pointer">
|
2019-04-08 21:46:09 +00:00
|
|
|
<tr>
|
2020-02-02 18:50:44 +00:00
|
|
|
<th>{i18n.t('name')}</th>
|
|
|
|
<th class="d-none d-lg-table-cell">{i18n.t('title')}</th>
|
|
|
|
<th>{i18n.t('category')}</th>
|
|
|
|
<th class="text-right">{i18n.t('subscribers')}</th>
|
2019-10-19 00:20:27 +00:00
|
|
|
<th class="text-right d-none d-lg-table-cell">
|
2020-02-02 18:50:44 +00:00
|
|
|
{i18n.t('posts')}
|
2019-10-19 00:20:27 +00:00
|
|
|
</th>
|
|
|
|
<th class="text-right d-none d-lg-table-cell">
|
2020-02-02 18:50:44 +00:00
|
|
|
{i18n.t('comments')}
|
2019-10-19 00:20:27 +00:00
|
|
|
</th>
|
|
|
|
<th></th>
|
2019-04-08 21:46:09 +00:00
|
|
|
</tr>
|
2019-10-19 00:20:27 +00:00
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{this.state.communities.map(community => (
|
|
|
|
<tr>
|
|
|
|
<td>
|
2020-04-14 23:18:13 +00:00
|
|
|
<CommunityLink community={community} />
|
2019-10-19 00:20:27 +00:00
|
|
|
</td>
|
|
|
|
<td class="d-none d-lg-table-cell">{community.title}</td>
|
|
|
|
<td>{community.category_name}</td>
|
|
|
|
<td class="text-right">
|
|
|
|
{community.number_of_subscribers}
|
|
|
|
</td>
|
|
|
|
<td class="text-right d-none d-lg-table-cell">
|
|
|
|
{community.number_of_posts}
|
|
|
|
</td>
|
|
|
|
<td class="text-right d-none d-lg-table-cell">
|
|
|
|
{community.number_of_comments}
|
|
|
|
</td>
|
|
|
|
<td class="text-right">
|
|
|
|
{community.subscribed ? (
|
|
|
|
<span
|
|
|
|
class="pointer btn-link"
|
|
|
|
onClick={linkEvent(
|
|
|
|
community.id,
|
|
|
|
this.handleUnsubscribe
|
|
|
|
)}
|
|
|
|
>
|
2020-02-02 18:50:44 +00:00
|
|
|
{i18n.t('unsubscribe')}
|
2019-10-19 00:20:27 +00:00
|
|
|
</span>
|
|
|
|
) : (
|
|
|
|
<span
|
|
|
|
class="pointer btn-link"
|
|
|
|
onClick={linkEvent(
|
|
|
|
community.id,
|
|
|
|
this.handleSubscribe
|
|
|
|
)}
|
|
|
|
>
|
2020-02-02 18:50:44 +00:00
|
|
|
{i18n.t('subscribe')}
|
2019-10-19 00:20:27 +00:00
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
))}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
{this.paginator()}
|
2019-04-08 21:46:09 +00:00
|
|
|
</div>
|
2019-10-19 00:20:27 +00:00
|
|
|
)}
|
2019-04-04 20:00:19 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-30 13:49:01 +00:00
|
|
|
paginator() {
|
|
|
|
return (
|
|
|
|
<div class="mt-2">
|
2019-10-19 00:20:27 +00:00
|
|
|
{this.state.page > 1 && (
|
|
|
|
<button
|
|
|
|
class="btn btn-sm btn-secondary mr-1"
|
|
|
|
onClick={linkEvent(this, this.prevPage)}
|
|
|
|
>
|
2020-02-02 18:50:44 +00:00
|
|
|
{i18n.t('prev')}
|
2019-10-19 00:20:27 +00:00
|
|
|
</button>
|
|
|
|
)}
|
2020-01-31 20:52:27 +00:00
|
|
|
|
2020-07-10 00:03:33 +00:00
|
|
|
{this.state.communities.length > 0 && (
|
2020-01-19 21:47:54 +00:00
|
|
|
<button
|
|
|
|
class="btn btn-sm btn-secondary"
|
|
|
|
onClick={linkEvent(this, this.nextPage)}
|
|
|
|
>
|
2020-02-02 18:50:44 +00:00
|
|
|
{i18n.t('next')}
|
2020-01-19 21:47:54 +00:00
|
|
|
</button>
|
|
|
|
)}
|
2019-04-30 13:49:01 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-14 05:13:43 +00:00
|
|
|
updateUrl(paramUpdates: CommunitiesProps) {
|
2020-07-13 03:17:56 +00:00
|
|
|
const page = paramUpdates.page || this.state.page;
|
|
|
|
this.props.history.push(`/communities/page/${page}`);
|
2019-04-30 13:49:01 +00:00
|
|
|
}
|
|
|
|
|
2019-10-19 00:20:27 +00:00
|
|
|
nextPage(i: Communities) {
|
2020-07-13 03:17:56 +00:00
|
|
|
i.updateUrl({ page: i.state.page + 1 });
|
2019-04-30 13:49:01 +00:00
|
|
|
}
|
|
|
|
|
2019-10-19 00:20:27 +00:00
|
|
|
prevPage(i: Communities) {
|
2020-07-13 03:17:56 +00:00
|
|
|
i.updateUrl({ page: i.state.page - 1 });
|
2019-04-30 13:49:01 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 06:26:38 +00:00
|
|
|
handleUnsubscribe(communityId: number) {
|
|
|
|
let form: FollowCommunityForm = {
|
|
|
|
community_id: communityId,
|
2019-10-19 00:20:27 +00:00
|
|
|
follow: false,
|
2019-04-05 06:26:38 +00:00
|
|
|
};
|
|
|
|
WebSocketService.Instance.followCommunity(form);
|
|
|
|
}
|
2019-04-04 20:00:19 +00:00
|
|
|
|
2019-04-05 06:26:38 +00:00
|
|
|
handleSubscribe(communityId: number) {
|
|
|
|
let form: FollowCommunityForm = {
|
|
|
|
community_id: communityId,
|
2019-10-19 00:20:27 +00:00
|
|
|
follow: true,
|
2019-04-05 06:26:38 +00:00
|
|
|
};
|
|
|
|
WebSocketService.Instance.followCommunity(form);
|
|
|
|
}
|
|
|
|
|
2019-04-30 13:49:01 +00:00
|
|
|
refetch() {
|
|
|
|
let listCommunitiesForm: ListCommunitiesForm = {
|
|
|
|
sort: SortType[SortType.TopAll],
|
2020-01-19 21:47:54 +00:00
|
|
|
limit: communityLimit,
|
2019-04-30 13:49:01 +00:00
|
|
|
page: this.state.page,
|
2019-10-19 00:20:27 +00:00
|
|
|
};
|
2019-04-30 13:49:01 +00:00
|
|
|
|
|
|
|
WebSocketService.Instance.listCommunities(listCommunitiesForm);
|
|
|
|
}
|
|
|
|
|
2020-01-19 05:38:45 +00:00
|
|
|
parseMessage(msg: WebSocketJsonResponse) {
|
2019-04-04 20:00:19 +00:00
|
|
|
console.log(msg);
|
2020-01-19 04:54:10 +00:00
|
|
|
let res = wsJsonToRes(msg);
|
2020-01-25 14:58:53 +00:00
|
|
|
if (msg.error) {
|
2020-01-23 03:29:11 +00:00
|
|
|
toast(i18n.t(msg.error), 'danger');
|
2019-04-04 20:00:19 +00:00
|
|
|
return;
|
2020-01-19 04:54:10 +00:00
|
|
|
} else if (res.op == UserOperation.ListCommunities) {
|
|
|
|
let data = res.data as ListCommunitiesResponse;
|
|
|
|
this.state.communities = data.communities;
|
2019-10-19 00:20:27 +00:00
|
|
|
this.state.communities.sort(
|
|
|
|
(a, b) => b.number_of_subscribers - a.number_of_subscribers
|
|
|
|
);
|
2019-04-08 21:46:09 +00:00
|
|
|
this.state.loading = false;
|
2019-10-19 00:20:27 +00:00
|
|
|
window.scrollTo(0, 0);
|
2019-04-04 20:00:19 +00:00
|
|
|
this.setState(this.state);
|
2019-06-02 16:41:57 +00:00
|
|
|
let table = document.querySelector('#community_table');
|
|
|
|
Sortable.initTable(table);
|
2020-01-19 04:54:10 +00:00
|
|
|
} else if (res.op == UserOperation.FollowCommunity) {
|
|
|
|
let data = res.data as CommunityResponse;
|
|
|
|
let found = this.state.communities.find(c => c.id == data.community.id);
|
|
|
|
found.subscribed = data.community.subscribed;
|
|
|
|
found.number_of_subscribers = data.community.number_of_subscribers;
|
2019-04-05 06:26:38 +00:00
|
|
|
this.setState(this.state);
|
2020-07-08 05:02:14 +00:00
|
|
|
} else if (res.op == UserOperation.GetSite) {
|
|
|
|
let data = res.data as GetSiteResponse;
|
|
|
|
document.title = `${i18n.t('communities')} - ${data.site.name}`;
|
2019-10-19 00:20:27 +00:00
|
|
|
}
|
2019-04-04 20:00:19 +00:00
|
|
|
}
|
|
|
|
}
|