Use ZOD for group-members

environments/review-group-comp-s3yeyp/deployments/2859
Chewbacca 2023-03-13 15:08:02 -04:00
rodzic 08f97a133e
commit 9d1c2df1a2
6 zmienionych plików z 36 dodań i 42 usunięć

Wyświetl plik

@ -10,11 +10,10 @@ import { HStack, IconButton, Menu, MenuButton, MenuDivider, MenuItem, MenuLink,
import SvgIcon from 'soapbox/components/ui/icon/svg-icon';
import { useAccount, useAppDispatch } from 'soapbox/hooks';
import { BaseGroupRoles, useGroupRoles } from 'soapbox/hooks/useGroupRoles';
import { GroupMember } from 'soapbox/normalizers/group-member';
import toast from 'soapbox/toast';
import type { Menu as IMenu } from 'soapbox/components/dropdown-menu';
import type { Account as AccountEntity, Group } from 'soapbox/types/entities';
import type { Account as AccountEntity, Group, GroupMember } from 'soapbox/types/entities';
const messages = defineMessages({
blockConfirm: { id: 'confirmations.block_from_group.confirm', defaultMessage: 'Block' },

Wyświetl plik

@ -1,28 +1,17 @@
import { Entities } from 'soapbox/entity-store/entities';
import { useEntities } from 'soapbox/entity-store/hooks';
import { normalizeAccount } from 'soapbox/normalizers';
import { Account } from 'soapbox/types/entities';
import { GroupMember, groupMemberSchema } from 'soapbox/schemas';
import { BaseGroupRoles, TruthSocialGroupRoles } from '../useGroupRoles';
interface GroupMember {
id: string
role: BaseGroupRoles | TruthSocialGroupRoles
account: Account | any
}
const normalizeGroupMember = (groupMember: GroupMember): GroupMember => {
return {
...groupMember,
account: normalizeAccount(groupMember.account),
};
const parseGroupMember = (entity: unknown) => {
const result = groupMemberSchema.safeParse(entity);
if (result.success) {
return result.data;
}
};
const parseGroupMember = (entity: unknown) => entity ? normalizeGroupMember(entity as GroupMember) : undefined;
function useGroupMembers(groupId: string, role: string) {
const { entities, ...result } = useEntities<GroupMember>(
[Entities.GROUP_MEMBERSHIPS, `${groupId}:${role}`],
[Entities.GROUP_MEMBERSHIPS, groupId, role],
`/api/v1/groups/${groupId}/memberships?role=${role}&limit=1`,
{ parser: parseGroupMember },
);

Wyświetl plik

@ -1,21 +0,0 @@
/**
* Group Member normalizer:
* Converts API group members into our internal format.
*/
import { BaseGroupRoles, TruthSocialGroupRoles } from 'soapbox/hooks/useGroupRoles';
import { Account } from 'soapbox/types/entities';
import { normalizeAccount } from './account';
export interface GroupMember {
id: string
role: BaseGroupRoles | TruthSocialGroupRoles
account: Account | any
}
export const normalizeGroupMember = (groupMember: GroupMember): GroupMember => {
return {
...groupMember,
account: normalizeAccount(groupMember.account),
};
};

Wyświetl plik

@ -13,7 +13,6 @@ export { FilterRecord, normalizeFilter } from './filter';
export { FilterKeywordRecord, normalizeFilterKeyword } from './filter-keyword';
export { FilterStatusRecord, normalizeFilterStatus } from './filter-status';
export { normalizeGroup } from './group';
// export { GroupMember, normalizeGroupMember } from './group-member';
export { GroupRelationshipRecord, normalizeGroupRelationship } from './group-relationship';
export { HistoryRecord, normalizeHistory } from './history';
export { InstanceRecord, normalizeInstance } from './instance';

Wyświetl plik

@ -0,0 +1,26 @@
import z from 'zod';
enum TruthSocialGroupRoles {
ADMIN = 'owner',
MODERATOR = 'admin',
USER = 'user'
}
enum BaseGroupRoles {
ADMIN = 'admin',
MODERATOR = 'moderator',
USER = 'user'
}
const groupMemberSchema = z.object({
id: z.string(),
account: z.any(),
role: z.union([
z.nativeEnum(TruthSocialGroupRoles),
z.nativeEnum(BaseGroupRoles),
]),
});
type GroupMember = z.infer<typeof groupMemberSchema>;
export { groupMemberSchema, GroupMember };

Wyświetl plik

@ -2,5 +2,7 @@ export { customEmojiSchema } from './custom-emoji';
export type { CustomEmoji } from './custom-emoji';
export { groupSchema } from './group';
export type { Group } from './group';
export { groupMemberSchema } from './group-member';
export type { GroupMember } from './group-member';
export { groupRelationshipSchema } from './group-relationship';
export type { GroupRelationship } from './group-relationship';