From 36dd60ca6fce7712b35e6d7c1b9602710f442ada Mon Sep 17 00:00:00 2001 From: dujinkim Date: Wed, 28 May 2025 12:26:28 +0000 Subject: (최겸) 기술영업 해양 rfq 개발v1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/rfqs-tech/table/update-rfq-sheet.tsx | 243 +++++++++++++++++++++++++++++++ 1 file changed, 243 insertions(+) create mode 100644 lib/rfqs-tech/table/update-rfq-sheet.tsx (limited to 'lib/rfqs-tech/table/update-rfq-sheet.tsx') diff --git a/lib/rfqs-tech/table/update-rfq-sheet.tsx b/lib/rfqs-tech/table/update-rfq-sheet.tsx new file mode 100644 index 00000000..9517bc89 --- /dev/null +++ b/lib/rfqs-tech/table/update-rfq-sheet.tsx @@ -0,0 +1,243 @@ +"use client" + +import * as React from "react" +import { zodResolver } from "@hookform/resolvers/zod" +import { useForm } from "react-hook-form" +import { toast } from "sonner" + +import { Button } from "@/components/ui/button" +import { + Form, + FormControl, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "@/components/ui/form" +import { Input } from "@/components/ui/input" +import { + Sheet, + SheetClose, + SheetContent, + SheetDescription, + SheetFooter, + SheetHeader, + SheetTitle, +} from "@/components/ui/sheet" +import { + Select, + SelectContent, + SelectItem, + SelectTrigger, + SelectValue, +} from "@/components/ui/select" + +import { updateRfqSchema, type UpdateRfqSchema } from "../validations" +import { modifyRfq } from "../service" +import { RfqWithItemCount } from "@/db/schema/rfq" +import { useSession } from "next-auth/react" +import { ProjectSelector } from "@/components/ProjectSelector" +import { Project } from "../service" + +interface UpdateRfqSheetProps + extends React.ComponentPropsWithRef { + rfq: RfqWithItemCount | null +} + +export function UpdateRfqSheet({ rfq, ...props }: UpdateRfqSheetProps) { + const { data: session } = useSession() + const userId = Number(session?.user?.id || 1) + + // RHF setup + const form = useForm({ + resolver: zodResolver(updateRfqSchema), + defaultValues: { + id: rfq?.rfqId ?? 0, // PK + rfqCode: rfq?.rfqCode ?? "", + description: rfq?.description ?? "", + projectId: rfq?.projectId, // 프로젝트 ID + dueDate: rfq?.dueDate ?? undefined, // null을 undefined로 변환 + status: rfq?.status ?? "DRAFT", + createdBy: rfq?.createdBy ?? userId, + }, + }); + + // 프로젝트 선택 처리 + const handleProjectSelect = (project: Project | null) => { + if (project === null) { + return; + } + form.setValue("projectId", project.id); + }; + + async function onSubmit(input: UpdateRfqSchema) { + const { error } = await modifyRfq({ + ...input, + }) + + if (error) { + toast.error(error) + return + } + + form.reset() + props.onOpenChange?.(false) // close the sheet + toast.success("RFQ updated!") + } + + return ( + + + + Update RFQ + + Update the RFQ details and save the changes + + + + {/* RHF Form */} +
+ + + {/* Hidden or code-based id field */} + ( + + )} + /> + + {/* Project Selector - 재사용 컴포넌트 사용 */} + ( + + Project + + + + + + )} + /> + + {/* rfqCode */} + ( + + RFQ Code + + + + + + )} + /> + + {/* description */} + ( + + Description + + + + + + )} + /> + + {/* dueDate */} + ( + + Due Date + + yyyy-mm-dd + value={field.value ? field.value.toISOString().slice(0, 10) : ""} + onChange={(e) => { + const val = e.target.value + field.onChange(val ? new Date(val + "T00:00:00") : undefined) + }} + /> + + + + )} + /> + + {/* status (Select) */} + ( + + Status + + + + + + )} + /> + + {/* createdBy (hidden or read-only) */} + ( + + )} + /> + + + + + + + + + +
+
+ ) +} \ No newline at end of file -- cgit v1.2.3