From cfdf780f8627dca2b8faaf1f4fc2c94cbe39f866 Mon Sep 17 00:00:00 2001 From: RabeeAbuBaker Date: Thu, 12 Oct 2023 20:18:33 +0300 Subject: [PATCH 1/2] fix: Notifications events not updating correctly --- .../AppNotificationsAlert.tsx | 34 +++++++++++++------ 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/app/routes/account.$accountId.$appId.notifications/components/AppNotificationsAlert/AppNotificationsAlert.tsx b/app/routes/account.$accountId.$appId.notifications/components/AppNotificationsAlert/AppNotificationsAlert.tsx index 727b7b533..4b7198300 100644 --- a/app/routes/account.$accountId.$appId.notifications/components/AppNotificationsAlert/AppNotificationsAlert.tsx +++ b/app/routes/account.$accountId.$appId.notifications/components/AppNotificationsAlert/AppNotificationsAlert.tsx @@ -48,31 +48,45 @@ export default function AppNotificationsAlert({ }: NotificationsAlertFormProps) { const { notifications } = app const fetcher = useFetcher() + useActionNotification(fetcher.data) + + const notificationEvents = notifications[0]?.appNotification?.events const getNotificationCheckedState = useCallback( (level: NotificationLevel) => { - // @ts-ignore - return Object.keys(notifications).length > 0 && notifications[level] - ? // @ts-ignore - (notifications[level] as boolean) + console.log(notificationEvents[level]) + return Object.keys(notificationEvents).length > 0 && + notificationEvents.hasOwnProperty(level) + ? (notificationEvents[level] as boolean) : DEFAULT_ALERT_PERCENTAGES[level] }, - [notifications], + [notificationEvents], ) const updateNotification = (level: string, value: string) => { + const otherNotificationEventsValues = NOTIFICATIONS_ALERT_LEVELS.filter( + (notificationLevel) => notificationLevel !== level, + ).reduce( + (result, notificationLevel) => ({ + ...result, + [notificationLevel]: getNotificationCheckedState(notificationLevel) + ? "on" + : "off", + }), + {}, + ) + fetcher.submit( { [level]: value, + ...otherNotificationEventsValues, }, { - method: "PUT", + method: "POST", }, ) } - useActionNotification(fetcher.data) - return ( @@ -89,10 +103,10 @@ export default function AppNotificationsAlert({ { - updateNotification(level, event.currentTarget.value) + updateNotification(level, event.currentTarget.checked ? "on" : "off") trackEvent({ category: AnalyticCategories.app, action: AnalyticActions.app_notifications, From 18e76b982adcf7f227e22cc7e90f2a325c1718a0 Mon Sep 17 00:00:00 2001 From: RabeeAbuBaker Date: Thu, 12 Oct 2023 21:20:25 +0300 Subject: [PATCH 2/2] refactor: clean up --- .../AppNotificationsAlert/AppNotificationsAlert.tsx | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/app/routes/account.$accountId.$appId.notifications/components/AppNotificationsAlert/AppNotificationsAlert.tsx b/app/routes/account.$accountId.$appId.notifications/components/AppNotificationsAlert/AppNotificationsAlert.tsx index 4b7198300..a8c3f9b54 100644 --- a/app/routes/account.$accountId.$appId.notifications/components/AppNotificationsAlert/AppNotificationsAlert.tsx +++ b/app/routes/account.$accountId.$appId.notifications/components/AppNotificationsAlert/AppNotificationsAlert.tsx @@ -53,13 +53,11 @@ export default function AppNotificationsAlert({ const notificationEvents = notifications[0]?.appNotification?.events const getNotificationCheckedState = useCallback( - (level: NotificationLevel) => { - console.log(notificationEvents[level]) - return Object.keys(notificationEvents).length > 0 && - notificationEvents.hasOwnProperty(level) + (level: NotificationLevel) => + Object.keys(notificationEvents).length > 0 && + notificationEvents.hasOwnProperty(level) ? (notificationEvents[level] as boolean) - : DEFAULT_ALERT_PERCENTAGES[level] - }, + : DEFAULT_ALERT_PERCENTAGES[level], [notificationEvents], )