Can now add pets in the appointments page.

This commit is contained in:
augmentedpotato
2026-04-07 23:53:48 -06:00
parent 83477904be
commit 2469c07fef
2 changed files with 164 additions and 10 deletions

View File

@@ -195,6 +195,100 @@ function DatePicker({ value, minDate, onChange }) {
);
}
function AddPetModal({ token, onClose, onAdded }) {
const [petName, setPetName] = useState("");
const [species, setSpecies] = useState("");
const [breed, setBreed] = useState("");
const [submitting, setSubmitting] = useState(false);
const [petError, setPetError] = useState(null);
async function handleSubmit(e) {
e.preventDefault();
setPetError(null);
setSubmitting(true);
try {
const res = await fetch(`${API_BASE}/api/v1/my-pets`, {
method: "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})`);
}
onAdded();
onClose();
}
catch (err) {
setPetError(err.message);
}
finally {
setSubmitting(false);
}
}
return (
<div className="appt-modal-overlay" onClick={onClose}>
<div className="appt-modal" onClick={(e) => e.stopPropagation()}>
<h3 className="profile-pet-form-title">Add a New Pet</h3>
{petError && <div className="appt-error">{petError}</div>}
<form onSubmit={handleSubmit}>
<label className="appt-label">
Name
<input
className="appt-input"
type="text"
value={petName}
onChange={(e) => setPetName(e.target.value)}
required
maxLength={50}
/>
</label>
<label className="appt-label">
Species
<input
className="appt-input"
type="text"
value={species}
onChange={(e) => setSpecies(e.target.value)}
required
maxLength={50}
placeholder="e.g. Dog, Cat, Bird"
/>
</label>
<label className="appt-label">
Breed (optional)
<input
className="appt-input"
type="text"
value={breed}
onChange={(e) => setBreed(e.target.value)}
maxLength={50}
placeholder="e.g. Golden Retriever"
/>
</label>
<div className="profile-pet-form-actions">
<button type="submit" className="appt-submit-btn" disabled={submitting}>
{submitting ? "Saving..." : "Add Pet"}
</button>
<button type="button" className="profile-pet-cancel-btn" onClick={onClose}>
Cancel
</button>
</div>
</form>
</div>
</div>
);
}
function AppointmentsPage() {
const { user, token, loading: authLoading } = useAuth();
const router = useRouter();
@@ -224,6 +318,8 @@ function AppointmentsPage() {
const [appointments, setAppointments] = useState([]);
const [loadingAppointments, setLoadingAppointments] = useState(false);
const [showAddPetModal, setShowAddPetModal] = useState(false);
const canBookAppointments = user?.role === "CUSTOMER" || user?.role === "ADMIN";
useEffect(() => {
@@ -234,6 +330,16 @@ const canBookAppointments = user?.role === "CUSTOMER" || user?.role === "ADMIN";
}, [authLoading, user, router, preselectedPetId]);
const loadCustomerPets = useCallback(() => {
if (!token || !canBookAppointments) return;
fetch(`${API_BASE}/api/v1/my-pets`, {
headers: { Authorization: `Bearer ${token}` },
})
.then((r) => r.json())
.then((data) => setCustomerPets(Array.isArray(data) ? data : []))
.catch(() => {});
}, [token, canBookAppointments]);
useEffect(() => {
if (!token) {
return;
@@ -256,15 +362,8 @@ const canBookAppointments = user?.role === "CUSTOMER" || user?.role === "ADMIN";
.then((data) => setAllPets(data.content ?? []))
.catch(() => {});
if (canBookAppointments) {
fetch(`${API_BASE}/api/v1/my-pets`, {
headers: { Authorization: `Bearer ${token}` },
})
.then((r) => r.json())
.then((data) => setCustomerPets(Array.isArray(data) ? data : []))
.catch(() => {});
}
}, [token, canBookAppointments]);
loadCustomerPets();
}, [token, loadCustomerPets]);
useEffect(() => {
if (didPreselectRef.current) {
@@ -482,10 +581,18 @@ const canBookAppointments = user?.role === "CUSTOMER" || user?.role === "ADMIN";
const petSectionLabel = isAdoptionService ? "Select a Pet to Adopt" : "Select Pet(s)";
const noPetsMessage = isAdoptionService
? "No pets are currently available for adoption."
: "No pets found. Please add your pets in your profile before booking.";
: "No pets found on your profile.";
return (
<main className="appt-page">
{showAddPetModal && (
<AddPetModal
token={token}
onClose={() => setShowAddPetModal(false)}
onAdded={loadCustomerPets}
/>
)}
<section className="appt-hero">
<h1 className="appt-hero-title">Schedule an Appointment</h1>
<p className="appt-hero-subtitle">Book a service for your pet or schedule a pet adoption visit</p>
@@ -589,6 +696,15 @@ const canBookAppointments = user?.role === "CUSTOMER" || user?.role === "ADMIN";
{serviceId && (
<div className="appt-label">
<span>{petSectionLabel}</span>
{isCustomerPetService && (
<button
type="button"
className="appt-add-pet-btn"
onClick={() => setShowAddPetModal(true)}
>
+ Add New Pet
</button>
)}
{petsToShow.length === 0 ? (
<p className="appt-no-slots">{noPetsMessage}</p>
) : isAdoptionService ? (

View File

@@ -1351,6 +1351,44 @@ body {
accent-color: orange;
}
.appt-add-pet-btn {
display: inline-block;
margin-top: 0.5rem;
padding: 0.4rem 0.85rem;
background: none;
border: 1.5px solid orange;
border-radius: 6px;
color: orange;
font-size: 0.875rem;
font-weight: 600;
cursor: pointer;
transition: background 0.15s, color 0.15s;
}
.appt-add-pet-btn:hover {
background: orange;
color: white;
}
.appt-modal-overlay {
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.45);
display: flex;
align-items: center;
justify-content: center;
z-index: 1000;
}
.appt-modal {
background: white;
border-radius: 12px;
padding: 2rem;
width: 100%;
max-width: 420px;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.18);
}
.appt-link {
color: orange;
font-weight: 600;