diff --git a/client/src/App.jsx b/client/src/App.jsx index 729ff39..bf0bfb1 100644 --- a/client/src/App.jsx +++ b/client/src/App.jsx @@ -1,5 +1,5 @@ import { BrowserRouter as Router, Routes, Route, Link, useLocation } from 'react-router-dom' -import { Home, Users, Activity, Heart, FlaskConical, Settings } from 'lucide-react' +import { Home, PawPrint, Activity, Heart, FlaskConical, Settings } from 'lucide-react' import Dashboard from './pages/Dashboard' import DogList from './pages/DogList' import DogDetail from './pages/DogDetail' @@ -41,7 +41,7 @@ function AppInner() {
- + diff --git a/client/src/pages/DogList.jsx b/client/src/pages/DogList.jsx index 1815348..ba5f79c 100644 --- a/client/src/pages/DogList.jsx +++ b/client/src/pages/DogList.jsx @@ -1,6 +1,6 @@ import { useEffect, useState } from 'react' -import { Link } from 'react-router-dom' -import { Dog, Plus, Search, Calendar, Hash, ArrowRight } from 'lucide-react' +import { Link, useNavigate } from 'react-router-dom' +import { Dog, Plus, Search, Calendar, Hash, ArrowRight, Trash2 } from 'lucide-react' import axios from 'axios' import DogForm from '../components/DogForm' import { ChampionBadge, ChampionBloodlineBadge } from '../components/ChampionBadge' @@ -12,6 +12,8 @@ function DogList() { const [sexFilter, setSexFilter] = useState('all') const [loading, setLoading] = useState(true) const [showAddModal, setShowAddModal] = useState(false) + const [deleteTarget, setDeleteTarget] = useState(null) // { id, name } + const [deleting, setDeleting] = useState(false) useEffect(() => { fetchDogs() }, []) useEffect(() => { filterDogs() }, [dogs, search, sexFilter]) @@ -43,6 +45,21 @@ function DogList() { const handleSave = () => { fetchDogs() } + const handleDelete = async () => { + if (!deleteTarget) return + setDeleting(true) + try { + await axios.delete(`/api/dogs/${deleteTarget.id}`) + setDogs(prev => prev.filter(d => d.id !== deleteTarget.id)) + setDeleteTarget(null) + } catch (err) { + console.error('Delete failed:', err) + alert('Failed to delete dog. Please try again.') + } finally { + setDeleting(false) + } + } + const calculateAge = (birthDate) => { if (!birthDate) return null const today = new Date() @@ -55,7 +72,6 @@ function DogList() { 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) @@ -132,18 +148,15 @@ function DogList() { ) : (
{filteredDogs.map(dog => ( - { e.currentTarget.style.borderColor = 'var(--primary)' @@ -157,36 +170,44 @@ function DogList() { }} > {/* Avatar */} -
- {dog.photo_urls && dog.photo_urls.length > 0 ? ( - {dog.name} - ) : ( - - )} -
+ +
+ {dog.photo_urls && dog.photo_urls.length > 0 ? ( + {dog.name} + ) : ( + + )} +
+ - {/* Info */} -
+ {/* Info — clicking navigates to detail */} +

)} -

+ -
- + {/* Actions */} +
+ + + +
- +
))}
)} + {/* Add Dog Modal */} {showAddModal && ( setShowAddModal(false)} onSave={handleSave} /> )} + + {/* Delete Confirmation Modal */} + {deleteTarget && ( +
+
+
+ +
+

Delete Dog?

+

+ {deleteTarget.name} will be + permanently removed along with all parent relationships, health records, + and heat cycles. This cannot be undone. +

+
+ + +
+
+
+ )}
) } diff --git a/server/routes/dogs.js b/server/routes/dogs.js index f5178f4..575924f 100644 --- a/server/routes/dogs.js +++ b/server/routes/dogs.js @@ -1,9 +1,9 @@ const express = require('express'); -const router = express.Router(); +const router = express.Router(); const { getDatabase } = require('../db/init'); const multer = require('multer'); -const path = require('path'); -const fs = require('fs'); +const path = require('path'); +const fs = require('fs'); const storage = multer.diskStorage({ destination: (req, file, cb) => { @@ -31,14 +31,14 @@ const upload = multer({ const emptyToNull = (v) => (v === '' || v === undefined) ? null : v; -// ── Shared SELECT columns ───────────────────────────────────────────── +// ── Shared SELECT columns ──────────────────────────────────────────── const DOG_COLS = ` id, name, registration_number, breed, sex, birth_date, color, microchip, photo_urls, notes, litter_id, is_active, is_champion, created_at, updated_at `; -// ── GET all dogs ─────────────────────────────────────────────────── +// ── GET all dogs ───────────────────────────────────────────────────── router.get('/', (req, res) => { try { const db = getDatabase(); @@ -49,7 +49,6 @@ router.get('/', (req, res) => { ORDER BY name `).all(); - // Also pull sire/dam so list page can compute bloodline status const parentStmt = db.prepare(` SELECT p.parent_type, d.id, d.name, d.is_champion FROM parents p @@ -71,7 +70,7 @@ router.get('/', (req, res) => { } }); -// ── GET single dog (with parents + offspring) ─────────────────────── +// ── GET single dog (with parents + offspring) ──────────────────────── router.get('/:id', (req, res) => { try { const db = getDatabase(); @@ -81,7 +80,6 @@ router.get('/:id', (req, res) => { dog.photo_urls = dog.photo_urls ? JSON.parse(dog.photo_urls) : []; - // Parents — include is_champion so frontend can render bloodline badge const parents = db.prepare(` SELECT p.parent_type, d.id, d.name, d.is_champion FROM parents p @@ -92,7 +90,6 @@ router.get('/:id', (req, res) => { dog.sire = parents.find(p => p.parent_type === 'sire') || null; dog.dam = parents.find(p => p.parent_type === 'dam') || null; - // Offspring — include is_champion for badge on offspring cards dog.offspring = db.prepare(` SELECT d.id, d.name, d.sex, d.is_champion FROM dogs d @@ -107,7 +104,7 @@ router.get('/:id', (req, res) => { } }); -// ── POST create dog ──────────────────────────────────────────────── +// ── POST create dog ────────────────────────────────────────────────── router.post('/', (req, res) => { try { const { name, registration_number, breed, sex, birth_date, color, @@ -138,7 +135,7 @@ router.post('/', (req, res) => { ); const dogId = result.lastInsertRowid; - console.log(`✓ Dog inserted with ID: ${dogId}`); + console.log(`✔ Dog inserted with ID: ${dogId}`); if (sire_id && sire_id !== '' && sire_id !== null) { db.prepare('INSERT INTO parents (dog_id, parent_id, parent_type) VALUES (?, ?, ?)').run(dogId, sire_id, 'sire'); @@ -150,7 +147,7 @@ router.post('/', (req, res) => { const dog = db.prepare(`SELECT ${DOG_COLS} FROM dogs WHERE id = ?`).get(dogId); dog.photo_urls = []; - console.log(`✓ Dog created: ${dog.name} (ID: ${dogId})`); + console.log(`✔ Dog created: ${dog.name} (ID: ${dogId})`); res.status(201).json(dog); } catch (error) { console.error('Error creating dog:', error); @@ -158,7 +155,7 @@ router.post('/', (req, res) => { } }); -// ── PUT update dog ──────────────────────────────────────────────── +// ── PUT update dog ─────────────────────────────────────────────────── router.put('/:id', (req, res) => { try { const { name, registration_number, breed, sex, birth_date, color, @@ -186,7 +183,6 @@ router.put('/:id', (req, res) => { req.params.id ); - // Re-link parents db.prepare('DELETE FROM parents WHERE dog_id = ?').run(req.params.id); if (sire_id && sire_id !== '' && sire_id !== null) { db.prepare('INSERT INTO parents (dog_id, parent_id, parent_type) VALUES (?, ?, ?)').run(req.params.id, sire_id, 'sire'); @@ -198,7 +194,7 @@ router.put('/:id', (req, res) => { const dog = db.prepare(`SELECT ${DOG_COLS} FROM dogs WHERE id = ?`).get(req.params.id); dog.photo_urls = dog.photo_urls ? JSON.parse(dog.photo_urls) : []; - console.log(`✓ Dog updated: ${dog.name} (ID: ${req.params.id})`); + console.log(`✔ Dog updated: ${dog.name} (ID: ${req.params.id})`); res.json(dog); } catch (error) { console.error('Error updating dog:', error); @@ -206,20 +202,34 @@ router.put('/:id', (req, res) => { } }); -// ── DELETE dog (soft) ─────────────────────────────────────────────── +// ── DELETE dog (hard delete with cascade) ──────────────────────────── +// Removes: parent relationships (both directions), health records, +// heat cycles, and the dog record itself. +// Photo files on disk are NOT removed here — run a gc job if needed. router.delete('/:id', (req, res) => { try { const db = getDatabase(); - db.prepare('UPDATE dogs SET is_active = 0 WHERE id = ?').run(req.params.id); - console.log(`✓ Dog soft-deleted: ID ${req.params.id}`); - res.json({ message: 'Dog deleted successfully' }); + const existing = db.prepare('SELECT id, name FROM dogs WHERE id = ?').get(req.params.id); + if (!existing) return res.status(404).json({ error: 'Dog not found' }); + + const id = req.params.id; + + // Cascade cleanup + db.prepare('DELETE FROM parents WHERE parent_id = ?').run(id); // remove as parent + db.prepare('DELETE FROM parents WHERE dog_id = ?').run(id); // remove own parents + db.prepare('DELETE FROM health_records WHERE dog_id = ?').run(id); + db.prepare('DELETE FROM heat_cycles WHERE dog_id = ?').run(id); + db.prepare('DELETE FROM dogs WHERE id = ?').run(id); + + console.log(`✔ Dog #${id} (${existing.name}) permanently deleted`); + res.json({ success: true, message: `${existing.name} has been deleted` }); } catch (error) { console.error('Error deleting dog:', error); res.status(500).json({ error: error.message }); } }); -// ── POST upload photo ─────────────────────────────────────────────── +// ── POST upload photo ──────────────────────────────────────────────── router.post('/:id/photos', upload.single('photo'), (req, res) => { try { if (!req.file) return res.status(400).json({ error: 'No file uploaded' });