2021-02-22 02:39:04 +00:00
|
|
|
import { Component, linkEvent } from "inferno";
|
|
|
|
import { HtmlTags } from "./html-tags";
|
|
|
|
import { Subscription } from "rxjs";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
|
|
|
UserOperation,
|
2020-12-24 01:58:27 +00:00
|
|
|
CommunityView,
|
2020-09-06 16:15:25 +00:00
|
|
|
ListCommunitiesResponse,
|
|
|
|
CommunityResponse,
|
2020-12-24 01:58:27 +00:00
|
|
|
FollowCommunity,
|
|
|
|
ListCommunities,
|
2020-09-06 16:15:25 +00:00
|
|
|
SortType,
|
2021-01-26 15:59:38 +00:00
|
|
|
ListingType,
|
2020-12-24 01:58:27 +00:00
|
|
|
SiteView,
|
2021-02-22 02:39:04 +00:00
|
|
|
} from "lemmy-js-client";
|
|
|
|
import { WebSocketService } from "../services";
|
2020-09-07 03:41:46 +00:00
|
|
|
import {
|
|
|
|
wsJsonToRes,
|
|
|
|
toast,
|
|
|
|
getPageFromProps,
|
|
|
|
isBrowser,
|
2020-09-07 15:32:07 +00:00
|
|
|
setIsoData,
|
|
|
|
wsSubscribe,
|
2020-12-24 01:58:27 +00:00
|
|
|
wsUserOp,
|
2020-12-24 22:05:57 +00:00
|
|
|
wsClient,
|
|
|
|
authField,
|
|
|
|
setOptionalAuth,
|
2021-02-22 02:39:04 +00:00
|
|
|
} from "../utils";
|
|
|
|
import { CommunityLink } from "./community-link";
|
|
|
|
import { Spinner } from "./icon";
|
|
|
|
import { i18n } from "../i18next";
|
|
|
|
import { InitialFetchRequest } from "shared/interfaces";
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
const communityLimit = 100;
|
|
|
|
|
|
|
|
interface CommunitiesState {
|
2020-12-24 01:58:27 +00:00
|
|
|
communities: CommunityView[];
|
2020-09-06 16:15:25 +00:00
|
|
|
page: number;
|
|
|
|
loading: boolean;
|
2020-12-24 01:58:27 +00:00
|
|
|
site_view: SiteView;
|
2021-01-23 23:07:02 +00:00
|
|
|
searchText: string;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface CommunitiesProps {
|
|
|
|
page: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Communities extends Component<any, CommunitiesState> {
|
|
|
|
private subscription: Subscription;
|
2020-09-07 15:32:07 +00:00
|
|
|
private isoData = setIsoData(this.context);
|
2020-09-06 16:15:25 +00:00
|
|
|
private emptyState: CommunitiesState = {
|
|
|
|
communities: [],
|
|
|
|
loading: true,
|
|
|
|
page: getPageFromProps(this.props),
|
2020-12-24 01:58:27 +00:00
|
|
|
site_view: this.isoData.site_res.site_view,
|
2021-02-22 02:39:04 +00:00
|
|
|
searchText: "",
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
this.state = this.emptyState;
|
2020-09-07 03:41:46 +00:00
|
|
|
|
2020-09-07 22:24:48 +00:00
|
|
|
this.parseMessage = this.parseMessage.bind(this);
|
2020-09-07 15:32:07 +00:00
|
|
|
this.subscription = wsSubscribe(this.parseMessage);
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2020-09-07 03:41:46 +00:00
|
|
|
// Only fetch the data if coming from another route
|
2020-09-07 15:32:07 +00:00
|
|
|
if (this.isoData.path == this.context.router.route.match.url) {
|
|
|
|
this.state.communities = this.isoData.routeData[0].communities;
|
2020-09-07 03:41:46 +00:00
|
|
|
this.state.loading = false;
|
|
|
|
} else {
|
|
|
|
this.refetch();
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2020-09-07 03:41:46 +00:00
|
|
|
if (isBrowser()) {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static getDerivedStateFromProps(props: any): CommunitiesProps {
|
|
|
|
return {
|
|
|
|
page: getPageFromProps(props),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(_: any, lastState: CommunitiesState) {
|
|
|
|
if (lastState.page !== this.state.page) {
|
|
|
|
this.setState({ loading: true });
|
|
|
|
this.refetch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get documentTitle(): string {
|
2021-02-22 02:39:04 +00:00
|
|
|
return `${i18n.t("communities")} - ${this.state.site_view.site.name}`;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div class="container">
|
2020-09-11 18:09:21 +00:00
|
|
|
<HtmlTags
|
|
|
|
title={this.documentTitle}
|
|
|
|
path={this.context.router.route.match.url}
|
|
|
|
/>
|
2020-09-06 16:15:25 +00:00
|
|
|
{this.state.loading ? (
|
2021-02-11 20:35:27 +00:00
|
|
|
<h5>
|
|
|
|
<Spinner />
|
2020-09-06 16:15:25 +00:00
|
|
|
</h5>
|
|
|
|
) : (
|
|
|
|
<div>
|
2021-01-23 23:07:02 +00:00
|
|
|
<div class="row">
|
|
|
|
<div class="col-md-6">
|
2021-02-22 02:39:04 +00:00
|
|
|
<h4>{i18n.t("list_of_communities")}</h4>
|
2021-01-23 23:07:02 +00:00
|
|
|
</div>
|
|
|
|
<div class="col-md-6">
|
|
|
|
<div class="float-md-right">{this.searchForm()}</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
<div class="table-responsive">
|
|
|
|
<table id="community_table" class="table table-sm table-hover">
|
|
|
|
<thead class="pointer">
|
|
|
|
<tr>
|
2021-02-22 02:39:04 +00:00
|
|
|
<th>{i18n.t("name")}</th>
|
|
|
|
<th class="text-right">{i18n.t("subscribers")}</th>
|
2021-01-29 15:10:37 +00:00
|
|
|
<th class="text-right">
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("users")} / {i18n.t("month")}
|
2021-01-29 15:10:37 +00:00
|
|
|
</th>
|
2020-09-06 16:15:25 +00:00
|
|
|
<th class="text-right d-none d-lg-table-cell">
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("posts")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</th>
|
|
|
|
<th class="text-right d-none d-lg-table-cell">
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("comments")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</th>
|
|
|
|
<th></th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
2020-12-24 01:58:27 +00:00
|
|
|
{this.state.communities.map(cv => (
|
2020-09-06 16:15:25 +00:00
|
|
|
<tr>
|
|
|
|
<td>
|
2020-12-24 01:58:27 +00:00
|
|
|
<CommunityLink community={cv.community} />
|
2020-09-06 16:15:25 +00:00
|
|
|
</td>
|
2020-12-24 01:58:27 +00:00
|
|
|
<td class="text-right">{cv.counts.subscribers}</td>
|
2021-01-29 15:10:37 +00:00
|
|
|
<td class="text-right">{cv.counts.users_active_month}</td>
|
2020-09-06 16:15:25 +00:00
|
|
|
<td class="text-right d-none d-lg-table-cell">
|
2020-12-24 01:58:27 +00:00
|
|
|
{cv.counts.posts}
|
2020-09-06 16:15:25 +00:00
|
|
|
</td>
|
|
|
|
<td class="text-right d-none d-lg-table-cell">
|
2020-12-24 01:58:27 +00:00
|
|
|
{cv.counts.comments}
|
2020-09-06 16:15:25 +00:00
|
|
|
</td>
|
|
|
|
<td class="text-right">
|
2020-12-24 01:58:27 +00:00
|
|
|
{cv.subscribed ? (
|
2020-09-06 16:15:25 +00:00
|
|
|
<span
|
|
|
|
class="pointer btn-link"
|
2021-02-06 20:20:41 +00:00
|
|
|
role="button"
|
2020-09-06 16:15:25 +00:00
|
|
|
onClick={linkEvent(
|
2020-12-24 01:58:27 +00:00
|
|
|
cv.community.id,
|
2020-09-06 16:15:25 +00:00
|
|
|
this.handleUnsubscribe
|
|
|
|
)}
|
|
|
|
>
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("unsubscribe")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</span>
|
|
|
|
) : (
|
|
|
|
<span
|
|
|
|
class="pointer btn-link"
|
2021-02-06 20:20:41 +00:00
|
|
|
role="button"
|
2020-09-06 16:15:25 +00:00
|
|
|
onClick={linkEvent(
|
2020-12-24 01:58:27 +00:00
|
|
|
cv.community.id,
|
2020-09-06 16:15:25 +00:00
|
|
|
this.handleSubscribe
|
|
|
|
)}
|
|
|
|
>
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("subscribe")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
))}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
{this.paginator()}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-23 23:07:02 +00:00
|
|
|
searchForm() {
|
|
|
|
return (
|
|
|
|
<form
|
|
|
|
class="form-inline"
|
|
|
|
onSubmit={linkEvent(this, this.handleSearchSubmit)}
|
|
|
|
>
|
|
|
|
<input
|
|
|
|
type="text"
|
2021-02-09 16:21:24 +00:00
|
|
|
id="communities-search"
|
2021-01-23 23:07:02 +00:00
|
|
|
class="form-control mr-2 mb-2"
|
|
|
|
value={this.state.searchText}
|
2021-02-22 02:39:04 +00:00
|
|
|
placeholder={`${i18n.t("search")}...`}
|
2021-01-23 23:07:02 +00:00
|
|
|
onInput={linkEvent(this, this.handleSearchChange)}
|
|
|
|
required
|
|
|
|
minLength={3}
|
|
|
|
/>
|
2021-02-09 16:21:24 +00:00
|
|
|
<label class="sr-only" htmlFor="communities-search">
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("search")}
|
2021-02-09 16:21:24 +00:00
|
|
|
</label>
|
2021-01-23 23:07:02 +00:00
|
|
|
<button type="submit" class="btn btn-secondary mr-2 mb-2">
|
2021-02-22 02:39:04 +00:00
|
|
|
<span>{i18n.t("search")}</span>
|
2021-01-23 23:07:02 +00:00
|
|
|
</button>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
paginator() {
|
|
|
|
return (
|
|
|
|
<div class="mt-2">
|
|
|
|
{this.state.page > 1 && (
|
|
|
|
<button
|
|
|
|
class="btn btn-secondary mr-1"
|
|
|
|
onClick={linkEvent(this, this.prevPage)}
|
|
|
|
>
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("prev")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{this.state.communities.length > 0 && (
|
|
|
|
<button
|
|
|
|
class="btn btn-secondary"
|
|
|
|
onClick={linkEvent(this, this.nextPage)}
|
|
|
|
>
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("next")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateUrl(paramUpdates: CommunitiesProps) {
|
|
|
|
const page = paramUpdates.page || this.state.page;
|
|
|
|
this.props.history.push(`/communities/page/${page}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
nextPage(i: Communities) {
|
|
|
|
i.updateUrl({ page: i.state.page + 1 });
|
|
|
|
}
|
|
|
|
|
|
|
|
prevPage(i: Communities) {
|
|
|
|
i.updateUrl({ page: i.state.page - 1 });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleUnsubscribe(communityId: number) {
|
2020-12-24 01:58:27 +00:00
|
|
|
let form: FollowCommunity = {
|
2020-09-06 16:15:25 +00:00
|
|
|
community_id: communityId,
|
|
|
|
follow: false,
|
2020-12-24 22:05:57 +00:00
|
|
|
auth: authField(),
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
2020-12-24 22:05:57 +00:00
|
|
|
WebSocketService.Instance.send(wsClient.followCommunity(form));
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleSubscribe(communityId: number) {
|
2020-12-24 01:58:27 +00:00
|
|
|
let form: FollowCommunity = {
|
2020-09-06 16:15:25 +00:00
|
|
|
community_id: communityId,
|
|
|
|
follow: true,
|
2020-12-24 22:05:57 +00:00
|
|
|
auth: authField(),
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
2020-12-24 22:05:57 +00:00
|
|
|
WebSocketService.Instance.send(wsClient.followCommunity(form));
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2021-01-23 23:07:02 +00:00
|
|
|
handleSearchChange(i: Communities, event: any) {
|
|
|
|
i.setState({ searchText: event.target.value });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSearchSubmit(i: Communities) {
|
|
|
|
const searchParamEncoded = encodeURIComponent(i.state.searchText);
|
|
|
|
i.context.router.history.push(
|
2021-04-23 21:48:31 +00:00
|
|
|
`/search/q/${searchParamEncoded}/type/Communities/sort/TopAll/listing_type/All/community_id/0/creator_id/0/page/1`
|
2021-01-23 23:07:02 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
refetch() {
|
2020-12-24 01:58:27 +00:00
|
|
|
let listCommunitiesForm: ListCommunities = {
|
2021-01-26 15:59:38 +00:00
|
|
|
type_: ListingType.All,
|
2021-04-09 02:28:29 +00:00
|
|
|
sort: SortType.TopMonth,
|
2020-09-06 16:15:25 +00:00
|
|
|
limit: communityLimit,
|
|
|
|
page: this.state.page,
|
2020-12-24 22:05:57 +00:00
|
|
|
auth: authField(false),
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
|
|
|
|
2020-12-24 22:05:57 +00:00
|
|
|
WebSocketService.Instance.send(
|
|
|
|
wsClient.listCommunities(listCommunitiesForm)
|
|
|
|
);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2020-11-12 21:56:46 +00:00
|
|
|
static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
|
2021-02-22 02:39:04 +00:00
|
|
|
let pathSplit = req.path.split("/");
|
2020-09-07 15:32:07 +00:00
|
|
|
let page = pathSplit[3] ? Number(pathSplit[3]) : 1;
|
2020-12-24 01:58:27 +00:00
|
|
|
let listCommunitiesForm: ListCommunities = {
|
2021-01-26 15:59:38 +00:00
|
|
|
type_: ListingType.All,
|
2021-04-09 02:28:29 +00:00
|
|
|
sort: SortType.TopMonth,
|
2020-09-07 03:41:46 +00:00
|
|
|
limit: communityLimit,
|
|
|
|
page,
|
|
|
|
};
|
2020-12-24 22:05:57 +00:00
|
|
|
setOptionalAuth(listCommunitiesForm, req.auth);
|
2020-09-07 03:41:46 +00:00
|
|
|
|
2020-11-12 21:56:46 +00:00
|
|
|
return [req.client.listCommunities(listCommunitiesForm)];
|
2020-09-07 03:41:46 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
parseMessage(msg: any) {
|
|
|
|
let op = wsUserOp(msg);
|
2021-04-07 15:54:38 +00:00
|
|
|
console.log(msg);
|
2020-09-06 16:15:25 +00:00
|
|
|
if (msg.error) {
|
2021-02-22 02:39:04 +00:00
|
|
|
toast(i18n.t(msg.error), "danger");
|
2020-09-06 16:15:25 +00:00
|
|
|
return;
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.ListCommunities) {
|
|
|
|
let data = wsJsonToRes<ListCommunitiesResponse>(msg).data;
|
2020-09-06 16:15:25 +00:00
|
|
|
this.state.communities = data.communities;
|
|
|
|
this.state.loading = false;
|
|
|
|
window.scrollTo(0, 0);
|
|
|
|
this.setState(this.state);
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.FollowCommunity) {
|
|
|
|
let data = wsJsonToRes<CommunityResponse>(msg).data;
|
|
|
|
let found = this.state.communities.find(
|
|
|
|
c => c.community.id == data.community_view.community.id
|
|
|
|
);
|
|
|
|
found.subscribed = data.community_view.subscribed;
|
|
|
|
found.counts.subscribers = data.community_view.counts.subscribers;
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|