From 35fbdcbae4a584629ef2664e1d300073d8669d09 Mon Sep 17 00:00:00 2001 From: Rob Moffat Date: Wed, 22 Jan 2025 14:37:29 +0000 Subject: [PATCH] Added message to say it's in progress + error handling --- src/api/signup-submit.js | 89 ++++++++++++++++++++++++++++------------ src/pages/signup.js | 28 ++++++++++--- 2 files changed, 86 insertions(+), 31 deletions(-) diff --git a/src/api/signup-submit.js b/src/api/signup-submit.js index d44813e5827..b7367cc60f7 100644 --- a/src/api/signup-submit.js +++ b/src/api/signup-submit.js @@ -1,6 +1,59 @@ import { google } from 'googleapis'; import { readFileSync } from 'fs'; +async function updateAttendees(api, calendarId, eventId, newAttendees) { + console.log(`Adding attendees to `, eventId, newAttendees) + + await api.events.patch({ + calendarId: calendarId, + eventId: eventId, + resource: { + attendees: newAttendees + }, + sendUpdates: 'externalOnly' + }) + +} + +async function addAttendeeToEvent(api, calendarId, event, email, addForReal) { + + console.log(`Adding attendee to `, event.id, event.start, event.summary, addForReal) + + const newAttendees = [ + ...event.attendees, + { + email: email, + }, + ] + const eventId = event.id + + if (addForReal) { + return updateAttendees(api, calendarId, eventId, newAttendees) + } +} + +async function updateEventRequest(api, calendarId, eventId, email) { + const event = (await api.events.get({ + calendarId: calendarId, + eventId: eventId, + })).data + + console.log(`Original Event:`, event) + + const masterEventId = event.recurringEventId ? event.recurringEventId : event.id + + console.log(`Master Event ID:`, masterEventId) + + // update the series + const masterEvent = (masterEventId != eventId) ? (await api.events.get({ + calendarId: calendarId, + eventId: masterEventId, + })).data : event + + console.log(`Master Event:`, masterEvent) + + await addAttendeeToEvent(api, calendarId, masterEvent, email, true) +} export default async function handler(req, res) { console.log(`submitted form`, req.body, __filename) @@ -14,8 +67,6 @@ export default async function handler(req, res) { JSON.parse(process.env.GOOGLE_SERVICE_ACCOUNT) : JSON.parse(readFileSync('./calendar-service-account.json')) - console.log(`CREDENTIALS`, CREDENTIALS) - // Scopes required for the Google Calendar API const SCOPES = [ 'https://www.googleapis.com/auth/calendar' @@ -31,31 +82,17 @@ export default async function handler(req, res) { }); // Create a Calendar API client - const calendar = google.calendar({ version: 'v3', auth: await auth.getClient() }); - - const event = (await calendar.events.get({ - calendarId: calendarId, - eventId: eventId, - })).data - - - console.log(`event`, event) + const api = google.calendar({ version: 'v3', auth: await auth.getClient() }); - const existingAttendees = event.attendees ?? [] + const individualEvents = eventId.split(',') - const done = await calendar.events.patch({ - calendarId: calendarId, - eventId: eventId, - resource: { - attendees: [ - ...existingAttendees, - { - email: req.body.email, - }, - ] - }, - sendUpdates: 'externalOnly' - }) + try { + for (const individualEventId of individualEvents) { + await updateEventRequest(api, calendarId, individualEventId, req.body.email) + } - res.json(`ok bebob ${JSON.stringify("hello")}`) + res.json(`success`) + } catch (err) { + res.json(`Error updating calendar: ${err}`) + } } \ No newline at end of file diff --git a/src/pages/signup.js b/src/pages/signup.js index 0c3c19899e3..ca83b648e71 100644 --- a/src/pages/signup.js +++ b/src/pages/signup.js @@ -10,6 +10,7 @@ export default function App({ location }) { formState: { errors }, } = useForm() const onSubmit = data => { + setIsSubmitted('spinner') fetch(`/api/signup-submit`, { method: `POST`, body: JSON.stringify(data), @@ -20,19 +21,36 @@ export default function App({ location }) { .then(res => res.json()) .then(body => { console.log(`response from API:`, body) - setIsSubmitted(true) + setIsSubmitted(body) + }).catch(err => { + console.error(`Error updating calendar:`, err) + setIsSubmitted(`Error updating calendar: ${err}`) }) } const params = new URLSearchParams(location.search) - if (isSubmitted) { + if (isSubmitted == 'spinner') { return ( -
-

Thank you for signing up!

-

Please check your inbox for a calendar invite for {params.get("title")}.

+
+

Please wait

+

Updating the calendar and sending invite...

) + + } else if (isSubmitted) { + if (isSubmitted == 'success') { + return ( +
+

Thank you for signing up!

+

Please check your inbox for a calendar invite for {params.get("title")}.

+
+ ) + } else { + return ( +
{isSubmitted}
+ ) + } } else { return (