fix(pets): persist supplement consumptions array in usePetStore and update target pet on checkout

This commit is contained in:
parsa aghaei committed 2026-07-26 20:44:55 +03:30
1 parent 5be2b9693d
commit a0145ffd9a
4 files changed
+68 -44

No files matched your search

+36 -32
View File
@@ -135,6 +135,10 @@ export const usePetStore = create<PetStore>()(
return pets.find((p) => p.id === activePetId) || null;
},
updatePet: async (id, updates) => {
set((state) => ({
pets: state.pets.map((p) => (p.id === id ? { ...p, ...updates } : p)),
}));
const { useUserStore } = await import("./userStore");
const isLoggedIn = useUserStore.getState().isLoggedIn;
@@ -154,10 +158,6 @@ export const usePetStore = create<PetStore>()(
console.error("Failed to update pet via backend API:", error);
toast.error("خطا در ویرایش اطلاعات پت در سرور");
}
} else {
set((state) => ({
pets: state.pets.map((p) => (p.id === id ? { ...p, ...updates } : p)),
}));
}
},
addReminder: async (petId, reminder) => {
@@ -191,34 +191,38 @@ export const usePetStore = create<PetStore>()(
}
},
setPets: (backendPets) => {
const mappedPets: PetProfile[] = backendPets.map(bp => ({
id: bp.id,
name: bp.name,
type: bp.type as any,
breed: bp.breed,
age: Number(bp.age),
weight: Number(bp.weight),
activityLevel: bp.activityLevel as any,
medicalConditions: bp.medicalConditions?.map((mc: any) => mc.condition) || [],
image: bp.imageUrl || undefined,
reminders: bp.reminders?.map((r: any) => ({
id: r.id,
title: r.title,
time: r.time,
frequency: r.frequency as any,
productId: r.productId,
completedDates: r.completions?.map((c: any) => c.completedDate) || []
})) || [],
logs: bp.healthLogs?.map((hl: any) => ({
id: hl.id,
date: hl.loggedDate,
appetite: hl.appetite as any,
energy: hl.energy as any,
digestion: hl.digestion as any,
note: hl.note || undefined
})) || [],
consumptions: []
}));
const currentLocalPets = get().pets;
const mappedPets: PetProfile[] = backendPets.map(bp => {
const existingLocal = currentLocalPets.find(lp => lp.id === bp.id);
return {
id: bp.id,
name: bp.name,
type: bp.type as any,
breed: bp.breed,
age: Number(bp.age),
weight: Number(bp.weight),
activityLevel: bp.activityLevel as any,
medicalConditions: bp.medicalConditions?.map((mc: any) => mc.condition) || [],
image: bp.imageUrl || undefined,
reminders: bp.reminders?.map((r: any) => ({
id: r.id,
title: r.title,
time: r.time,
frequency: r.frequency as any,
productId: r.productId,
completedDates: r.completions?.map((c: any) => c.completedDate) || []
})) || [],
logs: bp.healthLogs?.map((hl: any) => ({
id: hl.id,
date: hl.loggedDate,
appetite: hl.appetite as any,
energy: hl.energy as any,
digestion: hl.digestion as any,
note: hl.note || undefined
})) || [],
consumptions: existingLocal?.consumptions || []
};
});
set({ pets: mappedPets, activePetId: get().activePetId && mappedPets.some(p => p.id === get().activePetId) ? get().activePetId : (mappedPets[0]?.id || null) });
},
}),