"use client"; import { useEffect, useState, useCallback } from "react"; import { useRouter } from "next/navigation"; import { useAuth } from "@/context/AuthContext"; const API_BASE = ""; export default function ProfilePage() { const {user, token, loading, logout} = useAuth(); const router = useRouter(); const [pets, setPets] = useState([]); const [loadingPets, setLoadingPets] = useState(false); const [showForm, setShowForm] = useState(false); const [editingPet, setEditingPet] = useState(null); const [petName, setPetName] = useState(""); const [species, setSpecies] = useState(""); const [breed, setBreed] = useState(""); const [submitting, setSubmitting] = useState(false); const [petError, setPetError] = useState(null); useEffect(() => { if (!loading && !user) { router.replace("/login"); } }, [user, loading, router]); const loadPets = useCallback(() => { if (!token) return; setLoadingPets(true); fetch(`${API_BASE}/api/v1/my-pets`, { headers: { Authorization: `Bearer ${token}` }, }) .then((r) => r.json()) .then(setPets) .catch(() => {}) .finally(() => setLoadingPets(false)); }, [token]); useEffect(() => { if (user?.role === "CUSTOMER") { loadPets(); } }, [user, loadPets]); function handleLogout() { logout(); router.push("/"); } function openAddForm() { setEditingPet(null); setPetName(""); setSpecies(""); setBreed(""); setPetError(null); setShowForm(true); } function openEditForm(pet) { setEditingPet(pet); setPetName(pet.petName); setSpecies(pet.species); setBreed(pet.breed || ""); setPetError(null); setShowForm(true); } function closeForm() { setShowForm(false); setEditingPet(null); setPetError(null); } async function handlePetSubmit(e) { e.preventDefault(); setPetError(null); setSubmitting(true); const url = editingPet ? `${API_BASE}/api/v1/my-pets/${editingPet.customerPetId}` : `${API_BASE}/api/v1/my-pets`; try { const res = await fetch(url, { method: editingPet ? "PUT" : "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${token}`, }, body: JSON.stringify({ petName, species, breed: breed || null }), }); if (!res.ok) { const data = await res.json().catch(() => null); throw new Error(data?.message || `Request failed (${res.status})`); } closeForm(); loadPets(); } catch (err) { setPetError(err.message); } finally { setSubmitting(false); } } async function handleDeletePet(id) { if (!confirm("Remove this pet profile?")) { return; } try { await fetch(`${API_BASE}/api/v1/my-pets/${id}`, { method: "DELETE", headers: { Authorization: `Bearer ${token}` }, }); loadPets(); } catch { } } async function handleImageUpload(petId, file) { const formData = new FormData(); formData.append("image", file); try { const res = await fetch(`${API_BASE}/api/v1/my-pets/${petId}/image`, { method: "POST", headers: { Authorization: `Bearer ${token}` }, body: formData, }); if (!res.ok) { const data = await res.json().catch(() => null); alert(data?.message || "Failed to upload image"); return; } loadPets(); } catch { alert("Failed to upload image"); } } if (loading || !user) { return

Loading…

; } const fields = [ {label: "Full Name", value: user.fullName}, {label: "Username", value: user.username}, {label: "Email", value: user.email}, {label: "Phone", value: user.phone || "—"}, {label: "Role", value: user.role}, ...(user.storeName ? [{ label: "Store", value: user.storeName }] : []), ]; return (
{(user.fullName || user.username).charAt(0).toUpperCase()}

{user.fullName || user.username}

{user.role}
{fields.map(({ label, value }) => (
{label}
{value}
))}
{user.role === "CUSTOMER" && (

My Pets

{showForm && (

{editingPet ? "Edit Pet" : "Add a New Pet"}

{petError &&
{petError}
}
)} {loadingPets ? (

Loading pets...

) : pets.length === 0 && !showForm ? (

No pet profiles yet. Add your first pet above!

) : (
{pets.map((pet) => (
{pet.imageUrl ? ( {pet.petName} ) : (
🐾
)}
{pet.petName} {pet.species} {pet.breed && {pet.breed}}
))}
)}
)}
); }