]> Untitled Git - lemmy-ui.git/blobdiff - src/shared/components/community/community.tsx
Merge branch 'main' into route-data-refactor
[lemmy-ui.git] / src / shared / components / community / community.tsx
index 7dc150f332fd89b40a8635c79e526ffa53e3b3ed..e8412e76842f9481a8b07654642b799f11ac716d 100644 (file)
@@ -63,6 +63,7 @@ import { FirstLoadService } from "../../services/FirstLoadService";
 import { HttpService, RequestState } from "../../services/HttpService";
 import {
   QueryParams,
+  RouteDataResponse,
   commentsToFlatNodes,
   communityRSSUrl,
   editComment,
@@ -100,6 +101,12 @@ import { SiteSidebar } from "../home/site-sidebar";
 import { PostListings } from "../post/post-listings";
 import { CommunityLink } from "./community-link";
 
+type CommunityData = RouteDataResponse<{
+  communityResponse: GetCommunityResponse;
+  postsResponse?: GetPostsResponse;
+  commentsResponse?: GetCommentsResponse;
+}>;
+
 interface State {
   communityRes: RequestState<GetCommunityResponse>;
   postsRes: RequestState<GetPostsResponse>;
@@ -140,7 +147,7 @@ export class Community extends Component<
   RouteComponentProps<{ name: string }>,
   State
 > {
-  private isoData = setIsoData(this.context);
+  private isoData = setIsoData<CommunityData>(this.context);
   state: State = {
     communityRes: { state: "empty" },
     postsRes: { state: "empty" },
@@ -194,14 +201,37 @@ export class Community extends Component<
 
     // Only fetch the data if coming from another route
     if (FirstLoadService.isFirstLoad) {
-      const [communityRes, postsRes, commentsRes] = this.isoData.routeData;
+      const {
+        communityResponse: communityRes,
+        commentsResponse: commentsRes,
+        postsResponse: postsRes,
+      } = this.isoData.routeData;
+
       this.state = {
         ...this.state,
-        communityRes,
-        postsRes,
-        commentsRes,
         isIsomorphic: true,
       };
+
+      if (communityRes.state === "success") {
+        this.state = {
+          ...this.state,
+          communityRes,
+        };
+      }
+
+      if (postsRes?.state === "success") {
+        this.state = {
+          ...this.state,
+          postsRes,
+        };
+      }
+
+      if (commentsRes?.state === "success") {
+        this.state = {
+          ...this.state,
+          commentsRes,
+        };
+      }
     }
   }
 
@@ -227,23 +257,21 @@ export class Community extends Component<
     saveScrollPosition(this.context);
   }
 
-  static fetchInitialData({
+  static async fetchInitialData({
     client,
     path,
     query: { dataType: urlDataType, page: urlPage, sort: urlSort },
     auth,
   }: InitialFetchRequest<QueryParams<CommunityProps>>): Promise<
-    RequestState<any>
-  >[] {
+    Promise<CommunityData>
+  > {
     const pathSplit = path.split("/");
-    const promises: Promise<RequestState<any>>[] = [];
 
     const communityName = pathSplit[2];
     const communityForm: GetCommunity = {
       name: communityName,
       auth,
     };
-    promises.push(client.getCommunity(communityForm));
 
     const dataType = getDataTypeFromQuery(urlDataType);
 
@@ -251,6 +279,10 @@ export class Community extends Component<
 
     const page = getPageFromString(urlPage);
 
+    let postsResponse: RequestState<GetPostsResponse> | undefined = undefined;
+    let commentsResponse: RequestState<GetCommentsResponse> | undefined =
+      undefined;
+
     if (dataType === DataType.Post) {
       const getPostsForm: GetPosts = {
         community_name: communityName,
@@ -261,8 +293,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 +305,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 {
+      communityResponse: await client.getCommunity(communityForm),
+      commentsResponse,
+      postsResponse,
+    };
   }
 
   get documentTitle(): string {