'use client'; import { useEffect, useState, useCallback } from 'react'; import { api } from '@/lib/api'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { toast } from 'sonner'; import { Plus } from 'lucide-react'; interface City { id: string; name: string; } interface Region { id: string; name: string; cities: City[]; } interface Country { id: string; name: string; regions: Region[]; } export default function CitiesPage() { const [countries, setCountries] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [newCity, setNewCity] = useState(''); const [selectedRegionId, setSelectedRegionId] = useState(''); const load = useCallback(() => { setLoading(true); setError(null); api.get('/locations/countries') .then(setCountries) .catch(() => setError('Error al cargar ciudades')) .finally(() => setLoading(false)); }, []); useEffect(() => { load(); }, [load]); const addCity = async () => { if (!selectedRegionId || !newCity.trim()) { toast.error('Selecciona una región y escribe un nombre'); return; } try { await api.post('/locations/cities', { region_id: selectedRegionId, name: newCity.trim() }); toast.success('Ciudad agregada'); setNewCity(''); load(); } catch (e: any) { toast.error(e?.message || 'Error al agregar ciudad'); } }; if (loading) { return
Cargando...
; } if (error) { return (

{error}

); } return (

Ciudades

Agregar ciudad setNewCity(e.target.value)} placeholder="Nombre de la ciudad" /> {countries.map((c) => (

{c.name}

{c.regions?.map((r) => (
{r.name} ({r.cities?.length || 0} ciudades)
{r.cities?.map((city) => ( {city.name} ))}
))}
))}
); }