Skip to content

Commit 18c07d6

Browse files
committed
Changed Graph code to get a calendar view instead of list of events
1 parent 4818005 commit 18c07d6

6 files changed

Lines changed: 73 additions & 25 deletions

File tree

demo/graph-tutorial/package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/graph-tutorial/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
"@types/reactstrap": "^8.5.0",
1818
"bootstrap": "^4.5.0",
1919
"moment": "^2.27.0",
20+
"moment-timezone": "^0.5.31",
2021
"msal": "^1.3.2",
2122
"react": "^16.13.1",
2223
"react-dom": "^16.13.1",
2324
"react-router-dom": "^5.2.0",
2425
"react-scripts": "3.4.1",
2526
"reactstrap": "^8.5.1",
26-
"typescript": "~3.7.2"
27+
"typescript": "~3.7.2",
28+
"windows-iana": "^4.2.0"
2729
},
2830
"scripts": {
2931
"start": "react-scripts start",

demo/graph-tutorial/src/Calendar.tsx

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,66 @@
22
// Licensed under the MIT License.
33
import React from 'react';
44
import { Table } from 'reactstrap';
5-
import moment from 'moment';
5+
import moment from 'moment-timezone';
6+
import { findOneIana } from "windows-iana";
67
import { Event } from 'microsoft-graph';
78
import { config } from './Config';
8-
import { getEvents } from './GraphService';
9+
import { getUserWeekCalendar } from './GraphService';
910
import withAuthProvider, { AuthComponentProps } from './AuthProvider';
1011

1112
interface CalendarState {
13+
eventsLoaded: boolean;
1214
events: Event[];
1315
}
1416

17+
export interface CalendarProps extends AuthComponentProps {
18+
timeZone: any;
19+
timeFormat: any;
20+
}
21+
1522
// Helper function to format Graph date/time
1623
function formatDateTime(dateTime: string | undefined) {
1724
if (dateTime !== undefined) {
18-
return moment.utc(dateTime).local().format('M/D/YY h:mm A');
25+
return moment(dateTime).format('M/D/YY h:mm A');
1926
}
2027
}
2128

2229
class Calendar extends React.Component<AuthComponentProps, CalendarState> {
2330
constructor(props: any) {
2431
super(props);
25-
32+
console.log('Constructor: ' + JSON.stringify(props.user));
2633
this.state = {
34+
eventsLoaded: false,
2735
events: []
2836
};
2937
}
3038

31-
async componentDidMount() {
32-
try {
33-
// Get the user's access token
34-
var accessToken = await this.props.getAccessToken(config.scopes);
35-
// Get the user's events
36-
var events = await getEvents(accessToken);
37-
// Update the array of events in state
38-
this.setState({ events: events.value });
39-
}
40-
catch (err) {
41-
this.props.setError('ERROR', JSON.stringify(err));
39+
async componentDidUpdate()
40+
{
41+
if (this.props.user && !this.state.eventsLoaded)
42+
{
43+
try {
44+
// Get the user's access token
45+
var accessToken = await this.props.getAccessToken(config.scopes);
46+
47+
// Convert user's Windows time zone ("Pacific Standard Time")
48+
// to IANA format ("America/Los_Angeles")
49+
// Moment needs IANA format
50+
var ianaTimeZone = findOneIana(this.props.user.timeZone);
51+
52+
// Get midnight on the start of the current week in the user's timezone,
53+
// but in UTC. For example, for Pacific Standard Time, the time value would be
54+
// 07:00:00Z
55+
var startOfWeek = moment.tz(ianaTimeZone!.valueOf()).startOf('week').utc();
56+
console.log(`Start of week: ${startOfWeek}`);
57+
// Get the user's events
58+
var events = await getUserWeekCalendar(accessToken, this.props.user.timeZone, startOfWeek);
59+
// Update the array of events in state
60+
this.setState({ eventsLoaded: true, events: events.value });
61+
}
62+
catch (err) {
63+
this.props.setError('ERROR', JSON.stringify(err));
64+
}
4265
}
4366
}
4467

demo/graph-tutorial/src/GraphService.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
import { Moment } from "moment";
5+
46
// <graphServiceSnippet1>
57
var graph = require('@microsoft/microsoft-graph-client');
68

@@ -25,21 +27,28 @@ export async function getUserDetails(accessToken: string) {
2527
.select('displayName,mail,mailboxSettings,userPrincipalName')
2628
.get();
2729

28-
console.log(JSON.stringify(user));
2930
return user;
3031
}
3132
// </graphServiceSnippet1>
3233

33-
// <getEventsSnippet>
34-
export async function getEvents(accessToken: string) {
34+
// <getUserWeekCalendarSnippet>
35+
export async function getUserWeekCalendar(accessToken: string, timeZone: string, startDate: Moment) {
3536
const client = getAuthenticatedClient(accessToken);
3637

38+
// Generate startDateTime and endDateTime query params
39+
// to display a 7-day window
40+
var startDateTime = startDate.format();
41+
var endDateTime = startDate.add(7, 'day').format();
42+
3743
const events = await client
38-
.api('/me/events')
44+
.api('/me/calendarview')
45+
.header("Prefer", `outlook.timezone="${timeZone}"`)
46+
.query({ startDateTime: startDateTime, endDateTime: endDateTime })
3947
.select('subject,organizer,start,end')
40-
.orderby('createdDateTime DESC')
48+
.orderby('start/dateTime')
49+
.top(50)
4150
.get();
4251

4352
return events;
4453
}
45-
// </getEventsSnippet>
54+
// </getUserWeekCalendarSnippet>

tutorial/02-create-app.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ Before moving on, install some additional packages that you will use later:
2525
- [reactstrap](https://github.com/reactstrap/reactstrap) for React components based on Bootstrap.
2626
- [fontawesome-free](https://github.com/FortAwesome/Font-Awesome) for icons.
2727
- [moment](https://github.com/moment/moment) for formatting dates and times.
28+
- [windows-iana](https://github.com/rubenillodo/windows-iana) for translating Windows time zones to IANA format.
2829
- [msal](https://github.com/AzureAD/microsoft-authentication-library-for-js) for authenticating to Azure Active Directory and retrieving access tokens.
2930
- [microsoft-graph-client](https://github.com/microsoftgraph/msgraph-sdk-javascript) for making calls to Microsoft Graph.
3031
3132
Run the following command in your CLI.
3233
3334
```Shell
34-
npm install react-router-dom@5.2.0 @types/react-router-dom@5.1.5 bootstrap@4.5.0 reactstrap@8.5.1 @types/reactstrap@8.5.0
35-
npm install @fortawesome/fontawesome-free@5.13.1 moment@2.27.0 msal@1.3.2 @microsoft/microsoft-graph-client@2.0.0 @types/microsoft-graph@1.13.0
35+
npm install react-router-dom@5.2.0 @types/react-router-dom@5.1.5 bootstrap@4.5.0 reactstrap@8.5.1 @types/reactstrap@8.5.0 @fortawesome/fontawesome-free@5.13.1
36+
npm install moment@2.27.0 moment-timezone@0.5.31 windows-iana@4.2.0 msal@1.3.2 @microsoft/microsoft-graph-client@2.0.0 @types/microsoft-graph@1.13.0
3637
```
3738
3839
## Design the app

tutorial/05-add-ms-graph.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ In this exercise you will incorporate the Microsoft Graph into the application.
66

77
1. Open `./src/GraphService.ts` and add the following function.
88

9-
:::code language="typescript" source="../demo/graph-tutorial/src/GraphService.ts" id="getEventsSnippet":::
9+
:::code language="typescript" source="../demo/graph-tutorial/src/GraphService.ts" id="getUserWeekCalendarSnippet":::
1010

1111
Consider what this code is doing.
1212

0 commit comments

Comments
 (0)