summaryrefslogtreecommitdiff
path: root/lib/vendor-regular-registrations/table/safety-qualification-update-dialog.tsx
blob: 80084732bc0acc56984719d5ef108b3a1ab5d128 (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"use client"

import * as React from "react"
import { useForm } from "react-hook-form"
import { zodResolver } from "@hookform/resolvers/zod"
import { z } from "zod"
import { toast } from "sonner"

import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
} from "@/components/ui/dialog"
import {
  Form,
  FormControl,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from "@/components/ui/form"
import { Button } from "@/components/ui/button"
import { Textarea } from "@/components/ui/textarea"
import { updateSafetyQualification } from "../service"

const formSchema = z.object({
  safetyQualificationContent: z.string().min(1, "안전적격성 평가 내용을 입력해주세요."),
})

interface SafetyQualificationUpdateDialogProps {
  open: boolean
  onOpenChange: (open: boolean) => void
  registrationId?: number
  vendorName?: string
  currentContent?: string | null
  onSuccess?: () => void
}

export function SafetyQualificationUpdateDialog({
  open,
  onOpenChange,
  registrationId,
  vendorName,
  currentContent,
  onSuccess,
}: SafetyQualificationUpdateDialogProps) {
  const [isLoading, setIsLoading] = React.useState(false)

  const form = useForm<z.infer<typeof formSchema>>({
    resolver: zodResolver(formSchema),
    defaultValues: {
      safetyQualificationContent: currentContent || "",
    },
  })

  // 폼 값 초기화
  React.useEffect(() => {
    if (open) {
      form.reset({
        safetyQualificationContent: currentContent || "",
      })
    }
  }, [open, currentContent, form])

  async function onSubmit(values: z.infer<typeof formSchema>) {
    if (!registrationId) {
      toast.error("등록 ID가 없습니다.")
      return
    }

    setIsLoading(true)
    try {
      const result = await updateSafetyQualification(
        registrationId,
        values.safetyQualificationContent
      )

      if (result.success) {
        toast.success("안전적격성 평가가 등록되었습니다.")
        onOpenChange(false)
        onSuccess?.()
      } else {
        toast.error(result.error || "안전적격성 평가 등록에 실패했습니다.")
      }
    } catch (error) {
      console.error("안전적격성 평가 등록 오류:", error)
      toast.error("안전적격성 평가 등록 중 오류가 발생했습니다.")
    } finally {
      setIsLoading(false)
    }
  }

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent className="w-[400px] sm:w-[540px] max-h-[90vh] overflow-y-auto">
        <DialogHeader>
          <DialogTitle>안전적격성 평가 입력</DialogTitle>
          <DialogDescription>
            {vendorName && `${vendorName}의 `}안전적격성 평가 내용을 입력해주세요.
          </DialogDescription>
        </DialogHeader>

        <Form {...form}>
          <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6 mt-6">
            <FormField
              control={form.control}
              name="safetyQualificationContent"
              render={({ field }) => (
                <FormItem>
                  <FormLabel>안전적격성 평가 내용</FormLabel>
                  <FormControl>
                    <Textarea
                      placeholder="안전적격성 평가 결과 및 내용을 입력해주세요..."
                      className="min-h-[200px]"
                      {...field}
                    />
                  </FormControl>
                  <FormMessage />
                </FormItem>
              )}
            />

            <div className="flex justify-end space-x-2">
              <Button
                type="button"
                variant="outline"
                onClick={() => onOpenChange(false)}
                disabled={isLoading}
              >
                취소
              </Button>
              <Button type="submit" disabled={isLoading}>
                {isLoading ? "저장 중..." : "저장"}
              </Button>
            </div>
          </form>
        </Form>
      </DialogContent>
    </Dialog>
  )
}