Persist pet profile changes via PATCH /api/portal/pets/{petId}
- handlePetSave is now async; calls PATCH before updating local state - API response used as source of truth for local state update - Error state shown on API failure; edit form NOT cleared on failure - Loading/saving indicator in PetForm while API call in flight Refs: GRO-1470 Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
@@ -83,9 +83,27 @@ export function PetProfiles({ sessionId, readOnly }: Props) {
|
||||
const petHistory = appointments.appointments.filter(a => a.pet?.id === selectedPetId && new Date(a.startTime) <= new Date());
|
||||
const editingPet = editingPetId ? pets.find(p => p.id === editingPetId) ?? null : null;
|
||||
|
||||
function handlePetSave(updatedPet: Pet) {
|
||||
setPets(prev => prev.map(p => p.id === updatedPet.id ? updatedPet : p));
|
||||
setEditingPetId(null);
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [saveError, setSaveError] = useState<string | null>(null);
|
||||
|
||||
async function handlePetSave(updatedPet: Pet) {
|
||||
setSaving(true);
|
||||
setSaveError(null);
|
||||
try {
|
||||
const res = await fetch(`/api/portal/pets/${updatedPet.id}`, {
|
||||
method: "PATCH",
|
||||
headers: { "Content-Type": "application/json", ...buildHeaders(sessionId) },
|
||||
body: JSON.stringify(updatedPet),
|
||||
});
|
||||
if (!res.ok) throw new Error("Failed to save pet");
|
||||
const saved: Pet = await res.json();
|
||||
setPets(prev => prev.map(p => p.id === saved.id ? saved : p));
|
||||
setEditingPetId(null);
|
||||
} catch (e) {
|
||||
setSaveError(e instanceof Error ? e.message : "Failed to save pet");
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
}
|
||||
|
||||
if (editingPet) {
|
||||
@@ -94,6 +112,8 @@ export function PetProfiles({ sessionId, readOnly }: Props) {
|
||||
pet={editingPet}
|
||||
onSave={handlePetSave}
|
||||
onCancel={() => setEditingPetId(null)}
|
||||
saving={saving}
|
||||
saveError={saveError}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user