'use client'; import { useState, useEffect } from 'react'; import AlertModal from './AlertModal'; interface UniversPro { id: string; nom: string; prenom: string; telephone: string; email: string; adresse: string; nomEntreprise: string; } interface UniversProFormProps { contact: UniversPro | null; onClose: () => void; } export default function UniversProForm({ contact, onClose }: UniversProFormProps) { const [loading, setLoading] = useState(false); const [alertModal, setAlertModal] = useState<{ show: boolean; type: 'success' | 'error' | 'info' | 'warning'; title: string; message: string; } | null>(null); const [formData, setFormData] = useState({ nom: '', prenom: '', telephone: '', email: '', adresse: '', nomEntreprise: '', }); useEffect(() => { if (contact) { setFormData({ nom: contact.nom, prenom: contact.prenom, telephone: contact.telephone, email: contact.email, adresse: contact.adresse, nomEntreprise: contact.nomEntreprise, }); } }, [contact]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); try { const url = contact ? `/api/univers-pro/${contact.id}` : '/api/univers-pro'; const method = contact ? 'PUT' : 'POST'; const response = await fetch(url, { method, headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(formData), }); if (response.ok) { onClose(); } else { const error = await response.json(); setAlertModal({ show: true, type: 'error', title: 'Erreur', message: error.error || 'Une erreur est survenue', }); } } catch (error) { console.error('Erreur:', error); setAlertModal({ show: true, type: 'error', title: 'Erreur', message: 'Une erreur est survenue', }); } finally { setLoading(false); } }; return (
{contact ? 'Modifiez les informations du contact ci-dessous.' : 'Remplissez les informations pour créer un nouveau contact professionnel.'}