|
| 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 | +} |
0 commit comments