X-Git-Url: http://these/git/?a=blobdiff_plain;f=src%2Fshared%2Fcomponents%2Fcommunity%2Fcommunity.tsx;h=b88d6f6100e03843ef387ad60a3c8d524a0b1650;hb=53c3cfeade90150b07431386745a24aa699a25ec;hp=f2d7ad729a8e492b2944205314ae6e47e88c8dca;hpb=e540b1e4a92971a42ab9867e92a4af7904a7cfd0;p=lemmy-ui.git diff --git a/src/shared/components/community/community.tsx b/src/shared/components/community/community.tsx index f2d7ad7..b88d6f6 100644 --- a/src/shared/components/community/community.tsx +++ b/src/shared/components/community/community.tsx @@ -1,4 +1,28 @@ -import { Component, linkEvent } from "inferno"; +import { + commentsToFlatNodes, + communityRSSUrl, + editComment, + editPost, + editWith, + enableDownvotes, + enableNsfw, + getCommentParentId, + getDataTypeString, + myAuth, + postToCommentSortType, + setIsoData, + showLocal, + updateCommunityBlock, + updatePersonBlock, +} from "@utils/app"; +import { + getPageFromString, + getQueryParams, + getQueryString, +} from "@utils/helpers"; +import type { QueryParams } from "@utils/types"; +import { RouteDataResponse } from "@utils/types"; +import { Component, RefObject, createRef, linkEvent } from "inferno"; import { RouteComponentProps } from "inferno-router/dist/Route"; import { AddAdmin, @@ -52,42 +76,16 @@ import { SortType, TransferCommunity, } from "lemmy-js-client"; -import { i18n } from "../../i18next"; +import { fetchLimit, relTags } from "../../config"; import { CommentViewType, DataType, InitialFetchRequest, } from "../../interfaces"; -import { UserService } from "../../services"; -import { FirstLoadService } from "../../services/FirstLoadService"; +import { FirstLoadService, I18NextService, UserService } from "../../services"; import { HttpService, RequestState } from "../../services/HttpService"; -import { - QueryParams, - commentsToFlatNodes, - communityRSSUrl, - editComment, - editPost, - editWith, - enableDownvotes, - enableNsfw, - fetchLimit, - getCommentParentId, - getDataTypeString, - getPageFromString, - getQueryParams, - getQueryString, - myAuth, - postToCommentSortType, - relTags, - restoreScrollPosition, - saveScrollPosition, - setIsoData, - setupTippy, - showLocal, - toast, - updateCommunityBlock, - updatePersonBlock, -} from "../../utils"; +import { setupTippy } from "../../tippy"; +import { toast } from "../../toast"; import { CommentNodes } from "../comment/comment-nodes"; import { BannerIconHeader } from "../common/banner-icon-header"; import { DataTypeSelect } from "../common/data-type-select"; @@ -100,6 +98,12 @@ import { SiteSidebar } from "../home/site-sidebar"; import { PostListings } from "../post/post-listings"; import { CommunityLink } from "./community-link"; +type CommunityData = RouteDataResponse<{ + communityRes: GetCommunityResponse; + postsRes: GetPostsResponse; + commentsRes: GetCommentsResponse; +}>; + interface State { communityRes: RequestState; postsRes: RequestState; @@ -140,7 +144,7 @@ export class Community extends Component< RouteComponentProps<{ name: string }>, State > { - private isoData = setIsoData(this.context); + private isoData = setIsoData(this.context); state: State = { communityRes: { state: "empty" }, postsRes: { state: "empty" }, @@ -150,7 +154,7 @@ export class Community extends Component< finished: new Map(), isIsomorphic: false, }; - + private readonly mainContentRef: RefObject; constructor(props: RouteComponentProps<{ name: string }>, context: any) { super(props, context); @@ -191,16 +195,17 @@ export class Community extends Component< this.handleSavePost = this.handleSavePost.bind(this); this.handlePurgePost = this.handlePurgePost.bind(this); this.handleFeaturePost = this.handleFeaturePost.bind(this); - + this.mainContentRef = createRef(); // Only fetch the data if coming from another route if (FirstLoadService.isFirstLoad) { - const [communityRes, postsRes, commentsRes] = this.isoData.routeData; + const { communityRes, commentsRes, postsRes } = this.isoData.routeData; + this.state = { ...this.state, + isIsomorphic: true, + commentsRes, communityRes, postsRes, - commentsRes, - isIsomorphic: true, }; } } @@ -223,27 +228,21 @@ export class Community extends Component< setupTippy(); } - componentWillUnmount() { - saveScrollPosition(this.context); - } - - static fetchInitialData({ + static async fetchInitialData({ client, path, query: { dataType: urlDataType, page: urlPage, sort: urlSort }, auth, }: InitialFetchRequest>): Promise< - RequestState - >[] { + Promise + > { const pathSplit = path.split("/"); - const promises: Promise>[] = []; const communityName = pathSplit[2]; const communityForm: GetCommunity = { name: communityName, auth, }; - promises.push(client.getCommunity(communityForm)); const dataType = getDataTypeFromQuery(urlDataType); @@ -251,6 +250,11 @@ export class Community extends Component< const page = getPageFromString(urlPage); + let postsResponse: RequestState = { state: "empty" }; + let commentsResponse: RequestState = { + state: "empty", + }; + if (dataType === DataType.Post) { const getPostsForm: GetPosts = { community_name: communityName, @@ -261,8 +265,8 @@ export class Community extends Component< saved_only: false, auth, }; - promises.push(client.getPosts(getPostsForm)); - promises.push(Promise.resolve({ state: "empty" })); + + postsResponse = await client.getPosts(getPostsForm); } else { const getCommentsForm: GetComments = { community_name: communityName, @@ -273,11 +277,15 @@ export class Community extends Component< saved_only: false, auth, }; - promises.push(Promise.resolve({ state: "empty" })); - promises.push(client.getComments(getCommentsForm)); + + commentsResponse = await client.getComments(getCommentsForm); } - return promises; + return { + communityRes: await client.getCommunity(communityForm), + commentsRes: commentsResponse, + postsRes: postsResponse, + }; } get documentTitle(): string { @@ -304,19 +312,23 @@ export class Community extends Component<
-
+
{this.communityInfo(res)}
-
+
+
+
); @@ -343,7 +355,9 @@ export class Community extends Component< } render() { - return
{this.renderCommunity()}
; + return ( +
{this.renderCommunity()}
+ ); } sidebar(res: GetCommunityResponse) { @@ -472,7 +486,7 @@ export class Community extends Component< community && (
-
{community.title}
+

{community.title}

- + - + {communityRss && ( @@ -594,7 +608,6 @@ export class Community extends Component< }); } - restoreScrollPosition(this.context); setupTippy(); } @@ -741,14 +754,14 @@ export class Community extends Component< async handleCommentReport(form: CreateCommentReport) { const reportRes = await HttpService.client.createCommentReport(form); if (reportRes.state == "success") { - toast(i18n.t("report_created")); + toast(I18NextService.i18n.t("report_created")); } } async handlePostReport(form: CreatePostReport) { const reportRes = await HttpService.client.createPostReport(form); if (reportRes.state == "success") { - toast(i18n.t("report_created")); + toast(I18NextService.i18n.t("report_created")); } } @@ -774,7 +787,7 @@ export class Community extends Component< const transferCommunityRes = await HttpService.client.transferCommunity( form ); - toast(i18n.t("transfer_community")); + toast(I18NextService.i18n.t("transfer_community")); this.updateCommunityFull(transferCommunityRes); } @@ -863,7 +876,7 @@ export class Community extends Component< purgeItem(purgeRes: RequestState) { if (purgeRes.state == "success") { - toast(i18n.t("purge_success")); + toast(I18NextService.i18n.t("purge_success")); this.context.router.history.push(`/`); } }