// components/vendors/EditVendorModal.tsx 'use client' import { useState } from 'react' import { Vendor, VendorType, VendorStatus } from '@prisma/client' import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription } from '@/components/ui/dialog' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Textarea } from '@/components/ui/textarea' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select' import { Checkbox } from '@/components/ui/checkbox' import { toast } from 'sonner' interface EditVendorModalProps { vendor: { id: string name: string type: VendorType description?: string | null website?: string | null contactPerson?: string | null email?: string | null phone?: string | null address?: { street: string city: string state: string zip: number } | null status: VendorStatus isBooked: boolean bookedDate?: Date | null quotedPrice?: number | null finalCost?: number | null depositPaid?: number | null depositDueDate?: Date | null finalPaymentDue?: Date | null paymentNotes?: string | null contractUrl?: string | null proposalUrl?: string | null notes?: string | null } isOpen: boolean onClose: () => void onSave: (updatedVendor: any) => Promise } const VENDOR_TYPES = Object.values(VendorType) const VENDOR_STATUSES = Object.values(VendorStatus) export function EditVendorModal({ vendor, isOpen, onClose, onSave }: EditVendorModalProps) { const [loading, setLoading] = useState(false) const [formData, setFormData] = useState({ name: vendor.name, type: vendor.type, description: vendor.description || '', website: vendor.website || '', contactPerson: vendor.contactPerson || '', email: vendor.email || '', phone: vendor.phone || '', street: vendor.address?.street || '', city: vendor.address?.city || '', state: vendor.address?.state || '', postalCode: vendor.address?.zip?.toString() || '', status: vendor.status, isBooked: vendor.isBooked, bookedDate: vendor.bookedDate ? new Date(vendor.bookedDate).toISOString().split('T')[0] : '', quotedPrice: vendor.quotedPrice?.toString() || '', finalCost: vendor.finalCost?.toString() || '', depositPaid: vendor.depositPaid?.toString() || '', depositDueDate: vendor.depositDueDate ? new Date(vendor.depositDueDate).toISOString().split('T')[0] : '', finalPaymentDue: vendor.finalPaymentDue ? new Date(vendor.finalPaymentDue).toISOString().split('T')[0] : '', paymentNotes: vendor.paymentNotes || '', contractUrl: vendor.contractUrl || '', proposalUrl: vendor.proposalUrl || '', notes: vendor.notes || '', }) const handleChange = (e: React.ChangeEvent) => { const { name, value, type } = e.target setFormData(prev => ({ ...prev, [name]: type === 'checkbox' ? (e.target as HTMLInputElement).checked : value })) } const handleSelectChange = (name: string, value: string) => { setFormData(prev => ({ ...prev, [name]: value })) } const handleCheckboxChange = (name: string, checked: boolean) => { setFormData(prev => ({ ...prev, [name]: checked })) } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setLoading(true) try { const response = await fetch(`/api/vendors/${vendor.id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(formData), }) if (!response.ok) throw new Error('Failed to update vendor') const updatedVendor = await response.json() await onSave(updatedVendor) toast.success('Vendor updated successfully!') onClose() } catch (error) { console.error('Error updating vendor:', error) toast.error('Failed to update vendor') } finally { setLoading(false) } } return ( Edit Vendor Update the vendor information below
{/* Basic Information */}