import { useEffect, useState, useRef } from 'react' import { useParams, Link } from 'react-router-dom' import { Dog, GitBranch, Edit, Upload, Trash2 } from 'lucide-react' import axios from 'axios' import DogForm from '../components/DogForm' function DogDetail() { const { id } = useParams() const [dog, setDog] = useState(null) const [loading, setLoading] = useState(true) const [showEditModal, setShowEditModal] = useState(false) const [uploading, setUploading] = useState(false) const fileInputRef = useRef(null) useEffect(() => { fetchDog() }, [id]) const fetchDog = async () => { try { const res = await axios.get(`/api/dogs/${id}`) setDog(res.data) setLoading(false) } catch (error) { console.error('Error fetching dog:', error) setLoading(false) } } const handlePhotoUpload = async (e) => { const file = e.target.files[0] if (!file) return setUploading(true) const formData = new FormData() formData.append('photo', file) try { await axios.post(`/api/dogs/${id}/photos`, formData, { headers: { 'Content-Type': 'multipart/form-data' } }) fetchDog() } catch (error) { console.error('Error uploading photo:', error) alert('Failed to upload photo') } finally { setUploading(false) if (fileInputRef.current) fileInputRef.current.value = '' } } const handleDeletePhoto = async (photoIndex) => { if (!confirm('Delete this photo?')) return try { await axios.delete(`/api/dogs/${id}/photos/${photoIndex}`) fetchDog() } catch (error) { console.error('Error deleting photo:', error) alert('Failed to delete photo') } } if (loading) { return
Loading...
} if (!dog) { return
Dog not found
} return (

{dog.name}

View Pedigree

Basic Information

Breed: {dog.breed}
Sex: {dog.sex === 'male' ? 'Male ♂' : 'Female ♀'}
{dog.birth_date && (
Birth Date: {new Date(dog.birth_date).toLocaleDateString()}
)} {dog.color && (
Color: {dog.color}
)} {dog.registration_number && (
Registration: {dog.registration_number}
)} {dog.microchip && (
Microchip: {dog.microchip}
)}

Photos

{dog.photo_urls && dog.photo_urls.length > 0 ? (
{dog.photo_urls.map((url, index) => (
{`${dog.name}
))}
) : (

No photos uploaded

)}
{dog.notes && (

Notes

{dog.notes}

)}

Parents

Sire (Father)

{dog.sire ? ( {dog.sire.name} ) : (

Unknown

)}

Dam (Mother)

{dog.dam ? ( {dog.dam.name} ) : (

Unknown

)}
{dog.offspring && dog.offspring.length > 0 && (

Offspring ({dog.offspring.length})

{dog.offspring.map(child => ( {child.name} - {child.sex === 'male' ? '♂' : '♀'} ))}
)} {showEditModal && ( setShowEditModal(false)} onSave={() => { fetchDog() setShowEditModal(false) }} /> )}
) } export default DogDetail