1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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>
);
}
|