]> Untitled Git - lemmy-ui.git/blobdiff - src/shared/components/post/create-post.tsx
Merge branch 'main' into route-data-refactor
[lemmy-ui.git] / src / shared / components / post / create-post.tsx
index 71fac79aed8668bc9f694df463ee884628eb8419..bb39cdadbdab3dae9a162c0325997b43f25a34e0 100644 (file)
@@ -3,6 +3,7 @@ import { RouteComponentProps } from "inferno-router/dist/Route";
 import {
   CreatePost as CreatePostI,
   GetCommunity,
+  GetCommunityResponse,
   GetSiteResponse,
   ListCommunitiesResponse,
 } from "lemmy-js-client";
@@ -17,6 +18,7 @@ import {
 import {
   Choice,
   QueryParams,
+  RouteDataResponse,
   enableDownvotes,
   enableNsfw,
   getIdFromString,
@@ -32,6 +34,11 @@ export interface CreatePostProps {
   communityId?: number;
 }
 
+type CreatePostData = RouteDataResponse<{
+  communityResponse?: GetCommunityResponse;
+  initialCommunitiesRes: ListCommunitiesResponse;
+}>;
+
 function getCreatePostQueryParams() {
   return getQueryParams<CreatePostProps>({
     communityId: getIdFromString,
@@ -54,7 +61,7 @@ export class CreatePost extends Component<
   RouteComponentProps<Record<string, never>>,
   CreatePostState
 > {
-  private isoData = setIsoData(this.context);
+  private isoData = setIsoData<CreatePostData>(this.context);
   state: CreatePostState = {
     siteRes: this.isoData.site_res,
     loading: true,
@@ -71,7 +78,8 @@ export class CreatePost extends Component<
 
     // Only fetch the data if coming from another route
     if (FirstLoadService.isFirstLoad) {
-      const [communityRes, listCommunitiesRes] = this.isoData.routeData;
+      const { communityResponse: communityRes, initialCommunitiesRes } =
+        this.isoData.routeData;
 
       if (communityRes?.state === "success") {
         const communityChoice: Choice = {
@@ -88,7 +96,7 @@ export class CreatePost extends Component<
       this.state = {
         ...this.state,
         loading: false,
-        initialCommunitiesRes: listCommunitiesRes,
+        initialCommunitiesRes,
         isIsomorphic: true,
       };
     }
@@ -227,14 +235,16 @@ export class CreatePost extends Component<
     }
   }
 
-  static fetchInitialData({
+  static async fetchInitialData({
     client,
     query: { communityId },
     auth,
-  }: InitialFetchRequest<QueryParams<CreatePostProps>>): Promise<
-    RequestState<any>
-  >[] {
-    const promises: Promise<RequestState<any>>[] = [];
+  }: InitialFetchRequest<
+    QueryParams<CreatePostProps>
+  >): Promise<CreatePostData> {
+    const data: CreatePostData = {
+      initialCommunitiesRes: await fetchCommunitiesForOptions(client),
+    };
 
     if (communityId) {
       const form: GetCommunity = {
@@ -242,13 +252,9 @@ export class CreatePost extends Component<
         id: getIdFromString(communityId),
       };
 
-      promises.push(client.getCommunity(form));
-    } else {
-      promises.push(Promise.resolve({ state: "empty" }));
+      data.communityResponse = await client.getCommunity(form);
     }
 
-    promises.push(fetchCommunitiesForOptions(client));
-
-    return promises;
+    return data;
   }
 }