"use client" import * as React from "react" import { useForm } from "react-hook-form" import { zodResolver } from "@hookform/resolvers/zod" import { Check, ChevronsUpDown } from "lucide-react" import i18nIsoCountries from "i18n-iso-countries" import enLocale from "i18n-iso-countries/langs/en.json" import koLocale from "i18n-iso-countries/langs/ko.json" import { cn } from "@/lib/utils" 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 { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover" import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@/components/ui/command" // react-hook-form + shadcn/ui Form import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form" // shadcn/ui Select import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select" import { createVendorCandidateSchema, CreateVendorCandidateSchema } from "../validations" import { createVendorCandidate } from "../service" import { vendorCandidates } from "@/db/schema/vendors" // Register locales for countries i18nIsoCountries.registerLocale(enLocale) i18nIsoCountries.registerLocale(koLocale) // Generate country array const locale = "ko" const countryMap = i18nIsoCountries.getNames(locale, { select: "official" }) const countryArray = Object.entries(countryMap).map(([code, label]) => ({ code, label, })) export function AddCandidateDialog() { const [open, setOpen] = React.useState(false) const [isSubmitting, setIsSubmitting] = React.useState(false) // react-hook-form 세팅 const form = useForm({ resolver: zodResolver(createVendorCandidateSchema), defaultValues: { companyName: "", contactEmail: "", contactPhone: "", country: "", source: "", status: "COLLECTED", // Default status set to COLLECTED }, }) async function onSubmit(data: CreateVendorCandidateSchema) { setIsSubmitting(true) try { const result = await createVendorCandidate(data) if (result.error) { alert(`에러: ${result.error}`) return } // 성공 시 모달 닫고 폼 리셋 form.reset() setOpen(false) } catch (error) { console.error("Failed to create vendor candidate:", error) alert("An unexpected error occurred") } finally { setIsSubmitting(false) } } function handleDialogOpenChange(nextOpen: boolean) { if (!nextOpen) { form.reset() } setOpen(nextOpen) } return ( {/* 모달을 열기 위한 버튼 */} Create New Vendor Candidate 새 Vendor Candidate 정보를 입력하고 Create 버튼을 누르세요. {/* shadcn/ui Form을 이용해 react-hook-form과 연결 */}
{/* Company Name 필드 */} ( Company Name )} /> {/* Contact Email 필드 */} ( Contact Email )} /> {/* Contact Phone 필드 */} ( Contact Phone )} /> {/* Country 필드 */} { const selectedCountry = countryArray.find( (c) => c.code === field.value ) return ( Country No country found. {countryArray.map((country) => ( field.onChange(country.code) } > {country.label} ))} ) }} /> {/* Source 필드 */} ( Source )} /> {/* Status 필드 */} {/* ( Status )} /> */}
) }