Skip to content

Commit 3790b84

Browse files
committed
my upcoming events shows only single instances
1 parent e081854 commit 3790b84

2 files changed

Lines changed: 33 additions & 4 deletions

File tree

src/frontend/src/pages/CalendarPage/CalendarPage.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ import FilterModal from './FilterModal';
3030
import { DateCalendar } from '@mui/x-date-pickers';
3131
import { useCurrentUser } from '../../hooks/users.hooks';
3232
import { useGetUsersTeams } from '../../hooks/teams.hooks';
33-
import { convertIntToDay, eventsToEventInstances, getOverlapTime } from '../../utils/calendar.utils';
33+
import {
34+
convertIntToDay,
35+
eventsToEventInstances,
36+
eventsToNextEventInstance,
37+
getOverlapTime
38+
} from '../../utils/calendar.utils';
3439
import { filterEventTransformer } from '../../apis/transformers/calendar.transformer';
3540
import WarningIcon from '@mui/icons-material/Warning';
3641
import { useHistory } from 'react-router-dom';
@@ -233,9 +238,9 @@ const NewCalendarPage: React.FC<NewCalendarPageProps> = ({
233238
teamIds: teamList
234239
});
235240

236-
const upcomingOccurences = eventsToEventInstances(upcomingEvents ?? [])
237-
.filter((event) => new Date(event.startTime) >= new Date())
238-
.sort((a, b) => new Date(a.startTime).getTime() - new Date(b.startTime).getTime());
241+
const upcomingOccurences = eventsToNextEventInstance(upcomingEvents ?? []).sort(
242+
(a, b) => new Date(a.startTime).getTime() - new Date(b.startTime).getTime()
243+
);
239244

240245
const toggleCalendar = (calendarId: string) => {
241246
setSelectedCalendarIds((prev) =>

src/frontend/src/utils/calendar.utils.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,30 @@ export const eventsToEventInstances = (events: Event[]): EventInstance[] => {
169169
});
170170
};
171171

172+
// converts events to event instances, but only the next event instance for each event
173+
// If an event has no times in the future it will not be included in the result
174+
// if an event has multiple times in the future it will only include the next schedule slot
175+
export const eventsToNextEventInstance = (events: Event[]): EventInstance[] => {
176+
const now = new Date();
177+
178+
const eventsWithSlotInFuture = events.filter((event) => {
179+
return event.scheduledTimes.some((scheduleSlot) => scheduleSlot.endTime > now);
180+
});
181+
182+
// For each event, find the next schedule slot in the future
183+
const eventsWithOnlyNextSlot = eventsWithSlotInFuture.map((event) => ({
184+
...event,
185+
scheduledTimes: [
186+
event.scheduledTimes.reduce((acc, current) => {
187+
if (current.startTime < acc.startTime) return acc;
188+
return current;
189+
})
190+
]
191+
}));
192+
193+
return eventsToEventInstances(eventsWithOnlyNextSlot);
194+
};
195+
172196
// converts an Event into Event Form Values
173197
// Note: Because users can only edit a single instaces time, editModal is always populated with an event instance
174198
// representing a single occurrence of the event. However, event edits will effect the entire series for all values

0 commit comments

Comments
 (0)