mirror of
https://github.com/LemmyNet/lemmy-ui.git
synced 2024-11-02 02:29:59 +00:00
2b1af707c3
* Beginning work on websocket -> http client conversion. * About 30% done. * half done. * more done. * Almost passing lint. * Passing lint, but untested. * Add back in event listeners. * Fixing some community forms. * Remove webpack cache. * fixing some more. * Fixed ISOwrappers. * A few more fixes. * Refactor utils * Fix instance add/remove buttons * Not catching errors in isoWrapper. * Wrap Http client * Fixing up tagline and ratelimit forms. * Make all http client wrapping be in one place * Reworking some more forms. * Upgrading lemmy-js-client. * Fixing verify email. * Fix linting errors * Upgrading woodpecker node. * Fix comment scrolling rerender bug. * Fixing a few things, commenting out props for now. * v0.18.0-beta.1 * Trying to fix woodpecker, 1. * Trying to fix woodpecker, 2. * Handroll prompt * Add navigation prompt to other pages * Fix prompt navigation bug * Fix prompt bug introduced from last bug fix * Fix PWA bug * Fix isoData not working * Fix search page update url * Fix sharp issue. * v0.18.0-beta.2 * Make create post pre-fetch communities * Fix bug from last commit * Fix issue of posts/comments not being switched when changing select options * Fix unnecessary fetches on home screen * Make circular icon buttons not look stupid * Prevent unnecessary fetches * Make login experience smoother * Add PWA shortcuts * Add related application to PWA * Update translations * Forgot to add post editing. * Fixing site setup. * Deploy script setup. * v0.18.0-beta.4 * Sanitize again. * Adding sanitize json function. * Upping version. * Another sanitize fix. * Upping version. * Prevent search nav item from disappearing when on search page * Allow admin and mod actions on non-local comments. * Fix mobile menu collapse bug * Completely fix prompt component * Fix undefined value checks in use_http_client_2 (#1230) * fix: filter out undefined from posts * fix: emoji initialisation passing undefined * fix: || => ?? to be more explicit * linting --------- Co-authored-by: Alex Maras <alexmaras@gmail.com> * Re-add accidentally removed state * Fix dropdown bug * Use linkEvent where appropriate * Fix navigation warnings. --------- Co-authored-by: Dessalines <tyhou13@gmx.com> Co-authored-by: Alex Maras <dev@alexmaras.com> Co-authored-by: Alex Maras <alexmaras@gmail.com>
347 lines
10 KiB
TypeScript
347 lines
10 KiB
TypeScript
import { Component, linkEvent } from "inferno";
|
|
import {
|
|
CommunityResponse,
|
|
GetSiteResponse,
|
|
ListCommunities,
|
|
ListCommunitiesResponse,
|
|
ListingType,
|
|
} from "lemmy-js-client";
|
|
import { i18n } from "../../i18next";
|
|
import { InitialFetchRequest } from "../../interfaces";
|
|
import { FirstLoadService } from "../../services/FirstLoadService";
|
|
import { HttpService, RequestState } from "../../services/HttpService";
|
|
import {
|
|
QueryParams,
|
|
editCommunity,
|
|
getPageFromString,
|
|
getQueryParams,
|
|
getQueryString,
|
|
myAuth,
|
|
myAuthRequired,
|
|
numToSI,
|
|
setIsoData,
|
|
showLocal,
|
|
} from "../../utils";
|
|
import { HtmlTags } from "../common/html-tags";
|
|
import { Spinner } from "../common/icon";
|
|
import { ListingTypeSelect } from "../common/listing-type-select";
|
|
import { Paginator } from "../common/paginator";
|
|
import { CommunityLink } from "./community-link";
|
|
|
|
const communityLimit = 50;
|
|
|
|
interface CommunitiesState {
|
|
listCommunitiesResponse: RequestState<ListCommunitiesResponse>;
|
|
siteRes: GetSiteResponse;
|
|
searchText: string;
|
|
isIsomorphic: boolean;
|
|
}
|
|
|
|
interface CommunitiesProps {
|
|
listingType: ListingType;
|
|
page: number;
|
|
}
|
|
|
|
function getListingTypeFromQuery(listingType?: string): ListingType {
|
|
return listingType ? (listingType as ListingType) : "Local";
|
|
}
|
|
|
|
export class Communities extends Component<any, CommunitiesState> {
|
|
private isoData = setIsoData(this.context);
|
|
state: CommunitiesState = {
|
|
listCommunitiesResponse: { state: "empty" },
|
|
siteRes: this.isoData.site_res,
|
|
searchText: "",
|
|
isIsomorphic: false,
|
|
};
|
|
|
|
constructor(props: any, context: any) {
|
|
super(props, context);
|
|
this.handlePageChange = this.handlePageChange.bind(this);
|
|
this.handleListingTypeChange = this.handleListingTypeChange.bind(this);
|
|
|
|
// Only fetch the data if coming from another route
|
|
if (FirstLoadService.isFirstLoad) {
|
|
this.state = {
|
|
...this.state,
|
|
listCommunitiesResponse: this.isoData.routeData[0],
|
|
isIsomorphic: true,
|
|
};
|
|
}
|
|
}
|
|
|
|
async componentDidMount() {
|
|
if (!this.state.isIsomorphic) {
|
|
await this.refetch();
|
|
}
|
|
}
|
|
|
|
get documentTitle(): string {
|
|
return `${i18n.t("communities")} - ${
|
|
this.state.siteRes.site_view.site.name
|
|
}`;
|
|
}
|
|
|
|
renderListings() {
|
|
switch (this.state.listCommunitiesResponse.state) {
|
|
case "loading":
|
|
return (
|
|
<h5>
|
|
<Spinner large />
|
|
</h5>
|
|
);
|
|
case "success": {
|
|
const { listingType, page } = this.getCommunitiesQueryParams();
|
|
return (
|
|
<div>
|
|
<div className="row">
|
|
<div className="col-md-6">
|
|
<h4>{i18n.t("list_of_communities")}</h4>
|
|
<span className="mb-2">
|
|
<ListingTypeSelect
|
|
type_={listingType}
|
|
showLocal={showLocal(this.isoData)}
|
|
showSubscribed
|
|
onChange={this.handleListingTypeChange}
|
|
/>
|
|
</span>
|
|
</div>
|
|
<div className="col-md-6">
|
|
<div className="float-md-right">{this.searchForm()}</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="table-responsive">
|
|
<table
|
|
id="community_table"
|
|
className="table table-sm table-hover"
|
|
>
|
|
<thead className="pointer">
|
|
<tr>
|
|
<th>{i18n.t("name")}</th>
|
|
<th className="text-right">{i18n.t("subscribers")}</th>
|
|
<th className="text-right">
|
|
{i18n.t("users")} / {i18n.t("month")}
|
|
</th>
|
|
<th className="text-right d-none d-lg-table-cell">
|
|
{i18n.t("posts")}
|
|
</th>
|
|
<th className="text-right d-none d-lg-table-cell">
|
|
{i18n.t("comments")}
|
|
</th>
|
|
<th></th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{this.state.listCommunitiesResponse.data.communities.map(
|
|
cv => (
|
|
<tr key={cv.community.id}>
|
|
<td>
|
|
<CommunityLink community={cv.community} />
|
|
</td>
|
|
<td className="text-right">
|
|
{numToSI(cv.counts.subscribers)}
|
|
</td>
|
|
<td className="text-right">
|
|
{numToSI(cv.counts.users_active_month)}
|
|
</td>
|
|
<td className="text-right d-none d-lg-table-cell">
|
|
{numToSI(cv.counts.posts)}
|
|
</td>
|
|
<td className="text-right d-none d-lg-table-cell">
|
|
{numToSI(cv.counts.comments)}
|
|
</td>
|
|
<td className="text-right">
|
|
{cv.subscribed == "Subscribed" && (
|
|
<button
|
|
className="btn btn-link d-inline-block"
|
|
onClick={linkEvent(
|
|
{
|
|
i: this,
|
|
communityId: cv.community.id,
|
|
follow: false,
|
|
},
|
|
this.handleFollow
|
|
)}
|
|
>
|
|
{i18n.t("unsubscribe")}
|
|
</button>
|
|
)}
|
|
{cv.subscribed === "NotSubscribed" && (
|
|
<button
|
|
className="btn btn-link d-inline-block"
|
|
onClick={linkEvent(
|
|
{
|
|
i: this,
|
|
communityId: cv.community.id,
|
|
follow: true,
|
|
},
|
|
this.handleFollow
|
|
)}
|
|
>
|
|
{i18n.t("subscribe")}
|
|
</button>
|
|
)}
|
|
{cv.subscribed === "Pending" && (
|
|
<div className="text-warning d-inline-block">
|
|
{i18n.t("subscribe_pending")}
|
|
</div>
|
|
)}
|
|
</td>
|
|
</tr>
|
|
)
|
|
)}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<Paginator page={page} onChange={this.handlePageChange} />
|
|
</div>
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
render() {
|
|
return (
|
|
<div className="container-lg">
|
|
<HtmlTags
|
|
title={this.documentTitle}
|
|
path={this.context.router.route.match.url}
|
|
/>
|
|
{this.renderListings()}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
searchForm() {
|
|
return (
|
|
<form
|
|
className="form-inline"
|
|
onSubmit={linkEvent(this, this.handleSearchSubmit)}
|
|
>
|
|
<input
|
|
type="text"
|
|
id="communities-search"
|
|
className="form-control mr-2 mb-2"
|
|
value={this.state.searchText}
|
|
placeholder={`${i18n.t("search")}...`}
|
|
onInput={linkEvent(this, this.handleSearchChange)}
|
|
required
|
|
minLength={3}
|
|
/>
|
|
<label className="sr-only" htmlFor="communities-search">
|
|
{i18n.t("search")}
|
|
</label>
|
|
<button type="submit" className="btn btn-secondary mr-2 mb-2">
|
|
<span>{i18n.t("search")}</span>
|
|
</button>
|
|
</form>
|
|
);
|
|
}
|
|
|
|
async updateUrl({ listingType, page }: Partial<CommunitiesProps>) {
|
|
const { listingType: urlListingType, page: urlPage } =
|
|
this.getCommunitiesQueryParams();
|
|
|
|
const queryParams: QueryParams<CommunitiesProps> = {
|
|
listingType: listingType ?? urlListingType,
|
|
page: (page ?? urlPage)?.toString(),
|
|
};
|
|
|
|
this.props.history.push(`/communities${getQueryString(queryParams)}`);
|
|
|
|
await this.refetch();
|
|
}
|
|
|
|
handlePageChange(page: number) {
|
|
this.updateUrl({ page });
|
|
}
|
|
|
|
handleListingTypeChange(val: ListingType) {
|
|
this.updateUrl({
|
|
listingType: val,
|
|
page: 1,
|
|
});
|
|
}
|
|
|
|
handleSearchChange(i: Communities, event: any) {
|
|
i.setState({ searchText: event.target.value });
|
|
}
|
|
|
|
handleSearchSubmit(i: Communities, event: any) {
|
|
event.preventDefault();
|
|
const searchParamEncoded = encodeURIComponent(i.state.searchText);
|
|
i.context.router.history.push(`/search?q=${searchParamEncoded}`);
|
|
}
|
|
|
|
static fetchInitialData({
|
|
query: { listingType, page },
|
|
client,
|
|
auth,
|
|
}: InitialFetchRequest<QueryParams<CommunitiesProps>>): Promise<
|
|
RequestState<any>
|
|
>[] {
|
|
const listCommunitiesForm: ListCommunities = {
|
|
type_: getListingTypeFromQuery(listingType),
|
|
sort: "TopMonth",
|
|
limit: communityLimit,
|
|
page: getPageFromString(page),
|
|
auth: auth,
|
|
};
|
|
|
|
return [client.listCommunities(listCommunitiesForm)];
|
|
}
|
|
|
|
getCommunitiesQueryParams() {
|
|
return getQueryParams<CommunitiesProps>({
|
|
listingType: getListingTypeFromQuery,
|
|
page: getPageFromString,
|
|
});
|
|
}
|
|
|
|
async handleFollow(data: {
|
|
i: Communities;
|
|
communityId: number;
|
|
follow: boolean;
|
|
}) {
|
|
const res = await HttpService.client.followCommunity({
|
|
community_id: data.communityId,
|
|
follow: data.follow,
|
|
auth: myAuthRequired(),
|
|
});
|
|
data.i.findAndUpdateCommunity(res);
|
|
}
|
|
|
|
async refetch() {
|
|
this.setState({ listCommunitiesResponse: { state: "loading" } });
|
|
|
|
const { listingType, page } = this.getCommunitiesQueryParams();
|
|
|
|
this.setState({
|
|
listCommunitiesResponse: await HttpService.client.listCommunities({
|
|
type_: listingType,
|
|
sort: "TopMonth",
|
|
limit: communityLimit,
|
|
page,
|
|
auth: myAuth(),
|
|
}),
|
|
});
|
|
|
|
window.scrollTo(0, 0);
|
|
}
|
|
|
|
findAndUpdateCommunity(res: RequestState<CommunityResponse>) {
|
|
this.setState(s => {
|
|
if (
|
|
s.listCommunitiesResponse.state == "success" &&
|
|
res.state == "success"
|
|
) {
|
|
s.listCommunitiesResponse.data.communities = editCommunity(
|
|
res.data.community_view,
|
|
s.listCommunitiesResponse.data.communities
|
|
);
|
|
}
|
|
return s;
|
|
});
|
|
}
|
|
}
|