]> Untitled Git - lemmy-ui.git/commitdiff
Cleanup
authorabias <abias1122@gmail.com>
Fri, 16 Jun 2023 02:39:04 +0000 (22:39 -0400)
committerabias <abias1122@gmail.com>
Fri, 16 Jun 2023 02:39:04 +0000 (22:39 -0400)
src/shared/components/community/community.tsx
src/shared/components/home/admin-settings.tsx
src/shared/components/home/home.tsx
src/shared/components/home/instances.tsx
src/shared/components/modlog.tsx
src/shared/components/person/inbox.tsx
src/shared/components/person/reports.tsx
src/shared/components/post/create-post.tsx
src/shared/components/search.tsx
src/shared/utils.ts

index e8412e76842f9481a8b07654642b799f11ac716d..6b4eecff64bc19082b232e2571d7ae31eb2713bd 100644 (file)
@@ -102,9 +102,9 @@ import { PostListings } from "../post/post-listings";
 import { CommunityLink } from "./community-link";
 
 type CommunityData = RouteDataResponse<{
-  communityResponse: GetCommunityResponse;
-  postsResponse?: GetPostsResponse;
-  commentsResponse?: GetCommentsResponse;
+  communityRes: GetCommunityResponse;
+  postsRes: GetPostsResponse;
+  commentsRes: GetCommentsResponse;
 }>;
 
 interface State {
@@ -201,37 +201,15 @@ export class Community extends Component<
 
     // Only fetch the data if coming from another route
     if (FirstLoadService.isFirstLoad) {
-      const {
-        communityResponse: communityRes,
-        commentsResponse: commentsRes,
-        postsResponse: postsRes,
-      } = this.isoData.routeData;
+      const { communityRes, commentsRes, postsRes } = this.isoData.routeData;
 
       this.state = {
         ...this.state,
         isIsomorphic: true,
+        commentsRes,
+        communityRes,
+        postsRes,
       };
-
-      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,
-        };
-      }
     }
   }
 
@@ -279,9 +257,10 @@ export class Community extends Component<
 
     const page = getPageFromString(urlPage);
 
-    let postsResponse: RequestState<GetPostsResponse> | undefined = undefined;
-    let commentsResponse: RequestState<GetCommentsResponse> | undefined =
-      undefined;
+    let postsResponse: RequestState<GetPostsResponse> = { state: "empty" };
+    let commentsResponse: RequestState<GetCommentsResponse> = {
+      state: "empty",
+    };
 
     if (dataType === DataType.Post) {
       const getPostsForm: GetPosts = {
@@ -310,9 +289,9 @@ export class Community extends Component<
     }
 
     return {
-      communityResponse: await client.getCommunity(communityForm),
-      commentsResponse,
-      postsResponse,
+      communityRes: await client.getCommunity(communityForm),
+      commentsRes: commentsResponse,
+      postsRes: postsResponse,
     };
   }
 
