diff options
| author | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-06-19 09:44:28 +0000 |
|---|---|---|
| committer | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-06-19 09:44:28 +0000 |
| commit | 95bbe9c583ff841220da1267630e7b2025fc36dc (patch) | |
| tree | 5e3d5bb3302530bbaa7f7abbe8c9cf8193ccbd4c /lib/general-check-list/table/add-check-list-dialog.tsx | |
| parent | 0eb030580b5cbe5f03d570c3c9d8c519bac3b783 (diff) | |
(대표님) 20250619 1844 KST 작업사항
Diffstat (limited to 'lib/general-check-list/table/add-check-list-dialog.tsx')
| -rw-r--r-- | lib/general-check-list/table/add-check-list-dialog.tsx | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/lib/general-check-list/table/add-check-list-dialog.tsx b/lib/general-check-list/table/add-check-list-dialog.tsx new file mode 100644 index 00000000..5721bd59 --- /dev/null +++ b/lib/general-check-list/table/add-check-list-dialog.tsx @@ -0,0 +1,112 @@ +"use client"; +import * as React from "react"; +import { z } from "zod"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { useForm } from "react-hook-form"; +import { Dialog, DialogTrigger, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter } from "@/components/ui/dialog"; +import { Button } from "@/components/ui/button"; +import { Input } from "@/components/ui/input"; +import { Form, FormField, FormItem, FormLabel, FormControl, FormMessage } from "@/components/ui/form"; +import { Plus } from "lucide-react"; +import { toast } from "sonner"; +import { createGeneralEvaluation } from "@/lib/general-check-list/service"; +import { useRouter } from "next/navigation"; + +const schema = z.object({ + category: z.string().min(1, "카테고리를 입력하세요"), + inspectionItem: z.string().min(1, "점검 항목을 입력하세요"), + remarks: z.string().optional(), +}); + +type FormValues = z.infer<typeof schema>; + +export function CreateEvaluationDialog({ onSuccess }: { onSuccess?: () => void }) { + const [open, setOpen] = React.useState(false); + const [pending, setPending] = React.useState(false); + const router = useRouter(); // ⬅️ + + const form = useForm<FormValues>({ + resolver: zodResolver(schema), + defaultValues: { category: "", inspectionItem: "", remarks: "" }, + }); + + async function onSubmit(values: FormValues) { + setPending(true); + const res = await createGeneralEvaluation(values); + if (res.success) { + toast.success(res.message); + router.refresh(); // ❷ 새로고침 + + onSuccess?.(); + setOpen(false); + form.reset(); + } else { + toast.error(res.message); + } + setPending(false); + } + + return ( + <Dialog open={open} onOpenChange={(v) => !pending && setOpen(v)}> + <DialogTrigger asChild> + <Button variant="outline" size="sm" className="gap-2"> + <Plus className="size-4" /> 새 항목 + </Button> + </DialogTrigger> + <DialogContent className="sm:max-w-[480px]"> + <DialogHeader> + <DialogTitle>새 정기평가 체크리스트</DialogTitle> + <DialogDescription>점검 항목을 추가합니다.</DialogDescription> + </DialogHeader> + <Form {...form}> + <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4"> + <FormField + control={form.control} + name="category" + render={({ field }) => ( + <FormItem> + <FormLabel>카테고리</FormLabel> + <FormControl> + <Input placeholder="예: 안전" {...field} /> + </FormControl> + <FormMessage /> + </FormItem> + )} + /> + <FormField + control={form.control} + name="inspectionItem" + render={({ field }) => ( + <FormItem> + <FormLabel>점검 항목</FormLabel> + <FormControl> + <Input placeholder="예: 안전모 착용 여부" {...field} /> + </FormControl> + <FormMessage /> + </FormItem> + )} + /> + <FormField + control={form.control} + name="remarks" + render={({ field }) => ( + <FormItem> + <FormLabel>비고 (선택)</FormLabel> + <FormControl> + <Input placeholder="메모" {...field} /> + </FormControl> + <FormMessage /> + </FormItem> + )} + /> + <DialogFooter> + <Button type="submit" disabled={pending}> + {pending ? "저장중..." : "저장"} + </Button> + </DialogFooter> + </form> + </Form> + </DialogContent> + </Dialog> + ); +}
\ No newline at end of file |
