From 59f9cdbddeb4cd41b2fbea2c3a7b3df776aba621 Mon Sep 17 00:00:00 2001 From: Jack Fisher Date: Fri, 23 Feb 2024 14:58:25 -0600 Subject: [PATCH] updating notification method logic lint editing pattern to filter reduce --- .../src/features/alerts/AlertReportModal.tsx | 42 +++++++++++++------ .../alerts/components/NotificationMethod.tsx | 2 +- .../src/features/alerts/types.ts | 1 - 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/superset-frontend/src/features/alerts/AlertReportModal.tsx b/superset-frontend/src/features/alerts/AlertReportModal.tsx index 31a12dd9eaa87..50e2380b5fe3b 100644 --- a/superset-frontend/src/features/alerts/AlertReportModal.tsx +++ b/superset-frontend/src/features/alerts/AlertReportModal.tsx @@ -492,15 +492,26 @@ const AlertReportModal: FunctionComponent = ({ NotificationSetting[] >([]); const onNotificationAdd = () => { - const settings: NotificationSetting[] = notificationSettings.slice(); - settings.push({ - recipients: '', - options: allowedNotificationMethods, - }); + setNotificationSettings([ + ...notificationSettings, + { + recipients: '', + // options shown in the newly added notification method + options: allowedNotificationMethods.filter( + // are filtered such that + option => + // options are not included + !notificationSettings.reduce( + // when it exists in previous notificationSettings + (accum, setting) => accum || option === setting.method, + false, + ), + ), + }, + ]); - setNotificationSettings(settings); setNotificationAddState( - settings.length === allowedNotificationMethods.length + notificationSettings.length === allowedNotificationMethods.length ? 'hidden' : 'disabled', ); @@ -542,13 +553,20 @@ const AlertReportModal: FunctionComponent = ({ index: number, setting: NotificationSetting, ) => { - const settings = notificationSettings.slice(); + // if you've changed notification method + if (notificationSettings[index].method !== setting.method) { + notificationSettings[index] = setting; - settings[index] = setting; - setNotificationSettings(settings); + setNotificationSettings( + notificationSettings.filter((_, idx) => idx <= index), + ); + if (notificationSettings.length - 1 > index) { + setNotificationAddState('active'); + } - if (setting.method !== undefined && notificationAddState !== 'hidden') { - setNotificationAddState('active'); + if (setting.method !== undefined && notificationAddState !== 'hidden') { + setNotificationAddState('active'); + } } }; diff --git a/superset-frontend/src/features/alerts/components/NotificationMethod.tsx b/superset-frontend/src/features/alerts/components/NotificationMethod.tsx index cf3e3c168f10c..b50988700ef4e 100644 --- a/superset-frontend/src/features/alerts/components/NotificationMethod.tsx +++ b/superset-frontend/src/features/alerts/components/NotificationMethod.tsx @@ -124,7 +124,7 @@ export const NotificationMethod: FunctionComponent = ({ )} value={method} /> - {method !== undefined && index !== 0 && !!onRemove ? ( + {index !== 0 && !!onRemove ? (