import { useState, useEffect } from 'react'; import { useParams, useNavigate } from 'react-router-dom'; import ContactForm, { ContactFormData, emptyFormData } from '../components/ContactForm.tsx'; import api from '../services/api.ts'; export default function ContactsEdit() { const { contactId } = useParams<{ contactId: string }>(); const navigate = useNavigate(); const [initialData, setInitialData] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { loadContact(); }, [contactId]); const loadContact = async () => { try { setLoading(true); const response = await api.get(`/contacts/${contactId}`); const c = response.data; setInitialData({ type: c.type, company_name: c.company_name || '', first_name: c.first_name || '', last_name: c.last_name || '', email: c.email || '', phone: c.phone || '', mobile: c.mobile || '', website: c.website || '', billing_street: c.billing_street || '', billing_number: c.billing_number || '', billing_postalcode: c.billing_postalcode || '', billing_city: c.billing_city || '', billing_country: c.billing_country || '', shipping_street: c.shipping_street || '', shipping_number: c.shipping_number || '', shipping_postalcode: c.shipping_postalcode || '', shipping_city: c.shipping_city || '', shipping_country: c.shipping_country || '', tax_number: c.tax_number || '', note: c.note || '', tag_ids: c.tags?.map((t: any) => t.id) || [], }); } catch (err: any) { setError(err.response?.data?.detail || 'Failed to load contact'); } finally { setLoading(false); } }; const handleUpdate = async (formData: ContactFormData) => { await api.put(`/contacts/${contactId}`, formData); navigate(`/contacts/${contactId}`); }; if (loading) return

Loading contact...

; if (error) return

{error}

; if (!initialData) return

Contact not found.

; return (

Edit Contact

); }