index 11be72579c4ca410b3a667f878a430e7a6a0d7b8..91ba727f2bae7292b83124d750d190a15e2acca8 100644 (file)
@@ -34,8 +34,8 @@ import { SiteForm } from "./site-form";
 import { TaglineForm } from "./tagline-form";
 
 type AdminSettingsData = RouteDataResponse<{
-  bannedPersonsResponse: BannedPersonsResponse;
-  federatedInstancesResponse: GetFederatedInstancesResponse;
+  bannedRes: BannedPersonsResponse;
+  instancesRes: GetFederatedInstancesResponse;
 }>;
 
 interface AdminSettingsState {
@@ -72,10 +72,7 @@ export class AdminSettings extends Component<any, AdminSettingsState> {
 
     // Only fetch the data if coming from another route
     if (FirstLoadService.isFirstLoad) {
-      const {
-        bannedPersonsResponse: bannedRes,
-        federatedInstancesResponse: instancesRes,
-      } = this.isoData.routeData;
+      const { bannedRes, instancesRes } = this.isoData.routeData;
 
       this.state = {
         ...this.state,
@@ -91,10 +88,10 @@ export class AdminSettings extends Component<any, AdminSettingsState> {
     client,
   }: InitialFetchRequest): Promise<AdminSettingsData> {
     return {
-      bannedPersonsResponse: await client.getBannedPersons({
+      bannedRes: await client.getBannedPersons({
         auth: auth as string,
       }),
-      federatedInstancesResponse: await client.getFederatedInstances({
+      instancesRes: await client.getFederatedInstances({
         auth: auth as string,
       }),
     };
index 3daaa4fd8141f5692fb1c9b5ddbff79432f51241..4dc797cebabadaa02a38099ce19da22f7b47f822 100644 (file)
@@ -119,9 +119,9 @@ interface HomeProps {
 }
 
 type HomeData = RouteDataResponse<{
-  postsResponse?: GetPostsResponse;
-  commentsResponse?: GetCommentsResponse;
-  trendingResponse: ListCommunitiesResponse;
+  postsRes: GetPostsResponse;
+  commentsRes: GetCommentsResponse;
+  trendingCommunitiesRes: ListCommunitiesResponse;
 }>;
 
 function getDataTypeFromQuery(type?: string): DataType {
@@ -236,37 +236,30 @@ export class Home extends Component<any, HomeState> {
     // Only fetch the data if coming from another route
     if (FirstLoadService.isFirstLoad) {
       const {
-        trendingResponse: trendingCommunitiesRes,
-        commentsResponse: commentsRes,
-        postsResponse: postsRes,
+        trendingCommunitiesRes: trendingCommunitiesRes,
+        commentsRes: commentsRes,
+        postsRes: postsRes,
       } = this.isoData.routeData;
 
       this.state = {
         ...this.state,
         trendingCommunitiesRes,
+        commentsRes,
+        postsRes,
         tagline: getRandomFromList(this.state?.siteRes?.taglines ?? [])
           ?.content,
         isIsomorphic: true,
       };
-
-      if (commentsRes?.state === "success") {
-        this.state = {
-          ...this.state,
-          commentsRes,
-        };
-      }
-
-      if (postsRes?.state === "success") {
-        this.state = {
-          ...this.state,
-          postsRes,
-        };
-      }
     }
   }
 
   async componentDidMount() {
-    if (!this.state.isIsomorphic || !this.isoData.routeData.length) {
+    if (
+      !this.state.isIsomorphic ||
+      !Object.values(this.isoData.routeData).some(
+        res => res.state === "success" || res.state === "failed"
+      )
+    ) {
       await Promise.all([this.fetchTrendingCommunities(), this.fetchData()]);
     }
 
@@ -290,9 +283,10 @@ export class Home extends Component<any, HomeState> {
 
     const page = urlPage ? Number(urlPage) : 1;
 
-    let postsResponse: RequestState<GetPostsResponse> | undefined = undefined;
-    let commentsResponse: RequestState<GetCommentsResponse> | undefined =
-      undefined;
+    let postsRes: RequestState<GetPostsResponse> = { state: "empty" };
+    let commentsRes: RequestState<GetCommentsResponse> = {
+      state: "empty",
+    };
 
     if (dataType === DataType.Post) {
       const getPostsForm: GetPosts = {
@@ -304,7 +298,7 @@ export class Home extends Component<any, HomeState> {
         auth,
       };
 
-      postsResponse = await client.getPosts(getPostsForm);
+      postsRes = await client.getPosts(getPostsForm);
     } else {
       const getCommentsForm: GetComments = {
         page,
@@ -315,7 +309,7 @@ export class Home extends Component<any, HomeState> {
         auth,
       };
 
-      commentsResponse = await client.getComments(getCommentsForm);
+      commentsRes = await client.getComments(getCommentsForm);
     }
 
     const trendingCommunitiesForm: ListCommunities = {
@@ -326,9 +320,11 @@ export class Home extends Component<any, HomeState> {
     };
 
     return {
-      trendingResponse: await client.listCommunities(trendingCommunitiesForm),
-      commentsResponse,
-      postsResponse,
+      trendingCommunitiesRes: await client.listCommunities(
+        trendingCommunitiesForm
+      ),
+      commentsRes,
+      postsRes,
     };
   }
 
index bec472cf1ea9d6aca77ed7bd3f6685f5788cc272..ec2ff63572af0bfa00b4c299ec457d25f95a669e 100644 (file)
@@ -59,11 +59,11 @@ export class Instances extends Component<any, InstancesState> {
     });
   }
 
-  static async fetchInitialData(
-    req: InitialFetchRequest
-  ): Promise<InstancesData> {
+  static async fetchInitialData({
+    client,
+  }: InitialFetchRequest): Promise<InstancesData> {
     return {
-      federatedInstancesResponse: await req.client.getFederatedInstances({}),
+      federatedInstancesResponse: await client.getFederatedInstances({}),
     };
   }
 
index 48be10b6245849c0b511bc9ba9137599eb364566..b3f1fff1542c44b440c166aa1d6a62b1870945bf 100644 (file)
@@ -77,10 +77,10 @@ type View =
   | AdminPurgeCommentView;
 
 type ModlogData = RouteDataResponse<{
-  modlogResponse: GetModlogResponse;
-  communityResponse?: GetCommunityResponse;
-  modUserResponse?: GetPersonDetailsResponse;
-  userResponse?: GetPersonDetailsResponse;
+  res: GetModlogResponse;
+  communityRes: GetCommunityResponse;
+  modUserResponse: GetPersonDetailsResponse;
+  userResponse: GetPersonDetailsResponse;
 }>;
 
 interface ModlogType {
@@ -662,33 +662,23 @@ export class Modlog extends Component<
 
     // Only fetch the data if coming from another route
     if (FirstLoadService.isFirstLoad) {
-      const {
-        modlogResponse: res,
-        communityResponse: communityRes,
-        modUserResponse,
-        userResponse,
-      } = this.isoData.routeData;
+      const { res, communityRes, modUserResponse, userResponse } =
+        this.isoData.routeData;
 
       this.state = {
         ...this.state,
         res,
+        communityRes,
       };
 
-      if (communityRes?.state === "success") {
-        this.state = {
-          ...this.state,
-          communityRes,
-        };
-      }
-
-      if (modUserResponse?.state === "success") {
+      if (modUserResponse.state === "success") {
         this.state = {
           ...this.state,
           modSearchOptions: [personToChoice(modUserResponse.data.person_view)],
         };
       }
 
-      if (userResponse?.state === "success") {
+      if (userResponse.state === "success") {
         this.state = {
           ...this.state,
           userSearchOptions: [personToChoice(userResponse.data.person_view)],
@@ -1002,8 +992,9 @@ export class Modlog extends Component<
       auth,
     };
 
-    let communityResponse: RequestState<GetCommunityResponse> | undefined =
-      undefined;
+    let communityResponse: RequestState<GetCommunityResponse> = {
+      state: "empty",
+    };
 
     if (communityId) {
       const communityForm: GetCommunity = {
@@ -1014,8 +1005,9 @@ export class Modlog extends Component<
       communityResponse = await client.getCommunity(communityForm);
     }
 
-    let modUserResponse: RequestState<GetPersonDetailsResponse> | undefined =
-      undefined;
+    let modUserResponse: RequestState<GetPersonDetailsResponse> = {
+      state: "empty",
+    };
 
     if (modId) {
       const getPersonForm: GetPersonDetails = {
@@ -1026,8 +1018,9 @@ export class Modlog extends Component<
       modUserResponse = await client.getPersonDetails(getPersonForm);
     }
 
-    let userResponse: RequestState<GetPersonDetailsResponse> | undefined =
-      undefined;
+    let userResponse: RequestState<GetPersonDetailsResponse> = {
+      state: "empty",
+    };
 
     if (userId) {
       const getPersonForm: GetPersonDetails = {
@@ -1039,8 +1032,8 @@ export class Modlog extends Component<
     }
 
     return {
-      modlogResponse: await client.getModlog(modlogForm),
-      communityResponse,
+      res: await client.getModlog(modlogForm),
+      communityRes: communityResponse,
       modUserResponse,
       userResponse,
     };
index b0550f2241a8fae938d61f5b3cf60b7fe5bce0d0..c65d897b5b675a10720c7019e5eda835387f14fe 100644 (file)
@@ -92,9 +92,9 @@ enum ReplyEnum {
 }
 
 type InboxData = RouteDataResponse<{
-  repliesResponse: GetRepliesResponse;
-  personMentionsResponse: GetPersonMentionsResponse;
-  privateMessagesResponse: PrivateMessagesResponse;
+  repliesRes: GetRepliesResponse;
+  mentionsRes: GetPersonMentionsResponse;
+  messagesRes: PrivateMessagesResponse;
 }>;
 
 type ReplyType = {
@@ -167,11 +167,7 @@ export class Inbox extends Component<any, InboxState> {
 
     // Only fetch the data if coming from another route
     if (FirstLoadService.isFirstLoad) {
-      const {
-        personMentionsResponse: mentionsRes,
-        privateMessagesResponse: messagesRes,
-        repliesResponse: repliesRes,
-      } = this.isoData.routeData;
+      const { mentionsRes, messagesRes, repliesRes } = this.isoData.routeData;
 
       this.state = {
         ...this.state,
@@ -702,7 +698,7 @@ export class Inbox extends Component<any, InboxState> {
     const sort: CommentSortType = "New";
 
     return {
-      personMentionsResponse: auth
+      mentionsRes: auth
         ? await client.getPersonMentions({
             sort,
             unread_only: true,
@@ -711,7 +707,7 @@ export class Inbox extends Component<any, InboxState> {
             auth,
           })
         : { state: "empty" },
-      privateMessagesResponse: auth
+      messagesRes: auth
         ? await client.getPrivateMessages({
             unread_only: true,
             page: 1,
@@ -719,7 +715,7 @@ export class Inbox extends Component<any, InboxState> {
             auth,
           })
         : { state: "empty" },
-      repliesResponse: auth
+      repliesRes: auth
         ? await client.getReplies({
             sort,
             unread_only: true,
index fb8e8b831870bacc68daada998c165759753ed69..99a0333645fe4e719b031eefbba68c5e53862780 100644 (file)
@@ -58,9 +58,9 @@ enum MessageEnum {
 }
 
 type ReportsData = RouteDataResponse<{
-  commentReportsResponse: ListCommentReportsResponse;
-  postReportsResponse: ListPostReportsResponse;
-  privateMessageReportsResponse?: ListPrivateMessageReportsResponse;
+  commentReportsRes: ListCommentReportsResponse;
+  postReportsRes: ListPostReportsResponse;
+  messageReportsRes: ListPrivateMessageReportsResponse;
 }>;
 
 type ItemType = {
@@ -106,11 +106,8 @@ export class Reports extends Component<any, ReportsState> {
 
     // Only fetch the data if coming from another route
     if (FirstLoadService.isFirstLoad) {
-      const {
-        commentReportsResponse: commentReportsRes,
-        postReportsResponse: postReportsRes,
-        privateMessageReportsResponse: messageReportsRes,
-      } = this.isoData.routeData;
+      const { commentReportsRes, postReportsRes, messageReportsRes } =
+        this.isoData.routeData;
 
       this.state = {
         ...this.state,
@@ -122,7 +119,7 @@ export class Reports extends Component<any, ReportsState> {
       if (amAdmin()) {
         this.state = {
           ...this.state,
-          messageReportsRes: messageReportsRes ?? { state: "empty" },
+          messageReportsRes: messageReportsRes,
         };
       }
     }
@@ -515,10 +512,9 @@ export class Reports extends Component<any, ReportsState> {
     };
 
     const data: ReportsData = {
-      commentReportsResponse: await client.listCommentReports(
-        commentReportsForm
-      ),
-      postReportsResponse: await client.listPostReports(postReportsForm),
+      commentReportsRes: await client.listCommentReports(commentReportsForm),
+      postReportsRes: await client.listPostReports(postReportsForm),
+      messageReportsRes: { state: "empty" },
     };
 
     if (amAdmin()) {
@@ -529,8 +525,9 @@ export class Reports extends Component<any, ReportsState> {
         auth: auth as string,
       };
 
-      data.privateMessageReportsResponse =
-        await client.listPrivateMessageReports(privateMessageReportsForm);
+      data.messageReportsRes = await client.listPrivateMessageReports(
+        privateMessageReportsForm
+      );
     }
 
     return data;
index bb39cdadbdab3dae9a162c0325997b43f25a34e0..a0e439bb8b842c852025cc68631510810cf863f8 100644 (file)
@@ -81,6 +81,13 @@ export class CreatePost extends Component<
       const { communityResponse: communityRes, initialCommunitiesRes } =
         this.isoData.routeData;
 
+      this.state = {
+        ...this.state,
+        loading: false,
+        initialCommunitiesRes,
+        isIsomorphic: true,
+      };
+
       if (communityRes?.state === "success") {
         const communityChoice: Choice = {
           label: communityRes.data.community_view.community.title,
@@ -92,13 +99,6 @@ export class CreatePost extends Component<
           selectedCommunityChoice: communityChoice,
         };
       }
-
-      this.state = {
-        ...this.state,
-        loading: false,
-        initialCommunitiesRes,
-        isIsomorphic: true,
-      };
     }
   }
 
index 9f466730a8c952cd5cf03fcefb3efe44a1f3e1d1..054cab016be94dd34e2b3294a1bd4825590cc75d 100644 (file)
@@ -72,11 +72,11 @@ interface SearchProps {
 }
 
 type SearchData = RouteDataResponse<{
-  communityResponse?: GetCommunityResponse;
-  listCommunitiesResponse?: ListCommunitiesResponse;
-  creatorDetailsResponse?: GetPersonDetailsResponse;
-  searchResponse?: SearchResponse;
-  resolveObjectResponse?: ResolveObjectResponse;
+  communityResponse: GetCommunityResponse;
+  listCommunitiesResponse: ListCommunitiesResponse;
+  creatorDetailsResponse: GetPersonDetailsResponse;
+  searchResponse: SearchResponse;
+  resolveObjectResponse: ResolveObjectResponse;
 }>;
 
 type FilterType = "creator" | "community";
@@ -365,11 +365,12 @@ export class Search extends Component<any, SearchState> {
     query: { communityId, creatorId, q, type, sort, listingType, page },
   }: InitialFetchRequest<QueryParams<SearchProps>>): Promise<SearchData> {
     const community_id = getIdFromString(communityId);
-    let communityResponse: RequestState<GetCommunityResponse> | undefined =
-      undefined;
-    let listCommunitiesResponse:
-      | RequestState<ListCommunitiesResponse>
-      | undefined = undefined;
+    let communityResponse: RequestState<GetCommunityResponse> = {
+      state: "empty",
+    };
+    let listCommunitiesResponse: RequestState<ListCommunitiesResponse> = {
+      state: "empty",
+    };
     if (community_id) {
       const getCommunityForm: GetCommunity = {
         id: community_id,
@@ -391,9 +392,9 @@ export class Search extends Component<any, SearchState> {
     }
 
     const creator_id = getIdFromString(creatorId);
-    let creatorDetailsResponse:
-      | RequestState<GetPersonDetailsResponse>
-      | undefined = undefined;
+    let creatorDetailsResponse: RequestState<GetPersonDetailsResponse> = {
+      state: "empty",
+    };
     if (creator_id) {
       const getCreatorForm: GetPersonDetails = {
         person_id: creator_id,
@@ -405,9 +406,10 @@ export class Search extends Component<any, SearchState> {
 
     const query = getSearchQueryFromQuery(q);
 
-    let searchResponse: RequestState<SearchResponse> | undefined = undefined;
-    let resolveObjectResponse: RequestState<ResolveObjectResponse> | undefined =
-      undefined;
+    let searchResponse: RequestState<SearchResponse> = { state: "empty" };
+    let resolveObjectResponse: RequestState<ResolveObjectResponse> = {
+      state: "empty",
+    };
 
     if (query) {
       const form: SearchForm = {
@@ -429,9 +431,7 @@ export class Search extends Component<any, SearchState> {
             q: query,
             auth,
           };
-          resolveObjectResponse = await client
-            .resolveObject(resolveObjectForm)
-            .catch(() => undefined);
+          resolveObjectResponse = await client.resolveObject(resolveObjectForm);
         }
       }
     }
index cf59086b8596e56540763048173e6c745b44b660..e1a0c14e97b0b81428704f6497cc1f766353c039 100644 (file)
@@ -1499,7 +1499,7 @@ export function newVote(voteType: VoteType, myVote?: number): number {
 }
 
 export type RouteDataResponse<T extends Record<string, any>> = {
-  [K in keyof T]: RequestState<Exclude<T[K], undefined>>;
+  [K in keyof T]: RequestState<T[K]>;
 };
 
 function sleep(millis: number): Promise<void> {