-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathCalendarScreen.tsx
More file actions
166 lines (151 loc) · 3.98 KB
/
CalendarScreen.tsx
File metadata and controls
166 lines (151 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright (c) Microsoft.
// Licensed under the MIT license.
import React from 'react';
import {
ActivityIndicator,
Alert,
FlatList,
Modal,
Platform,
StyleSheet,
Text,
View,
} from 'react-native';
import {createStackNavigator} from '@react-navigation/stack';
import * as MicrosoftGraph from '@microsoft/microsoft-graph-types';
import {endOfWeek, format, parseISO, startOfWeek} from 'date-fns';
import {fromZonedTime} from 'date-fns-tz';
import {findIana} from 'windows-iana';
import {UserContext} from '../UserContext';
import {GraphManager} from '../graph/GraphManager';
const Stack = createStackNavigator();
const CalendarState = React.createContext<CalendarScreenState>({
loadingEvents: true,
events: [],
});
type CalendarScreenState = {
loadingEvents: boolean;
events: MicrosoftGraph.Event[];
};
// Temporary JSON view
const CalendarComponent = () => {
const calendarState = React.useContext(CalendarState);
return (
<View style={styles.container}>
<Modal visible={calendarState.loadingEvents}>
<View style={styles.loading}>
<ActivityIndicator
color={Platform.OS === 'android' ? '#276b80' : undefined}
animating={calendarState.loadingEvents}
size='large'
/>
</View>
</Modal>
<FlatList
data={calendarState.events}
renderItem={({item}) => (
<View style={styles.eventItem}>
<Text style={styles.eventSubject}>{item.subject}</Text>
<Text style={styles.eventOrganizer}>
{item.organizer!.emailAddress!.name}
</Text>
<Text style={styles.eventDuration}>
{convertDateTime(item.start!.dateTime!)} -{' '}
{convertDateTime(item.end!.dateTime!)}
</Text>
</View>
)}
/>
</View>
);
};
const convertDateTime = (dateTime: string): string => {
return format(parseISO(dateTime), 'MMM do h:mm a');
};
export default class CalendarScreen extends React.Component {
static contextType = UserContext;
declare context: React.ContextType<typeof UserContext>;
state: CalendarScreenState = {
loadingEvents: true,
events: [],
};
async componentDidMount() {
try {
const tz = this.context.userTimeZone || 'UTC';
// Convert user's Windows time zone ("Pacific Standard Time")
// to IANA format ("America/Los_Angeles")
// Moment.js needs IANA format
const ianaTimeZone = findIana(tz)[0];
// Get midnight on the start of the current week in the user's
// time zone, but in UTC. For example, for PST, the time value
// would be 07:00:00Z
const now = new Date();
const startDateTime = fromZonedTime(
startOfWeek(now),
ianaTimeZone,
).toISOString();
const endDateTime = fromZonedTime(
endOfWeek(now),
ianaTimeZone,
).toISOString();
const events = await GraphManager.getCalendarView(
startDateTime,
endDateTime,
tz,
);
this.setState({
loadingEvents: false,
events: events.value,
});
} catch (error) {
Alert.alert(
'Error getting events',
JSON.stringify(error),
[
{
text: 'OK',
},
],
{cancelable: false},
);
}
}
render() {
return (
<CalendarState.Provider value={this.state}>
<Stack.Navigator>
<Stack.Screen
name='CalendarScreen'
component={CalendarComponent}
options={{
headerShown: false,
}}
/>
</Stack.Navigator>
</CalendarState.Provider>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
loading: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
eventItem: {
padding: 10,
},
eventSubject: {
fontWeight: '700',
fontSize: 18,
},
eventOrganizer: {
fontWeight: '200',
},
eventDuration: {
fontWeight: '200',
},
});