import { useEffect, useState } from 'react' import { Link } from 'react-router-dom' import { Dog, Plus, Search, Calendar, Hash, ArrowRight } from 'lucide-react' import axios from 'axios' import DogForm from '../components/DogForm' import { ChampionBadge, ChampionBloodlineBadge } from '../components/ChampionBadge' function DogList() { const [dogs, setDogs] = useState([]) const [filteredDogs, setFilteredDogs] = useState([]) const [search, setSearch] = useState('') const [sexFilter, setSexFilter] = useState('all') const [loading, setLoading] = useState(true) const [showAddModal, setShowAddModal] = useState(false) useEffect(() => { fetchDogs() }, []) useEffect(() => { filterDogs() }, [dogs, search, sexFilter]) const fetchDogs = async () => { try { const res = await axios.get('/api/dogs') setDogs(res.data) setLoading(false) } catch (error) { console.error('Error fetching dogs:', error) setLoading(false) } } const filterDogs = () => { let filtered = dogs if (search) { filtered = filtered.filter(dog => dog.name.toLowerCase().includes(search.toLowerCase()) || (dog.registration_number && dog.registration_number.toLowerCase().includes(search.toLowerCase())) ) } if (sexFilter !== 'all') { filtered = filtered.filter(dog => dog.sex === sexFilter) } setFilteredDogs(filtered) } const handleSave = () => { fetchDogs() } const calculateAge = (birthDate) => { if (!birthDate) return null const today = new Date() const birth = new Date(birthDate) let years = today.getFullYear() - birth.getFullYear() let months = today.getMonth() - birth.getMonth() if (months < 0) { years--; months += 12 } if (years === 0) return `${months}mo` if (months === 0) return `${years}y` return `${years}y ${months}mo` } // A dog has champion blood if sire or dam is a champion const hasChampionBlood = (dog) => (dog.sire && dog.sire.is_champion) || (dog.dam && dog.dam.is_champion) if (loading) { return
{filteredDogs.length} {filteredDogs.length === 1 ? 'dog' : 'dogs'} {search || sexFilter !== 'all' ? ' matching filters' : ' total'}
{search || sexFilter !== 'all' ? 'Try adjusting your search or filters' : 'Add your first dog to get started'}
{!search && sexFilter === 'all' && ( )}