"use client" import * as React from "react" import { zodResolver } from "@hookform/resolvers/zod" import { Check, ChevronsUpDown, Loader } from "lucide-react" import { useForm } from "react-hook-form" import { toast } from "sonner" 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 { Button } from "@/components/ui/button" import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form" import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select" import { Sheet, SheetClose, SheetContent, SheetDescription, SheetFooter, SheetHeader, SheetTitle, } from "@/components/ui/sheet" import { Input } from "@/components/ui/input" import { Textarea } from "@/components/ui/textarea" import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover" import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "@/components/ui/command" import { useSession } from "next-auth/react" // next-auth 세션 훅 import { updateVendorCandidateSchema, UpdateVendorCandidateSchema } from "../validations" import { updateVendorCandidate } from "../service" import { vendorCandidates,VendorCandidatesWithVendorInfo} from "@/db/schema" // 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, })) interface UpdateCandidateSheetProps extends React.ComponentPropsWithRef { candidate: VendorCandidatesWithVendorInfo | null } export function UpdateCandidateSheet({ candidate, ...props }: UpdateCandidateSheetProps) { const [isUpdatePending, startUpdateTransition] = React.useTransition() const { data: session, status } = useSession() // Set default values from candidate data when the component receives a new candidate React.useEffect(() => { if (candidate) { form.reset({ id: candidate.id, companyName: candidate.companyName, taxId: candidate.taxId, contactEmail: candidate.contactEmail || "", // null을 빈 문자열로 변환 contactPhone: candidate.contactPhone || "", address: candidate.address || "", country: candidate.country || "", source: candidate.source || "", items: candidate.items, remark: candidate.remark || "", status: candidate.status, }) } }, [candidate]) const form = useForm({ resolver: zodResolver(updateVendorCandidateSchema), defaultValues: { id: candidate?.id || 0, companyName: candidate?.companyName || "", taxId: candidate?.taxId || "", contactEmail: candidate?.contactEmail || "", contactPhone: candidate?.contactPhone || "", address: candidate?.address || "", country: candidate?.country || "", source: candidate?.source || "", items: candidate?.items || "", remark: candidate?.remark || "", status: candidate?.status || "COLLECTED", }, }) function onSubmit(input: UpdateVendorCandidateSchema) { startUpdateTransition(async () => { if (!session?.user?.id) { toast.error("인증 오류. 로그인 정보를 찾을 수 없습니다.") return } const userId = Number(session.user.id) if (!candidate) return const { error } = await updateVendorCandidate({ ...input, }, userId) if (error) { toast.error(error) return } form.reset() props.onOpenChange?.(false) toast.success("Vendor candidate updated") }) } return ( Update Vendor Candidate Update the vendor candidate details and save the changes
{/* Company Name Field */} ( Company Name * )} /> {/* Tax ID Field */} ( Tax ID )} /> {/* Contact Email Field */} ( Contact Email )} /> {/* Contact Phone Field */} ( Contact Phone )} /> {/* Address Field */} ( Address )} /> {/* Country Field */} { const selectedCountry = countryArray.find( (c) => c.code === field.value ) return ( Country No country found. {countryArray.map((country) => ( field.onChange(country.code) } > {country.label} ))} ) }} /> {/* Source Field */} ( Source * )} /> {/* Items Field */} ( Items *