Skip to content

Commit

Permalink
fix: resolve errors when creating tours when using the fillActivities…
Browse files Browse the repository at this point in the history
… feature
  • Loading branch information
cjeongmin committed Sep 29, 2024
1 parent cc8e624 commit e31f89e
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions src/features/trip/trip.slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -112,6 +113,15 @@ export const useTripStore = create<TripState & TripAction>((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,
Expand Down Expand Up @@ -144,22 +154,22 @@ export const useTripStore = create<TripState & TripAction>((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')[]
Expand All @@ -170,11 +180,11 @@ export const useTripStore = create<TripState & TripAction>((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 };
}),
Expand Down

0 comments on commit e31f89e

Please sign in to comment.