From 9842637be192305b7caf925abf8f2783bbe752bd Mon Sep 17 00:00:00 2001 From: Carsten Koch Date: Mon, 29 Apr 2024 19:18:32 +0200 Subject: [PATCH] fix: load all meeting --- api/ContextProjects.tsx | 2 +- api/useMeetings.ts | 35 +++++++++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/api/ContextProjects.tsx b/api/ContextProjects.tsx index 4858036e1..d0f176449 100644 --- a/api/ContextProjects.tsx +++ b/api/ContextProjects.tsx @@ -53,7 +53,7 @@ export type Project = { onHoldTill?: Date; myNextActions: string; othersNextActions: string; - context?: Context; + context: Context; accountIds: string[]; activityIds: string[]; }; diff --git a/api/useMeetings.ts b/api/useMeetings.ts index 4e6f72ac7..2e5819007 100644 --- a/api/useMeetings.ts +++ b/api/useMeetings.ts @@ -54,15 +54,24 @@ export const mapMeeting: (data: MeetingData) => Meeting = ({ .map(({ id }) => id), }); -const fetchMeetings = (page: number, context?: Context) => async () => { +type FetchMeetingsWithTokenFunction = (props: { + page: number; + token?: string; + context: Context; +}) => Promise; + +const fetchMeetingsWithToken: FetchMeetingsWithTokenFunction = async ({ + page, + token, + context, +}) => { if (!context) return; const toDate = flow( addDaysToDate(-4 * (page - 1) * 7 + 1), getDayOfDate )(new Date()); const fromDate = flow(addDaysToDate(-4 * page * 7), getDayOfDate)(new Date()); - const { data, errors } = await client.models.Meeting.list({ - filter: { + const filter = { and: [ { or: [ @@ -93,12 +102,26 @@ const fetchMeetings = (page: number, context?: Context) => async () => { ], }, ], - }, + }; + + const { data, errors, nextToken } = await client.models.Meeting.list({ + filter, selectionSet: meetingSelectionSet, + nextToken: token, }); if (errors) throw errors; - return data - .map(mapMeeting) + if (!nextToken) return data; + return [ + ...data, + ...((await fetchMeetingsWithToken({ page, token: nextToken, context })) || + []), + ]; +}; + +const fetchMeetings = (page: number, context?: Context) => async () => { + if (!context) return; + return (await fetchMeetingsWithToken({ page, context })) + ?.map(mapMeeting) .sort((a, b) => b.meetingOn.getTime() - a.meetingOn.getTime()); };