"use client"; import { useState, useEffect } from "react"; import PetCard from "@/components/PetCard"; const API_BASE = ""; export default function AdoptPage() { const [pets, setPets] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [health, setHealth] = useState(null); const [search, setSearch] = useState(""); const [query, setQuery] = useState(""); const [page, setPage] = useState(0); const [totalPages, setTotalPages] = useState(0); const PAGE_SIZE = 12; useEffect(() => { fetch(`${API_BASE}/api/v1/health`) .then((res) => (res.ok ? setHealth("online") : setHealth("error"))) .catch(() => setHealth("offline")); }, []); useEffect(() => { setLoading(true); setError(null); const params = new URLSearchParams({ page, size: PAGE_SIZE, sort: "id,asc" }); if (query) params.set("q", query); fetch(`${API_BASE}/api/v1/pets?${params}`) .then((res) => { if (!res.ok) throw new Error(`HTTP ${res.status} – ${res.statusText}`); return res.json(); }) .then((data) => { setPets(data.content ?? []); setTotalPages(data.totalPages ?? 0); }) .catch((err) => setError(err.message)) .finally(() => setLoading(false)); }, [page, query]); function handleSearch(e) { e.preventDefault(); setPage(0); setQuery(search.trim()); } return (

Find Your Perfect Companion

Give a loving pet their forever home

setSearch(e.target.value)} /> {query && ( )}
{loading &&

Loading pets...

} {error && (

Failed to load pets

{error}

{health === "offline" ? "The Spring Boot backend is not reachable. Make sure it is running in IntelliJ on port 8080." : health === "error" ? "The backend responded with an error. Check the IntelliJ Run console for stack traces." : "The backend is reachable but the /pets endpoint failed. Check the IntelliJ Run console."}

)} {!loading && !error && pets.length === 0 && (

No pets found.

)} {!loading && !error && pets.length > 0 && (
{pets.map((pet) => ( ))}
)} {!loading && totalPages > 1 && (
Page {page + 1} of {totalPages}
)}
); }