From e31f89e5589c38b209431aefa2cde11db1373ff9 Mon Sep 17 00:00:00 2001 From: cjeongmin Date: Sun, 29 Sep 2024 20:45:44 +0900 Subject: [PATCH] fix: resolve errors when creating tours when using the fillActivities feature --- src/features/trip/trip.slice.ts | 34 +++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/features/trip/trip.slice.ts b/src/features/trip/trip.slice.ts index 4377518..b542403 100644 --- a/src/features/trip/trip.slice.ts +++ b/src/features/trip/trip.slice.ts @@ -43,6 +43,7 @@ export interface TripAction { setRecommendContent: (content: string) => void; setRecommendedItems: (items: TripItem[]) => void; addTour: () => void; + removeTour: (day: number) => void; addActivity: (day: number, activity: Activity) => void; removeActivity: (day: number, activity: Activity) => void; fillActivities: (items: Activity[]) => void; @@ -112,6 +113,15 @@ export const useTripStore = create((set, get) => ({ addTour: () => set((prev) => ({ ...prev, activities: [...prev.activities, []] })), + removeTour: (day) => + set((prev) => ({ + ...prev, + activities: [ + ...prev.activities.slice(0, day - 1), + ...prev.activities.slice(day), + ], + })), + addActivity: (day, activity) => set((prev) => ({ ...prev, @@ -144,22 +154,22 @@ export const useTripStore = create((set, get) => ({ const newActivities = [...activities]; - for (const activity of items) { + items.forEach((activity) => { const targetTime = activity.dayTime; let flag = false; - for (const acts of newActivities) { - if (acts.find((act) => act.dayTime === targetTime)) continue; - acts.push(activity); + newActivities.some((acts, index) => { + if (acts.find((act) => act.dayTime === targetTime)) return false; + acts.push({ ...activity, dayNumber: index + 1 }); acts.sort((a, b) => TIME_ORDER[a.dayTime] - TIME_ORDER[b.dayTime]); flag = true; - break; - } + return true; + }); - if (flag) continue; + if (flag) return; - for (const acts of newActivities) { - if (acts.length === 4) continue; + newActivities.forEach((acts, index) => { + if (acts.length === 4) return; const remaining = acts.reduce< ('MORNING' | 'AFTERNOON' | 'EVENING' | 'NIGHT')[] @@ -170,11 +180,11 @@ export const useTripStore = create((set, get) => ({ const time = remaining.shift(); if (time) { - acts.push({ ...activity, dayTime: time }); + acts.push({ ...activity, dayTime: time, dayNumber: index + 1 }); acts.sort((a, b) => TIME_ORDER[a.dayTime] - TIME_ORDER[b.dayTime]); } - } - } + }); + }); return { ...prev, activities: newActivities }; }),