From 1b542c3ed7998274326ec569a079e2a1e969aa38 Mon Sep 17 00:00:00 2001 From: Chewbacca Date: Tue, 14 Mar 2023 10:19:34 -0400 Subject: [PATCH] Use Entities enum --- app/soapbox/entity-store/entities.ts | 5 +++- app/soapbox/hooks/api/useSuggestedGroups.ts | 2 +- app/soapbox/hooks/useGroups.ts | 30 +++++++++++++++++---- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/app/soapbox/entity-store/entities.ts b/app/soapbox/entity-store/entities.ts index 886c60113..05d7b60d9 100644 --- a/app/soapbox/entity-store/entities.ts +++ b/app/soapbox/entity-store/entities.ts @@ -1,4 +1,7 @@ export enum Entities { + GROUPS = 'Groups', + GROUP_RELATIONSHIPS = 'GroupRelationships', GROUP_MEMBERSHIPS = 'GroupMemberships', - POPULAR_GROUPS = 'PopularGroups' + POPULAR_GROUPS = 'PopularGroups', + SUGGESTED_GROUPS = 'SuggestedGroups', } \ No newline at end of file diff --git a/app/soapbox/hooks/api/useSuggestedGroups.ts b/app/soapbox/hooks/api/useSuggestedGroups.ts index 829108469..54477c249 100644 --- a/app/soapbox/hooks/api/useSuggestedGroups.ts +++ b/app/soapbox/hooks/api/useSuggestedGroups.ts @@ -9,7 +9,7 @@ function useSuggestedGroups() { const features = useFeatures(); const { entities, ...result } = useEntities( - [Entities.POPULAR_GROUPS, ''], + [Entities.SUGGESTED_GROUPS, ''], '/api/mock/groups', // '/api/v1/truth/suggestions/groups' { schema: groupSchema, diff --git a/app/soapbox/hooks/useGroups.ts b/app/soapbox/hooks/useGroups.ts index 4759973c8..f31144714 100644 --- a/app/soapbox/hooks/useGroups.ts +++ b/app/soapbox/hooks/useGroups.ts @@ -1,12 +1,20 @@ +import { Entities } from 'soapbox/entity-store/entities'; import { useEntities, useEntity } from 'soapbox/entity-store/hooks'; import { groupSchema, Group } from 'soapbox/schemas/group'; import { groupRelationshipSchema, GroupRelationship } from 'soapbox/schemas/group-relationship'; function useGroups() { - const { entities, ...result } = useEntities(['Group', ''], '/api/v1/groups', { schema: groupSchema }); + const { entities, ...result } = useEntities( + [Entities.GROUPS, ''], + '/api/v1/groups', + { schema: groupSchema }, + ); const { relationships } = useGroupRelationships(entities.map(entity => entity.id)); - const groups = entities.map((group) => ({ ...group, relationship: relationships[group.id] || null })); + const groups = entities.map((group) => ({ + ...group, + relationship: relationships[group.id] || null, + })); return { ...result, @@ -15,7 +23,11 @@ function useGroups() { } function useGroup(groupId: string, refetch = true) { - const { entity: group, ...result } = useEntity(['Group', groupId], `/api/v1/groups/${groupId}`, { schema: groupSchema, refetch }); + const { entity: group, ...result } = useEntity( + [Entities.GROUPS, groupId], + `/api/v1/groups/${groupId}`, + { schema: groupSchema, refetch }, + ); const { entity: relationship } = useGroupRelationship(groupId); return { @@ -25,13 +37,21 @@ function useGroup(groupId: string, refetch = true) { } function useGroupRelationship(groupId: string) { - return useEntity(['GroupRelationship', groupId], `/api/v1/groups/relationships?id[]=${groupId}`, { schema: groupRelationshipSchema }); + return useEntity( + [Entities.GROUP_RELATIONSHIPS, groupId], + `/api/v1/groups/relationships?id[]=${groupId}`, + { schema: groupRelationshipSchema }, + ); } function useGroupRelationships(groupIds: string[]) { const q = groupIds.map(id => `id[]=${id}`).join('&'); const endpoint = groupIds.length ? `/api/v1/groups/relationships?${q}` : undefined; - const { entities, ...result } = useEntities(['GroupRelationship', q], endpoint, { schema: groupRelationshipSchema }); + const { entities, ...result } = useEntities( + [Entities.GROUP_RELATIONSHIPS, q], + endpoint, + { schema: groupRelationshipSchema }, + ); const relationships = entities.reduce>((map, relationship) => { map[relationship.id] = relationship;