"use client" import * as React from "react" import { zodResolver } from "@hookform/resolvers/zod" import { useForm } from "react-hook-form" import { toast } from "sonner" import { Button } from "@/components/ui/button" import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form" import { Input } from "@/components/ui/input" import { Sheet, SheetContent, SheetDescription, SheetHeader, SheetTitle, } from "@/components/ui/sheet" import { Checkbox } from "@/components/ui/checkbox" import { Loader2 } from "lucide-react" import type { TechVendorContact } from "@/db/schema/techVendors" import { updateTechVendorContactSchema, type UpdateTechVendorContactSchema } from "../validations" import { updateTechVendorContact } from "../service" interface UpdateContactSheetProps extends React.ComponentPropsWithoutRef { contact: TechVendorContact | null vendorId: number } export function UpdateContactSheet({ contact, vendorId, ...props }: UpdateContactSheetProps) { const [isPending, startTransition] = React.useTransition() const form = useForm({ resolver: zodResolver(updateTechVendorContactSchema), defaultValues: { contactName: contact?.contactName ?? "", contactPosition: contact?.contactPosition ?? "", contactEmail: contact?.contactEmail ?? "", contactPhone: contact?.contactPhone ?? "", contactCountry: contact?.contactCountry ?? "", contactTitle: contact?.contactTitle ?? "", isPrimary: contact?.isPrimary ?? false, }, }) React.useEffect(() => { if (contact) { form.reset({ contactName: contact.contactName, contactPosition: contact.contactPosition ?? "", contactEmail: contact.contactEmail, contactPhone: contact.contactPhone ?? "", contactCountry: contact.contactCountry ?? "", contactTitle: contact.contactTitle ?? "", isPrimary: contact.isPrimary, }) } }, [contact, form]) async function onSubmit(data: UpdateTechVendorContactSchema) { if (!contact) return startTransition(async () => { try { const { error } = await updateTechVendorContact({ id: contact.id, vendorId: vendorId, ...data }) if (error) throw new Error(error) toast.success("연락처 정보가 업데이트되었습니다!") form.reset() props.onOpenChange?.(false) } catch (err: unknown) { toast.error(String(err)) } }) } return ( 연락처 수정 연락처 정보를 수정하세요. 완료되면 저장 버튼을 클릭하세요.
( 담당자명 * )} /> ( 직책 )} /> ( 직책 )} /> ( 이메일 )} /> ( 전화번호 )} /> ( 국가 )} /> (
주 담당자
)} />
) }