Skip to content

Commit

Permalink
Merge pull request #45 from cabcookie/fix-fetching-meetings
Browse files Browse the repository at this point in the history
  • Loading branch information
cabcookie authored Apr 29, 2024
2 parents 321fd6a + 9842637 commit af867e6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
2 changes: 1 addition & 1 deletion api/ContextProjects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export type Project = {
onHoldTill?: Date;
myNextActions: string;
othersNextActions: string;
context?: Context;
context: Context;
accountIds: string[];
activityIds: string[];
};
Expand Down
35 changes: 29 additions & 6 deletions api/useMeetings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<MeetingData[] | undefined>;

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: [
Expand Down Expand Up @@ -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());
};

Expand Down

0 comments on commit af867e6

Please sign in to comment.