Refactor: 필요없는 form 제거

This commit is contained in:
정현조 2024-09-20 10:15:53 +09:00
parent 557a2b6e37
commit 6d44657bc2

View File

@ -1,9 +1,4 @@
import { useLocation } from 'react-router-dom'; import { useLocation } from 'react-router-dom';
import { useForm } from 'react-hook-form';
import { z } from 'zod';
import { zodResolver } from '@hookform/resolvers/zod';
import { Form, FormControl, FormField, FormItem, FormMessage } from '../ui/form';
import { Input } from '../ui/input';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '../ui/select'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '../ui/select';
import useUpdateProjectMemberPrivilegeQuery from '@/queries/projects/useUpdateProjectMemberPrivilegeQuery'; import useUpdateProjectMemberPrivilegeQuery from '@/queries/projects/useUpdateProjectMemberPrivilegeQuery';
import useRemoveProjectMemberQuery from '@/queries/projects/useRemoveProjectMemberQuery'; import useRemoveProjectMemberQuery from '@/queries/projects/useRemoveProjectMemberQuery';
@ -22,18 +17,6 @@ const roleToStr: { [key in Role]: string } = {
NONE: '역할 없음', NONE: '역할 없음',
}; };
const formSchema = z.object({
members: z.array(
z.object({
memberId: z.number(),
nickname: z.string().nonempty('닉네임을 입력하세요.'),
role: z.enum(roles as [Role, ...Role[]], { errorMap: () => ({ message: '역할을 선택해주세요.' }) }),
})
),
});
export type ProjectMemberManageFormValues = z.infer<typeof formSchema>;
interface ProjectMemberManageFormProps { interface ProjectMemberManageFormProps {
workspaceMembers: Array<{ memberId: number; nickname: string }>; workspaceMembers: Array<{ memberId: number; nickname: string }>;
} }
@ -51,24 +34,6 @@ export default function ProjectMemberManageForm({ workspaceMembers }: ProjectMem
const noRoleMembers = workspaceMembers.filter((wm) => !projectMembers.some((pm) => pm.memberId === wm.memberId)); const noRoleMembers = workspaceMembers.filter((wm) => !projectMembers.some((pm) => pm.memberId === wm.memberId));
const form = useForm<ProjectMemberManageFormValues>({
resolver: zodResolver(formSchema),
defaultValues: {
members: [
...projectMembers.map((m) => ({
memberId: m.memberId,
nickname: m.nickname,
role: m.privilegeType as Role,
})),
...noRoleMembers.map((m) => ({
memberId: m.memberId,
nickname: m.nickname,
role: 'NONE' as Role,
})),
],
},
});
const handleRoleChange = (memberId: number, role: Role) => { const handleRoleChange = (memberId: number, role: Role) => {
if (role === 'NONE') { if (role === 'NONE') {
removeMember({ removeMember({
@ -89,42 +54,18 @@ export default function ProjectMemberManageForm({ workspaceMembers }: ProjectMem
}; };
return ( return (
<Form {...form}>
<div className="flex w-full flex-col gap-4"> <div className="flex w-full flex-col gap-4">
{form.getValues('members').map((member, index) => ( {[...projectMembers, ...noRoleMembers].map((member) => (
<div <div
key={member.memberId} key={member.memberId}
className="flex items-center gap-4" className="flex items-center gap-4"
> >
<FormField <span className="flex-1">{member.nickname}</span>
name={`members.${index}.nickname`} <div className="flex-1">
control={form.control}
render={({ field }) => (
<FormItem className="flex-1">
<FormControl>
<Input
placeholder="닉네임을 입력하세요."
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
name={`members.${index}.role`}
control={form.control}
render={({ field }) => (
<FormItem className="flex-1">
<FormControl>
<Select <Select
onValueChange={(value) => { onValueChange={(value) => handleRoleChange(member.memberId, value as Role)}
field.onChange(value); defaultValue={projectMembers.find((m) => m.memberId === member.memberId)?.privilegeType || 'NONE'}
handleRoleChange(member.memberId, value as Role); disabled={projectMembers.some((m) => m.memberId === member.memberId && m.privilegeType === 'ADMIN')}
}}
defaultValue={field.value}
disabled={member.role === 'ADMIN'}
> >
<SelectTrigger> <SelectTrigger>
<SelectValue placeholder="역할을 선택해주세요." /> <SelectValue placeholder="역할을 선택해주세요." />
@ -140,14 +81,9 @@ export default function ProjectMemberManageForm({ workspaceMembers }: ProjectMem
))} ))}
</SelectContent> </SelectContent>
</Select> </Select>
</FormControl> </div>
<FormMessage />
</FormItem>
)}
/>
</div> </div>
))} ))}
</div> </div>
</Form>
); );
} }