]> Untitled Git - lemmy-ui.git/blobdiff - src/shared/components/modlog.tsx
Merge branch 'main' into route-data-refactor
[lemmy-ui.git] / src / shared / components / modlog.tsx
index d917f5f35ed675842e43169fa6da129b696335b6..48be10b6245849c0b511bc9ba9137599eb364566 100644 (file)
@@ -13,6 +13,7 @@ import {
   GetModlog,
   GetModlogResponse,
   GetPersonDetails,
+  GetPersonDetailsResponse,
   ModAddCommunityView,
   ModAddView,
   ModBanFromCommunityView,
@@ -34,6 +35,7 @@ import { HttpService, RequestState } from "../services/HttpService";
 import {
   Choice,
   QueryParams,
+  RouteDataResponse,
   amAdmin,
   amMod,
   debounce,
@@ -74,6 +76,13 @@ type View =
   | AdminPurgePostView
   | AdminPurgeCommentView;
 
+type ModlogData = RouteDataResponse<{
+  modlogResponse: GetModlogResponse;
+  communityResponse?: GetCommunityResponse;
+  modUserResponse?: GetPersonDetailsResponse;
+  userResponse?: GetPersonDetailsResponse;
+}>;
+
 interface ModlogType {
   id: number;
   type_: ModlogActionType;
@@ -631,7 +640,7 @@ export class Modlog extends Component<
   RouteComponentProps<{ communityId?: string }>,
   ModlogState
 > {
-  private isoData = setIsoData(this.context);
+  private isoData = setIsoData<ModlogData>(this.context);
 
   state: ModlogState = {
     res: { state: "empty" },
@@ -653,25 +662,36 @@ export class Modlog extends Component<
 
     // Only fetch the data if coming from another route
     if (FirstLoadService.isFirstLoad) {
-      const [res, communityRes, filteredModRes, filteredUserRes] =
-        this.isoData.routeData;
+      const {
+        modlogResponse: res,
+        communityResponse: communityRes,
+        modUserResponse,
+        userResponse,
+      } = this.isoData.routeData;
+
       this.state = {
         ...this.state,
         res,
-        communityRes,
       };
 
-      if (filteredModRes.state === "success") {
+      if (communityRes?.state === "success") {
         this.state = {
           ...this.state,
-          modSearchOptions: [personToChoice(filteredModRes.data.person_view)],
+          communityRes,
         };
       }
 
-      if (filteredUserRes.state === "success") {
+      if (modUserResponse?.state === "success") {
         this.state = {
           ...this.state,
-          userSearchOptions: [personToChoice(filteredUserRes.data.person_view)],
+          modSearchOptions: [personToChoice(modUserResponse.data.person_view)],
+        };
+      }
+
+      if (userResponse?.state === "success") {
+        this.state = {
+          ...this.state,
+          userSearchOptions: [personToChoice(userResponse.data.person_view)],
         };
       }
     }
@@ -958,17 +978,14 @@ export class Modlog extends Component<
     }
   }
 
-  static fetchInitialData({
+  static async fetchInitialData({
     client,
     path,
     query: { modId: urlModId, page, userId: urlUserId, actionType },
     auth,
     site,
-  }: InitialFetchRequest<QueryParams<ModlogProps>>): Promise<
-    RequestState<any>
-  >[] {
+  }: InitialFetchRequest<QueryParams<ModlogProps>>): Promise<ModlogData> {
     const pathSplit = path.split("/");
-    const promises: Promise<RequestState<any>>[] = [];
     const communityId = getIdFromString(pathSplit[2]);
     const modId = !site.site_view.local_site.hide_modlog_mod_names
       ? getIdFromString(urlModId)
@@ -985,40 +1002,47 @@ export class Modlog extends Component<
       auth,
     };
 
-    promises.push(client.getModlog(modlogForm));
+    let communityResponse: RequestState<GetCommunityResponse> | undefined =
+      undefined;
 
     if (communityId) {
       const communityForm: GetCommunity = {
         id: communityId,
         auth,
       };
-      promises.push(client.getCommunity(communityForm));
-    } else {
-      promises.push(Promise.resolve({ state: "empty" }));
+
+      communityResponse = await client.getCommunity(communityForm);
     }
 
+    let modUserResponse: RequestState<GetPersonDetailsResponse> | undefined =
+      undefined;
+
     if (modId) {
       const getPersonForm: GetPersonDetails = {
         person_id: modId,
         auth,
       };
 
-      promises.push(client.getPersonDetails(getPersonForm));
-    } else {
-      promises.push(Promise.resolve({ state: "empty" }));
+      modUserResponse = await client.getPersonDetails(getPersonForm);
     }
 
+    let userResponse: RequestState<GetPersonDetailsResponse> | undefined =
+      undefined;
+
     if (userId) {
       const getPersonForm: GetPersonDetails = {
         person_id: userId,
         auth,
       };
 
-      promises.push(client.getPersonDetails(getPersonForm));
-    } else {
-      promises.push(Promise.resolve({ state: "empty" }));
+      userResponse = await client.getPersonDetails(getPersonForm);
     }
 
-    return promises;
+    return {
+      modlogResponse: await client.getModlog(modlogForm),
+      communityResponse,
+      modUserResponse,
+      userResponse,
+    };
   }
 }