Make useGroups hooks use zod parser, update group types

develop^2
Alex Gleason 2023-03-10 13:36:00 -06:00
rodzic 5e8c92ed4d
commit 3d2331d20b
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 7211D1F99744FBB7
6 zmienionych plików z 34 dodań i 23 usunięć

Wyświetl plik

@ -18,7 +18,7 @@ describe('<GroupActionButton />', () => {
describe('with a private group', () => { describe('with a private group', () => {
beforeEach(() => { beforeEach(() => {
group = group.set('locked', true); group = { ...group, locked: true };
}); });
it('should render the Request Access button', () => { it('should render the Request Access button', () => {
@ -30,7 +30,7 @@ describe('<GroupActionButton />', () => {
describe('with a public group', () => { describe('with a public group', () => {
beforeEach(() => { beforeEach(() => {
group = group.set('locked', false); group = { ...group, locked: false };
}); });
it('should render the Join Group button', () => { it('should render the Join Group button', () => {
@ -52,7 +52,7 @@ describe('<GroupActionButton />', () => {
describe('with a private group', () => { describe('with a private group', () => {
beforeEach(() => { beforeEach(() => {
group = group.set('locked', true); group = { ...group, locked: true };
}); });
it('should render the Request Access button', () => { it('should render the Request Access button', () => {
@ -64,7 +64,7 @@ describe('<GroupActionButton />', () => {
describe('with a public group', () => { describe('with a public group', () => {
beforeEach(() => { beforeEach(() => {
group = group.set('locked', false); group = { ...group, locked: false };
}); });
it('should render the Join Group button', () => { it('should render the Join Group button', () => {

Wyświetl plik

@ -1,13 +1,12 @@
import { useEntities, useEntity } from 'soapbox/entity-store/hooks'; import { useEntities, useEntity } from 'soapbox/entity-store/hooks';
import { normalizeGroup, normalizeGroupRelationship } from 'soapbox/normalizers'; import { groupSchema, Group } from 'soapbox/schemas/group';
import { groupRelationshipSchema, GroupRelationship } from 'soapbox/schemas/group-relationship';
import type { Group, GroupRelationship } from 'soapbox/types/entities';
function useGroups() { function useGroups() {
const { entities, ...result } = useEntities<Group>(['Group', ''], '/api/v1/groups', { parser: parseGroup }); const { entities, ...result } = useEntities<Group>(['Group', ''], '/api/v1/groups', { parser: parseGroup });
const { relationships } = useGroupRelationships(entities.map(entity => entity.id)); const { relationships } = useGroupRelationships(entities.map(entity => entity.id));
const groups = entities.map((group) => group.set('relationship', relationships[group.id] || null)); const groups = entities.map((group) => ({ ...group, relationship: relationships[group.id] || null }));
return { return {
...result, ...result,
@ -21,7 +20,7 @@ function useGroup(groupId: string, refetch = true) {
return { return {
...result, ...result,
group: group?.set('relationship', relationship || null), group: group ? { ...group, relationship: relationship || null } : undefined,
}; };
} }
@ -45,9 +44,18 @@ function useGroupRelationships(groupIds: string[]) {
}; };
} }
// HACK: normalizers currently don't have the desired API. const parseGroup = (entity: unknown) => {
// TODO: rewrite normalizers as Zod parsers. const result = groupSchema.safeParse(entity);
const parseGroup = (entity: unknown) => entity ? normalizeGroup(entity as Record<string, any>) : undefined; if (result.success) {
const parseGroupRelationship = (entity: unknown) => entity ? normalizeGroupRelationship(entity as Record<string, any>) : undefined; return result.data;
}
};
const parseGroupRelationship = (entity: unknown) => {
const result = groupRelationshipSchema.safeParse(entity);
if (result.success) {
return result.data;
}
};
export { useGroup, useGroups }; export { useGroup, useGroups };

Wyświetl plik

@ -23,13 +23,14 @@ export const GroupRecord = ImmutableRecord({
created_at: '', created_at: '',
display_name: '', display_name: '',
domain: '', domain: '',
emojis: ImmutableList<Emoji>(), emojis: [] as Emoji[],
group_visibility: '',
header: '', header: '',
header_static: '', header_static: '',
id: '', id: '',
locked: false, locked: false,
membership_required: false, membership_required: false,
members_count: undefined as number | undefined, members_count: 0,
note: '', note: '',
statuses_visibility: 'public', statuses_visibility: 'public',
uri: '', uri: '',
@ -69,7 +70,7 @@ const normalizeHeader = (group: ImmutableMap<string, any>) => {
/** Normalize emojis */ /** Normalize emojis */
const normalizeEmojis = (entity: ImmutableMap<string, any>) => { const normalizeEmojis = (entity: ImmutableMap<string, any>) => {
const emojis = entity.get('emojis', ImmutableList()).map(normalizeEmoji); const emojis = entity.get('emojis', ImmutableList()).map(normalizeEmoji);
return entity.set('emojis', emojis); return entity.set('emojis', emojis.toArray());
}; };
/** Set display name from username, if applicable */ /** Set display name from username, if applicable */

Wyświetl plik

@ -12,7 +12,7 @@ export { EmojiReactionRecord } from './emoji-reaction';
export { FilterRecord, normalizeFilter } from './filter'; export { FilterRecord, normalizeFilter } from './filter';
export { FilterKeywordRecord, normalizeFilterKeyword } from './filter-keyword'; export { FilterKeywordRecord, normalizeFilterKeyword } from './filter-keyword';
export { FilterStatusRecord, normalizeFilterStatus } from './filter-status'; export { FilterStatusRecord, normalizeFilterStatus } from './filter-status';
export { GroupRecord, normalizeGroup } from './group'; export { normalizeGroup } from './group';
export { GroupRelationshipRecord, normalizeGroupRelationship } from './group-relationship'; export { GroupRelationshipRecord, normalizeGroupRelationship } from './group-relationship';
export { HistoryRecord, normalizeHistory } from './history'; export { HistoryRecord, normalizeHistory } from './history';
export { InstanceRecord, normalizeInstance } from './instance'; export { InstanceRecord, normalizeInstance } from './instance';

Wyświetl plik

@ -0,0 +1,3 @@
export { customEmojiSchema, CustomEmoji } from './custom-emoji';
export { groupSchema, Group } from './group';
export { groupRelationshipSchema, GroupRelationship } from './group-relationship';

Wyświetl plik

@ -14,8 +14,6 @@ import {
FilterRecord, FilterRecord,
FilterKeywordRecord, FilterKeywordRecord,
FilterStatusRecord, FilterStatusRecord,
GroupRecord,
GroupRelationshipRecord,
HistoryRecord, HistoryRecord,
InstanceRecord, InstanceRecord,
ListRecord, ListRecord,
@ -48,8 +46,6 @@ type Field = ReturnType<typeof FieldRecord>;
type Filter = ReturnType<typeof FilterRecord>; type Filter = ReturnType<typeof FilterRecord>;
type FilterKeyword = ReturnType<typeof FilterKeywordRecord>; type FilterKeyword = ReturnType<typeof FilterKeywordRecord>;
type FilterStatus = ReturnType<typeof FilterStatusRecord>; type FilterStatus = ReturnType<typeof FilterStatusRecord>;
type Group = ReturnType<typeof GroupRecord>;
type GroupRelationship = ReturnType<typeof GroupRelationshipRecord>;
type History = ReturnType<typeof HistoryRecord>; type History = ReturnType<typeof HistoryRecord>;
type Instance = ReturnType<typeof InstanceRecord>; type Instance = ReturnType<typeof InstanceRecord>;
type List = ReturnType<typeof ListRecord>; type List = ReturnType<typeof ListRecord>;
@ -95,8 +91,6 @@ export {
Filter, Filter,
FilterKeyword, FilterKeyword,
FilterStatus, FilterStatus,
Group,
GroupRelationship,
History, History,
Instance, Instance,
List, List,
@ -114,3 +108,8 @@ export {
APIEntity, APIEntity,
EmbeddedEntity, EmbeddedEntity,
}; };
export type {
Group,
GroupRelationship,
} from 'soapbox/schemas';