import { useState, useEffect } from "react"; import type { castrations } from "~/database/schema"; interface CastrationTrackerProps { castrations: (typeof castrations.$inferSelect)[]; totalCount: number; } export function CastrationTracker({ castrations: initialCastrations, totalCount: initialCount, }: CastrationTrackerProps) { const [castrations, setCastrations] = useState(initialCastrations); const [totalCount, setTotalCount] = useState(initialCount); const [loading, setLoading] = useState(false); const handleCastration = async (gender: "male" | "female") => { setLoading(true); try { const response = await fetch("/api/castration", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ gender }), }); if (response.ok) { const result = await response.json(); // Add new castration to the list const newCastration = { id: castrations.length + 1, gender, timestamp: new Date(), }; setCastrations([newCastration, ...castrations]); setTotalCount(totalCount + 1); } } catch (error) { console.error("Failed to record castration:", error); } finally { setLoading(false); } }; const formatTime = (date: Date | string) => { const d = typeof date === "string" ? new Date(date) : date; return d.toLocaleString(); }; return (
{totalCount}
Total Castrations
{castration.gender}
{formatTime(castration.timestamp)}
No castrations recorded yet. Start by clicking a button above!