Skip to content

Commit e1b0ee5

Browse files
committed
Completed add MS Graph step
1 parent 7d9ef67 commit e1b0ee5

File tree

7 files changed

+207
-119
lines changed

7 files changed

+207
-119
lines changed

demo/graph-tutorial/package-lock.json

Lines changed: 5 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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"@testing-library/react": "^9.3.2",
1010
"@testing-library/user-event": "^7.1.2",
1111
"@types/jest": "^24.0.0",
12+
"@types/microsoft-graph": "^1.12.0",
1213
"@types/node": "^12.0.0",
1314
"@types/react": "^16.9.0",
1415
"@types/react-dom": "^16.9.0",

demo/graph-tutorial/src/App.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { UserAgentApplication } from 'msal';
77
import NavBar from './NavBar';
88
import ErrorMessage from './ErrorMessage';
99
import Welcome from './Welcome';
10+
import Calendar from './Calendar';
1011
import { config } from './Config';
1112
import { getUserDetails } from './GraphService';
1213
import 'bootstrap/dist/css/bootstrap.css';
@@ -30,7 +31,7 @@ class App extends Component<any, AppState> {
3031
redirectUri: config.redirectUri
3132
},
3233
cache: {
33-
cacheLocation: "localStorage",
34+
cacheLocation: "sessionStorage",
3435
storeAuthStateInCookie: true
3536
}
3637
});
@@ -73,6 +74,11 @@ class App extends Component<any, AppState> {
7374
user={this.state.user}
7475
authButtonMethod={this.login.bind(this)} />
7576
} />
77+
<Route exact path="/calendar"
78+
render={(props) =>
79+
<Calendar {...props}
80+
showError={this.setErrorMessage.bind(this)} />
81+
} />
7682
</Container>
7783
</div>
7884
</Router>
@@ -126,6 +132,7 @@ class App extends Component<any, AppState> {
126132
}
127133
// </logoutSnippet>
128134

135+
// <getUserProfileSnippet>
129136
async getUserProfile() {
130137
try {
131138
// Get the access token silently
@@ -171,6 +178,7 @@ class App extends Component<any, AppState> {
171178
});
172179
}
173180
}
181+
// </getUserProfileSnippet>
174182
}
175183

176184
export default App;
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import React from 'react';
2+
import { Table } from 'reactstrap';
3+
import moment from 'moment';
4+
import { UserAgentApplication } from 'msal';
5+
import { Event } from 'microsoft-graph';
6+
import { config } from './Config';
7+
import { getEvents } from './GraphService';
8+
9+
interface CalendarProps {
10+
showError: any;
11+
}
12+
13+
interface CalendarState {
14+
events: Event[];
15+
}
16+
17+
// Helper function to format Graph date/time
18+
function formatDateTime(dateTime: string | undefined) {
19+
if (dateTime !== undefined) {
20+
return moment.utc(dateTime).local().format('M/D/YY h:mm A');
21+
}
22+
}
23+
24+
export default class Calendar extends React.Component<CalendarProps, CalendarState> {
25+
constructor(props: any) {
26+
super(props);
27+
28+
this.state = {
29+
events: []
30+
};
31+
}
32+
33+
async componentDidMount() {
34+
try {
35+
// Get the user's access token
36+
const msal = window.msal as UserAgentApplication;
37+
38+
var accessToken = await msal.acquireTokenSilent({
39+
scopes: config.scopes
40+
});
41+
// Get the user's events
42+
var events = await getEvents(accessToken.accessToken);
43+
// Update the array of events in state
44+
this.setState({events: events.value});
45+
}
46+
catch(err) {
47+
this.props.showError('ERROR', JSON.stringify(err));
48+
}
49+
}
50+
51+
// <renderSnippet>
52+
render() {
53+
return (
54+
<div>
55+
<h1>Calendar</h1>
56+
<Table>
57+
<thead>
58+
<tr>
59+
<th scope="col">Organizer</th>
60+
<th scope="col">Subject</th>
61+
<th scope="col">Start</th>
62+
<th scope="col">End</th>
63+
</tr>
64+
</thead>
65+
<tbody>
66+
{this.state.events.map(
67+
function(event: Event){
68+
return(
69+
<tr key={event.id}>
70+
<td>{event.organizer?.emailAddress?.name}</td>
71+
<td>{event.subject}</td>
72+
<td>{formatDateTime(event.start?.dateTime)}</td>
73+
<td>{formatDateTime(event.end?.dateTime)}</td>
74+
</tr>
75+
);
76+
})}
77+
</tbody>
78+
</Table>
79+
</div>
80+
);
81+
}
82+
// </renderSnippet>
83+
}

demo/graph-tutorial/src/GraphService.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,18 @@ export async function getUserDetails(accessToken: string) {
2323
const user = await client.api('/me').get();
2424
return user;
2525
}
26-
// </graphServiceSnippet1>
26+
// </graphServiceSnippet1>
27+
28+
// <getEventsSnippet>
29+
export async function getEvents(accessToken: string) {
30+
const client = getAuthenticatedClient(accessToken);
31+
32+
const events = await client
33+
.api('/me/events')
34+
.select('subject,organizer,start,end')
35+
.orderby('createdDateTime DESC')
36+
.get();
37+
38+
return events;
39+
}
40+
// </getEventsSnippet>

tutorial/02-create-app.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Run the following command in your CLI.
3232
3333
```Shell
3434
35-
35+
3636
```
3737
3838
## Design the app

0 commit comments

Comments
 (0)