From c1a629f0467a8204bddbcc9328aebb85002b732b Mon Sep 17 00:00:00 2001 From: RachelElysia Date: Thu, 21 Mar 2024 16:53:48 -0400 Subject: [PATCH 1/3] Add error handling for invalid JSON, escape characters from placeholder text --- .../IntegrationsPage/cards/Calendars/Calendars.tsx | 13 +++++++++++-- tsconfig.json | 3 ++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/frontend/pages/admin/IntegrationsPage/cards/Calendars/Calendars.tsx b/frontend/pages/admin/IntegrationsPage/cards/Calendars/Calendars.tsx index de7c79a139b0..90ad97ad933f 100644 --- a/frontend/pages/admin/IntegrationsPage/cards/Calendars/Calendars.tsx +++ b/frontend/pages/admin/IntegrationsPage/cards/Calendars/Calendars.tsx @@ -33,7 +33,7 @@ const API_KEY_JSON_PLACEHOLDER = `{ "type": "service_account", "project_id": "fleet-in-your-calendar", "private_key_id": "", - "private_key": "-----BEGIN PRIVATE KEY-----\n\n-----END PRIVATE KEY-----\n", + "private_key": "-----BEGIN PRIVATE KEY----\\n\\n-----END PRIVATE KEY-----\\n", "client_email": "fleet-calendar-events@fleet-in-your-calendar.iam.gserviceaccount.com", "client_id": "", "auth_uri": "https://accounts.google.com/o/oauth2/auth", @@ -103,7 +103,14 @@ const Calendars = (): JSX.Element => { errors.apiKeyJson = "API key JSON must be present"; } if (!curFormData.domain && !!curFormData.apiKeyJson) { - errors.apiKeyJson = "Domain must be present"; + errors.domain = "Domain must be present"; + } + if (curFormData.apiKeyJson) { + try { + JSON.parse(curFormData.apiKeyJson); + } catch (e) { + errors.apiKeyJson = e.message.toString(); + } } return errors; }; @@ -277,6 +284,7 @@ const Calendars = (): JSX.Element => { placeholder={API_KEY_JSON_PLACEHOLDER} ignore1password inputClassName={`${baseClass}__api-key-json`} + error={formErrors.apiKeyJson} /> { /> } + error={formErrors.domain} />