-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.html
358 lines (315 loc) · 10.4 KB
/
index.html
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Find meeting times</title>
<script src="https://unpkg.com/@microsoft/mgt@2/dist/bundle/mgt-loader.js"></script>
<script src="./env.js"></script>
<style>
html,
select,
input,
button {
font-family: 'Segoe UI', Tahoma, sans-serif;
}
html {
font-size: 0.875em;
}
.error {
color: red;
}
.grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-column-gap: 0px;
grid-row-gap: 0px;
}
.title {
grid-area: 1 / 1 / 2 / 2;
}
.login {
display: grid;
grid-area: 1 / 2 / 2 / 3;
align-content: center;
justify-content: end;
}
.body {
grid-area: 2 / 1 / 3 / 3;
width: 40em;
}
.field {
margin-bottom: 1em;
clear: both;
}
label {
display: block;
margin-bottom: 0.2em;
}
select {
padding: 0.25em;
}
input {
width: 50%;
}
button {
padding: 0.5em 1em;
}
input {
padding: 0.5em;
}
#result ul {
list-style-type: none;
margin: 0;
padding: 0;
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-column-gap: 10px;
}
#result li {
display: grid;
border: 1px solid #aaa;
border-radius: 5px;
}
#result li.selected {
border-color: rgb(14, 164, 234);
background-color: #eee;
}
#result li label {
cursor: pointer;
padding: 0.75em 1em;
}
#result li input {
display: none;
}
#result li .date {
font-weight: bold;
display: block;
margin-bottom: 0.5em;
}
#schedule {
display: none;
}
#schedule button {
display: block;
margin-bottom: 1em;
}
</style>
</head>
<body>
<div class="grid">
<div class="title">
<h1>Find meeting times</h1>
</div>
<div class="login">
<mgt-login></mgt-login>
</div>
<div class="body">
<div class="field">
<label>Invite attendees</label>
<mgt-people-picker></mgt-people-picker>
</div>
<div class="field">
<label for="duration">Meeting duration</label>
<select id="duration">
<option value="25M">25 minutes</option>
<option value="55M">55 minutes</option>
<option value="1H">1 hour</option>
</select>
</div>
<div class="field" id="findMeetingTimes">
<button disabled>Find available meeting times</button>
</div>
<div class="field" id="result">
</div>
<div id="schedule">
<div class="field">
<label for="subject">Meeting subject</label>
<input id="subject" type="text" required>
</div>
<div class="field">
<label for="location">Meeting location</label>
<input id="location" type="text" required>
</div>
<div class="field">
<button disabled>Schedule meeting</button>
<span id="status"></span>
</div>
</div>
</div>
</div>
<script>
(() => {
let availableMeetingTimes = null;
let selectedMeetingTime = null;
let graphClient;
// set up auth
mgt.Providers.globalProvider = new mgt.Msal2Provider({
clientId: appId,
// define all permissions upfront so that we don't get additional prompts
scopes: ['User.Read', 'People.Read', 'User.ReadBasic.All', 'Calendars.Read.Shared', 'Calendars.ReadWrite']
});
// show body when logged in
mgt.Providers.onProviderUpdated(() => {
document.querySelector('.body').style.display =
mgt.Providers.globalProvider.state === mgt.ProviderState.SignedIn ? 'block' : 'none';
graphClient = mgt.Providers.globalProvider.graph.client;
});
function clearMeetingTimes() {
availableMeetingTimes = null;
selectedMeetingTime = null;
document.querySelector('#result').innerHTML = '';
document.querySelector('#schedule').style.display = 'none';
document.querySelector('#status').innerHTML = '';
}
function toggleFindMeetingTimes() {
const people = document.querySelector('mgt-people-picker').selectedPeople;
document
.querySelector('#findMeetingTimes button')
.disabled = people.length === 0;
clearMeetingTimes();
}
function toggleInputForm(props) {
document.querySelector('mgt-people-picker').disabled = !props.enabled;
document.querySelector('#duration').disabled = !props.enabled;
document.querySelector('#findMeetingTimes button').disabled = !props.enabled;
document.querySelector('#subject').disabled = !props.enabled;
document.querySelector('#location').disabled = !props.enabled;
if (props.enabled) {
// we can't just enable the schedule button, because we might be
// missing required subject and location fields
toggleScheduleMeeting();
}
else {
document.querySelector('#schedule button').disabled = true;
}
}
function toggleScheduleMeeting() {
document.querySelector('#schedule button').disabled =
selectedMeetingTime === 'null' ||
document.querySelector('#subject').value.length === 0 ||
document.querySelector('#location').value.length === 0;
}
function selectMeetingTime(event) {
// clear previously selected meeting time
const previouslySelected = document.querySelector('#result .selected');
if (previouslySelected) {
previouslySelected.classList.remove('selected');
}
// mark selected meeting time
const input = event.target;
input.parentElement.parentElement.classList.add('selected');
selectedMeetingTime = input.value;
document.querySelector('#schedule').style.display = 'block';
}
function getTimeString(date) {
const hours = date.getHours();
const minutes = date.getMinutes();
return `${hours}:${minutes < 10 ? '0' : ''}${minutes}`;
}
function showMeetingTimes(meetingTimes) {
const result = document.querySelector('#result');
if (meetingTimes.length > 0) {
result.innerHTML = '<label>Select meeting time</label>';
const ul = document.createElement('ul');
result.appendChild(ul);
meetingTimes.forEach((meetingTime, index) => {
const li = document.createElement('li');
const startDateTime = new Date(meetingTime.meetingTimeSlot.start.dateTime + 'Z');
const endDateTime = new Date(meetingTime.meetingTimeSlot.end.dateTime + 'Z');
li.innerHTML = `<label>
<input type="radio" name="meetingTime" value="${index}">
<span class="date">${startDateTime.toLocaleDateString()}</span>
<span class="time">${getTimeString(startDateTime)} - ${getTimeString(endDateTime)}</span>
</label>`;
ul.appendChild(li);
});
// listen to selecting a meeting time
ul.querySelectorAll('input')
.forEach(input => input.addEventListener('change', selectMeetingTime));
}
else {
result.innerHTML = '⚠️ No meeting times available. Change the duration and try again';
}
}
async function findMeetingTimes() {
clearMeetingTimes();
toggleInputForm({ enabled: false });
document.querySelector('#result').innerHTML = '<em>Finding available meeting times...</em>';
try {
const meetingTimes = await graphClient
.api('/me/findMeetingTimes')
.post({
attendees: document.querySelector('mgt-people-picker').selectedPeople.map(p => {
return {
emailAddress: {
address: p.userPrincipalName,
name: p.displayName
},
type: 'required'
};
}),
maxCandidates: 3,
meetingDuration: `PT${document.querySelector('#duration').value}`,
returnSuggestionReasons: true,
minimumAttendeePercentage: 100
});
availableMeetingTimes = meetingTimes.meetingTimeSuggestions;
showMeetingTimes(meetingTimes.meetingTimeSuggestions);
}
catch (error) {
document.querySelector('#result').innerHTML = `<span class="error">🛑 Error finding meeting times: ${error}</span>`;
}
finally {
toggleInputForm({ enabled: true });
}
}
async function scheduleMeeting() {
toggleInputForm({ enabled: false });
document.querySelector('#status').innerHTML = '<em>Scheduling meeting...</em>';
const meetingTime = availableMeetingTimes[selectedMeetingTime].meetingTimeSlot;
try {
await graphClient
.api('/me/events')
.post({
subject: document.querySelector('#subject').value,
start: meetingTime.start,
end: meetingTime.end,
attendees: document.querySelector('mgt-people-picker').selectedPeople.map(p => {
return {
emailAddress: {
address: p.userPrincipalName,
name: p.displayName
},
type: 'required'
};
})
});
document.querySelector('#status').innerHTML = '✅ Meeting scheduled!';
}
catch (error) {
document.querySelector('#status').innerHTML = `<span class="error">🛑 Error scheduling meeting: ${error}</span>`;
}
finally {
toggleInputForm({ enabled: true });
}
}
document
.querySelector('mgt-people-picker')
.addEventListener('selectionChanged', toggleFindMeetingTimes);
document
.querySelector('#duration')
.addEventListener('change', clearMeetingTimes);
document
.querySelector('#findMeetingTimes button')
.addEventListener('click', findMeetingTimes);
document
.querySelectorAll('#subject, #location')
.forEach(input => input.addEventListener('input', toggleScheduleMeeting));
document
.querySelector('#schedule button')
.addEventListener('click', scheduleMeeting);
})();
</script>
</body>
</html>