import { useEffect, useState } from 'react' import api from '../api' import toast from 'react-hot-toast' function Modal({ title, onClose, children }) { return (

{title}

{children}
) } const emptyForm = { first_name: '', last_name: '', email: '', phone: '' } export default function Tenants() { const [tenants, setTenants] = useState([]) const [showModal, setShowModal] = useState(false) const [form, setForm] = useState(emptyForm) const [editing, setEditing] = useState(null) const [loading, setLoading] = useState(false) const load = () => api.get('/tenants').then(r => setTenants(r.data)) useEffect(() => { load() }, []) const openAdd = () => { setForm(emptyForm); setEditing(null); setShowModal(true) } const openEdit = t => { setForm({ first_name: t.first_name, last_name: t.last_name, email: t.email || '', phone: t.phone || '' }) setEditing(t.id); setShowModal(true) } const handleSubmit = async e => { e.preventDefault(); setLoading(true) try { if (editing) await api.put(`/tenants/${editing}`, form) else await api.post('/tenants', form) toast.success(editing ? 'Locataire modifié !' : 'Locataire ajouté !') setShowModal(false); load() } catch (err) { toast.error(err.response?.data?.error || 'Erreur') } finally { setLoading(false) } } const handleDelete = async id => { if (!confirm('Supprimer ce locataire ?')) return try { await api.delete(`/tenants/${id}`); toast.success('Locataire supprimé'); load() } catch (err) { toast.error(err.response?.data?.error || 'Erreur') } } return (

👤 Locataires

{tenants.length} locataire(s) enregistré(s)

{tenants.length === 0 ? (
👤

Aucun locataire enregistré.

) : (
{tenants.map(t => ( ))}
Nom Email Téléphone
{t.first_name} {t.last_name} {t.email || '—'} {t.phone || '—'}
)} {showModal && ( setShowModal(false)}>
setForm(f => ({ ...f, first_name: e.target.value }))} />
setForm(f => ({ ...f, last_name: e.target.value }))} />
setForm(f => ({ ...f, email: e.target.value }))} />
setForm(f => ({ ...f, phone: e.target.value }))} />
)}
) }