2021-02-22 02:39:04 +00:00
|
|
|
import { Component, linkEvent } from "inferno";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
|
|
|
CommunityResponse,
|
2021-07-17 20:42:55 +00:00
|
|
|
CommunityView,
|
2020-12-24 01:58:27 +00:00
|
|
|
FollowCommunity,
|
|
|
|
ListCommunities,
|
2021-07-17 20:42:55 +00:00
|
|
|
ListCommunitiesResponse,
|
2021-01-26 15:59:38 +00:00
|
|
|
ListingType,
|
2020-12-24 01:58:27 +00:00
|
|
|
SiteView,
|
2021-07-17 20:42:55 +00:00
|
|
|
SortType,
|
|
|
|
UserOperation,
|
2021-02-22 02:39:04 +00:00
|
|
|
} from "lemmy-js-client";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { Subscription } from "rxjs";
|
|
|
|
import { InitialFetchRequest } from "shared/interfaces";
|
|
|
|
import { i18n } from "../../i18next";
|
2021-07-19 13:04:06 +00:00
|
|
|
import { WebSocketService } from "../../services";
|
2020-09-07 03:41:46 +00:00
|
|
|
import {
|
2021-07-17 20:42:55 +00:00
|
|
|
authField,
|
2021-07-19 13:04:06 +00:00
|
|
|
getListingTypeFromPropsNoDefault,
|
2020-09-07 03:41:46 +00:00
|
|
|
getPageFromProps,
|
|
|
|
isBrowser,
|
2021-09-18 16:35:49 +00:00
|
|
|
numToSI,
|
2020-09-07 15:32:07 +00:00
|
|
|
setIsoData,
|
2021-07-17 20:42:55 +00:00
|
|
|
setOptionalAuth,
|
2021-07-18 16:17:50 +00:00
|
|
|
showLocal,
|
2021-07-17 20:42:55 +00:00
|
|
|
toast,
|
|
|
|
wsClient,
|
|
|
|
wsJsonToRes,
|
2020-09-07 15:32:07 +00:00
|
|
|
wsSubscribe,
|
2020-12-24 01:58:27 +00:00
|
|
|
wsUserOp,
|
2021-07-17 20:42:55 +00:00
|
|
|
} from "../../utils";
|
|
|
|
import { HtmlTags } from "../common/html-tags";
|
|
|
|
import { Spinner } from "../common/icon";
|
2021-07-18 16:17:50 +00:00
|
|
|
import { ListingTypeSelect } from "../common/listing-type-select";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { Paginator } from "../common/paginator";
|
2021-02-22 02:39:04 +00:00
|
|
|
import { CommunityLink } from "./community-link";
|
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;
|
2021-07-18 16:17:50 +00:00
|
|
|
listingType: ListingType;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface CommunitiesProps {
|
2021-07-18 16:17:50 +00:00
|
|
|
listingType?: ListingType;
|
|
|
|
page?: number;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
2021-07-19 13:04:06 +00:00
|
|
|
listingType: getListingTypeFromPropsNoDefault(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;
|
2021-07-16 19:40:56 +00:00
|
|
|
this.handlePageChange = this.handlePageChange.bind(this);
|
2021-07-18 16:17:50 +00:00
|
|
|
this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
|
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 {
|
2021-07-19 13:04:06 +00:00
|
|
|
listingType: getListingTypeFromPropsNoDefault(props),
|
2020-09-06 16:15:25 +00:00
|
|
|
page: getPageFromProps(props),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(_: any, lastState: CommunitiesState) {
|
2021-07-18 16:17:50 +00:00
|
|
|
if (
|
|
|
|
lastState.page !== this.state.page ||
|
|
|
|
lastState.listingType !== this.state.listingType
|
|
|
|
) {
|
2020-09-06 16:15:25 +00:00
|
|
|
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>
|
2021-07-17 20:21:31 +00:00
|
|
|
<Spinner large />
|
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-07-18 16:17:50 +00:00
|
|
|
<span class="mb-2">
|
|
|
|
<ListingTypeSelect
|
|
|
|
type_={this.state.listingType}
|
|
|
|
showLocal={showLocal(this.isoData)}
|
|
|
|
onChange={this.handleListingTypeChange}
|
|
|
|
/>
|
|
|
|
</span>
|
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>
|
2021-09-18 16:35:49 +00:00
|
|
|
<td class="text-right">
|
|
|
|
{numToSI(cv.counts.subscribers)}
|
|
|
|
</td>
|
|
|
|
<td class="text-right">
|
|
|
|
{numToSI(cv.counts.users_active_month)}
|
|
|
|
</td>
|
2020-09-06 16:15:25 +00:00
|
|
|
<td class="text-right d-none d-lg-table-cell">
|
2021-09-18 16:35:49 +00:00
|
|
|
{numToSI(cv.counts.posts)}
|
2020-09-06 16:15:25 +00:00
|
|
|
</td>
|
|
|
|
<td class="text-right d-none d-lg-table-cell">
|
2021-09-18 16:35:49 +00:00
|
|
|
{numToSI(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 ? (
|
2021-07-17 20:42:55 +00:00
|
|
|
<button
|
|
|
|
class="btn btn-link d-inline-block"
|
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")}
|
2021-07-17 20:42:55 +00:00
|
|
|
</button>
|
2020-09-06 16:15:25 +00:00
|
|
|
) : (
|
2021-07-17 20:42:55 +00:00
|
|
|
<button
|
|
|
|
class="btn btn-link d-inline-block"
|
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")}
|
2021-07-17 20:42:55 +00:00
|
|
|
</button>
|
2020-09-06 16:15:25 +00:00
|
|
|
)}
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
))}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
2021-07-16 19:40:56 +00:00
|
|
|
<Paginator
|
|
|
|
page={this.state.page}
|
|
|
|
onChange={this.handlePageChange}
|
|
|
|
/>
|
2020-09-06 16:15:25 +00:00
|
|
|
</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
|
|
|
updateUrl(paramUpdates: CommunitiesProps) {
|
|
|
|
const page = paramUpdates.page || this.state.page;
|
2021-07-18 16:17:50 +00:00
|
|
|
const listingTypeStr = paramUpdates.listingType || this.state.listingType;
|
|
|
|
this.props.history.push(
|
|
|
|
`/communities/listing_type/${listingTypeStr}/page/${page}`
|
|
|
|
);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 19:40:56 +00:00
|
|
|
handlePageChange(page: number) {
|
|
|
|
this.updateUrl({ page });
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2021-07-18 16:17:50 +00:00
|
|
|
handleListingTypeChange(val: ListingType) {
|
|
|
|
this.updateUrl({
|
|
|
|
listingType: val,
|
|
|
|
page: 1,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
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-07-18 16:17:50 +00:00
|
|
|
type_: this.state.listingType,
|
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("/");
|
2021-07-18 16:17:50 +00:00
|
|
|
let type_: ListingType = pathSplit[3]
|
|
|
|
? ListingType[pathSplit[3]]
|
|
|
|
: ListingType.Local;
|
|
|
|
let page = pathSplit[5] ? Number(pathSplit[5]) : 1;
|
2020-12-24 01:58:27 +00:00
|
|
|
let listCommunitiesForm: ListCommunities = {
|
2021-07-18 16:17:50 +00:00
|
|
|
type_,
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|