|
2 | 2 | // Licensed under the MIT License. |
3 | 3 | import React from 'react'; |
4 | 4 | import { Table } from 'reactstrap'; |
5 | | -import moment from 'moment'; |
| 5 | +import moment from 'moment-timezone'; |
| 6 | +import { findOneIana } from "windows-iana"; |
6 | 7 | import { Event } from 'microsoft-graph'; |
7 | 8 | import { config } from './Config'; |
8 | | -import { getEvents } from './GraphService'; |
| 9 | +import { getUserWeekCalendar } from './GraphService'; |
9 | 10 | import withAuthProvider, { AuthComponentProps } from './AuthProvider'; |
10 | 11 |
|
11 | 12 | interface CalendarState { |
| 13 | + eventsLoaded: boolean; |
12 | 14 | events: Event[]; |
13 | 15 | } |
14 | 16 |
|
| 17 | +export interface CalendarProps extends AuthComponentProps { |
| 18 | + timeZone: any; |
| 19 | + timeFormat: any; |
| 20 | +} |
| 21 | + |
15 | 22 | // Helper function to format Graph date/time |
16 | 23 | function formatDateTime(dateTime: string | undefined) { |
17 | 24 | 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'); |
19 | 26 | } |
20 | 27 | } |
21 | 28 |
|
22 | 29 | class Calendar extends React.Component<AuthComponentProps, CalendarState> { |
23 | 30 | constructor(props: any) { |
24 | 31 | super(props); |
25 | | - |
| 32 | + console.log('Constructor: ' + JSON.stringify(props.user)); |
26 | 33 | this.state = { |
| 34 | + eventsLoaded: false, |
27 | 35 | events: [] |
28 | 36 | }; |
29 | 37 | } |
30 | 38 |
|
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 | + } |
42 | 65 | } |
43 | 66 | } |
44 | 67 |
|
|
0 commit comments