Fixed issue when not all calendars were fetched for some accounts

Signed-off-by: Alex Rybchenko <arybchenko@box.com>
This commit is contained in:
Alex Rybchenko
2022-03-28 15:06:19 +02:00
parent 21a3e09fd5
commit c6616e6fc9
5 changed files with 30 additions and 11 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-gcalendar': patch
---
Fixed issue when not all calendars were fetched for some accounts
+2 -2
View File
@@ -15,7 +15,7 @@
*/
import { OAuthApi, createApiRef, FetchApi } from '@backstage/core-plugin-api';
import { GCalendar, GCalendarEvent } from './types';
import { GCalendarEvent, GCalendarList } from './types';
import { ResponseError } from '@backstage/errors';
type Options = {
@@ -58,7 +58,7 @@ export class GCalendarApiClient {
}
public async getCalendars(params?: any) {
return this.get<{ items: GCalendar[] }>(
return this.get<GCalendarList>(
'/calendar/v3/users/me/calendarList',
params,
);
+2
View File
@@ -17,6 +17,8 @@
/// <reference types="gapi.auth2" />
/// <reference types="gapi.client.calendar" />
export type GCalendarList = gapi.client.calendar.CalendarList;
export type GCalendar = gapi.client.calendar.CalendarListEntry;
export type EventAttendee = gapi.client.calendar.EventAttendee;
@@ -15,7 +15,7 @@
*/
import { sortBy } from 'lodash';
import { DateTime } from 'luxon';
import React, { useEffect, useMemo, useState } from 'react';
import React, { useEffect, useState } from 'react';
import { InfoCard, Progress } from '@backstage/core-components';
import { useAnalytics } from '@backstage/core-plugin-api';
@@ -51,10 +51,10 @@ export const CalendarCard = () => {
signIn(true);
}, [signIn]);
const { isLoading: isCalendarLoading, data } = useCalendarsQuery({
enabled: isSignedIn,
});
const calendars = useMemo(() => data?.items || [], [data]);
const { isLoading: isCalendarLoading, data: calendars = [] } =
useCalendarsQuery({
enabled: isSignedIn,
});
const primaryCalendarId = calendars.find(c => c.primary === true)?.id;
const defaultSelectedCalendars = primaryCalendarId ? [primaryCalendarId] : [];
const [storedCalendars, setStoredCalendars] = useStoredCalendars(
@@ -30,10 +30,22 @@ export const useCalendarsQuery = ({ enabled }: Options) => {
return useQuery(
['calendars'],
async () =>
calendarApi.getCalendars({
minAccessRole: 'reader',
}),
async () => {
const calendars = [];
let token = '';
do {
const { nextPageToken = '', items = [] } =
await calendarApi.getCalendars({
maxResults: 100,
minAccessRole: 'reader',
pageToken: token,
});
token = nextPageToken;
calendars.push(...items);
} while (token);
return calendars;
},
{
enabled,
keepPreviousData: true,