Skip to content

Commit

Permalink
fix: use iterator to load events from event-query
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinbarron committed Jul 12, 2023
1 parent 764bf12 commit cfaca97
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
import { GraphPageIterator, IGraph, prepScopes } from '@microsoft/mgt-element';
import * as MicrosoftGraph from '@microsoft/microsoft-graph-types';

export const getEventsQueryPageIterator = async (
graph: IGraph,
query: string,
scopes = 'calendars.read'
): Promise<GraphPageIterator<MicrosoftGraph.Event>> => {
const request = graph.api(query).middlewareOptions(prepScopes(scopes)).orderby('start/dateTime');

return GraphPageIterator.create<MicrosoftGraph.Event>(graph, request);
};

/**
* returns Calender events iterator associated with either the logged in user or a specific groupId
*
Expand All @@ -24,8 +34,6 @@ export const getEventsPageIterator = async (
endDateTime: Date,
groupId?: string
): Promise<GraphPageIterator<MicrosoftGraph.Event>> => {
const scopes = 'calendars.read';

const sdt = `startdatetime=${startDateTime.toISOString()}`;
const edt = `enddatetime=${endDateTime.toISOString()}`;

Expand All @@ -39,7 +47,5 @@ export const getEventsPageIterator = async (

uri += `/calendarview?${sdt}&${edt}`;

const request = graph.api(uri).middlewareOptions(prepScopes(scopes)).orderby('start/dateTime');

return GraphPageIterator.create<MicrosoftGraph.Event>(graph, request);
return getEventsQueryPageIterator(graph, uri);
};
19 changes: 8 additions & 11 deletions packages/mgt-components/src/components/mgt-agenda/mgt-agenda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
import '../../styles/style-helper';
import '../mgt-person/mgt-person';
import { styles } from './mgt-agenda-css';
import { getEventsPageIterator } from './mgt-agenda.graph';
import { getEventsPageIterator, getEventsQueryPageIterator } from './mgt-agenda.graph';
import { SvgIcon, getSvg } from '../../utils/SvgHelper';
import { MgtPeople } from '../mgt-people/mgt-people';
import { registerFluentComponents } from '../../utils/FluentComponents';
Expand Down Expand Up @@ -629,17 +629,14 @@ export class MgtAgenda extends MgtTemplatedComponent {
} else {
query = this.eventQuery;
}
const iterator = await getEventsQueryPageIterator(graph, query, scope);
if (iterator?.value) {
events = iterator.value;

let request = graph.api(query);

if (scope) {
request = request.middlewareOptions(prepScopes(scope));
}

const results = (await request.get()) as CollectionResponse<MicrosoftGraph.Event>;

if (results?.value) {
events = results.value;
while (iterator.hasNext) {
await iterator.next();
events = iterator.value;
}
}
// eslint-disable-next-line no-empty
} catch (e) {}
Expand Down
2 changes: 1 addition & 1 deletion stories/components/agenda/agenda.properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default {
};

export const getByEventQuery = () => html`
<mgt-agenda event-query="/me/events?orderby=start/dateTime"></mgt-agenda>
<mgt-agenda event-query="/me/calendarview?$orderby=start/dateTime&startdatetime=2023-07-12T00:00:00.000Z&enddatetime=2023-07-18T00:00:00.000Z"></mgt-agenda>
`;

export const groupByDay = () => html`
Expand Down

0 comments on commit cfaca97

Please sign in to comment.