fix(pets): persist supplement consumptions array in usePetStore and update target pet on checkout
This commit is contained in:
1 parent
5be2b9693d
commit
a0145ffd9a
4 files changed
+68
-44
No files matched your search
@@ -125,8 +125,9 @@ export default function CheckoutPage() {
|
||||
await fetchProfile();
|
||||
|
||||
// 4. Update pet consumptions and charity contributions for active pet
|
||||
if (activePet) {
|
||||
const newConsumptions = [...(activePet.consumptions || [])];
|
||||
const targetPet = activePet || usePetStore.getState().pets[0];
|
||||
if (targetPet) {
|
||||
const newConsumptions = [...(targetPet.consumptions || [])];
|
||||
|
||||
items.forEach(item => {
|
||||
const existing = newConsumptions.find(c => c.productId === item.product.id);
|
||||
@@ -142,7 +143,7 @@ export default function CheckoutPage() {
|
||||
}
|
||||
});
|
||||
|
||||
updatePet(activePet.id, {
|
||||
updatePet(targetPet.id, {
|
||||
consumptions: newConsumptions
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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) });
|
||||
},
|
||||
}),
|
||||
|
||||
Reference in new issue
Block a user