"use client" import * as React from "react" import { zodResolver } from "@hookform/resolvers/zod" import { Loader, Save } from "lucide-react" import { useForm } from "react-hook-form" import { toast } from "sonner" import { z } from "zod" import { useRouter } from "next/navigation" import { Button } from "@/components/ui/button" import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form" import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select" import { Sheet, SheetClose, SheetContent, SheetDescription, SheetFooter, SheetHeader, SheetTitle, } from "@/components/ui/sheet" import { Input } from "@/components/ui/input" import { Textarea } from "@/components/ui/textarea" import { modifyPq } from "../service" import { groupOptions } from "./add-pq-dialog" // PQ 수정을 위한 Zod 스키마 정의 const updatePqSchema = z.object({ code: z.string().min(1, "Code is required"), checkPoint: z.string().min(1, "Check point is required"), groupName: z.string().min(1, "Group is required"), description: z.string().optional(), remarks: z.string().optional() }); type UpdatePqSchema = z.infer; interface UpdatePqSheetProps extends React.ComponentPropsWithRef { pq: { id: number; code: string; checkPoint: string; description: string | null; remarks: string | null; groupName: string | null; } | null } export function UpdatePqSheet({ pq, ...props }: UpdatePqSheetProps) { const [isUpdatePending, startUpdateTransition] = React.useTransition() const router = useRouter() const form = useForm({ resolver: zodResolver(updatePqSchema), defaultValues: { code: pq?.code ?? "", checkPoint: pq?.checkPoint ?? "", groupName: pq?.groupName ?? groupOptions[0], description: pq?.description ?? "", remarks: pq?.remarks ?? "", }, }) // 폼 초기화 (pq가 변경될 때) React.useEffect(() => { if (pq) { form.reset({ code: pq.code, checkPoint: pq.checkPoint, groupName: pq.groupName ?? groupOptions[0], description: pq.description ?? "", remarks: pq.remarks ?? "", }); } }, [pq, form]); function onSubmit(input: UpdatePqSchema) { startUpdateTransition(async () => { if (!pq) return const result = await modifyPq({ id: pq.id, ...input, }) if (!result.success && 'error' in result) { toast.error(result.error) } else { toast.error("Failed to update PQ criteria") } form.reset() props.onOpenChange?.(false) toast.success("PQ criteria updated successfully") router.refresh() }) } return ( Update PQ Criteria Update the PQ criteria details and save the changes
{/* Code 필드 */} ( Code * )} /> {/* Check Point 필드 */} ( Check Point * )} /> {/* Group Name 필드 (Select) */} ( Group * )} /> {/* Description 필드 */} ( Description