"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 { updatePqCriteria } from "../service" import { groupOptions } from "./add-pq-dialog" import { Checkbox } from "@/components/ui/checkbox" // 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(), inputFormat: z.string().default("TEXT"), subGroupName: z.string().optional(), }); type UpdatePqSchema = z.infer; // 입력 형식 옵션 const inputFormatOptions = [ { value: "TEXT", label: "텍스트" }, { value: "FILE", label: "파일" }, { value: "EMAIL", label: "이메일" }, { value: "PHONE", label: "전화번호" }, { value: "NUMBER", label: "숫자" }, { value: "NUMBER_WITH_UNIT", label: "숫자+단위" }, { value: "TEXT_FILE", label: "텍스트 + 파일" } ]; interface UpdatePqSheetProps extends React.ComponentPropsWithRef { pq: { id: number; code: string; checkPoint: string; description: string | null; remarks: string | null; groupName: string | null; inputFormat: string; subGroupName: 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 ?? "", inputFormat: pq?.inputFormat ?? "TEXT", subGroupName: pq?.subGroupName ?? "", }, }) // 폼 초기화 (pq가 변경될 때) React.useEffect(() => { if (pq) { form.reset({ code: pq.code, checkPoint: pq.checkPoint, groupName: pq.groupName ?? groupOptions[0], description: pq.description ?? "", remarks: pq.remarks ?? "", inputFormat: pq.inputFormat ?? "TEXT", subGroupName: pq.subGroupName ?? "", }); } }, [pq, form]); function onSubmit(input: UpdatePqSchema) { startUpdateTransition(async () => { if (!pq) return const result = await updatePqCriteria(pq.id, input) if (!result.success) { toast.error(result.message || "PQ 항목 수정에 실패했습니다") return } toast.success(result.message || "PQ 항목이 성공적으로 수정되었습니다") form.reset() props.onOpenChange?.(false) 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 * )} /> {/* Sub Group Name 필드 */} ( Sub Group Name )} /> {/* Input Format 필드 */} ( 입력 형식 )} /> {/* Required 체크박스 */} {/* Description 필드 */} ( Description