From a6699f94c9d0ff9d45c1e8d4d21f446e47b4fd03 Mon Sep 17 00:00:00 2001 From: eemaanamir Date: Tue, 23 Jan 2024 19:47:59 +0500 Subject: [PATCH] refactor: onChannelToggle updated for readability --- .../NotificationPreferenceApp.jsx | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/notification-preferences/NotificationPreferenceApp.jsx b/src/notification-preferences/NotificationPreferenceApp.jsx index 97f025267..7e37a1be1 100644 --- a/src/notification-preferences/NotificationPreferenceApp.jsx +++ b/src/notification-preferences/NotificationPreferenceApp.jsx @@ -27,20 +27,16 @@ const NotificationPreferenceApp = ({ appId }) => { const updatePreferencesStatus = useSelector(selectUpdatePreferencesStatus()); const nonEditable = useSelector(selectNonEditablePreferences(appId)); - const onChannelToggle = useCallback( - (event) => { - const { id: notificationChannel } = event.target; - const activePreferences = appPreferences.filter((preference) => preference[notificationChannel] === true - && !nonEditable?.[preference.id]?.includes(notificationChannel)); - dispatch(updateChannelPreferenceToggle( - courseId, - appId, - notificationChannel, - !(activePreferences.length > 0), - )); - }, - [appId, appPreferences, courseId, dispatch, nonEditable], - ); + const onChannelToggle = useCallback((event) => { + const { id: notificationChannel } = event.target; + const isPreferenceNonEditable = (preference) => nonEditable?.[preference.id]?.includes(notificationChannel) + || false; + + const truePreferences = appPreferences.filter((preference) => preference[notificationChannel] === true + && !isPreferenceNonEditable(preference)); + + dispatch(updateChannelPreferenceToggle(courseId, appId, notificationChannel, truePreferences.length === 0)); + }, [appId, appPreferences, courseId, dispatch, nonEditable]); const preferences = useMemo(() => ( appPreferences.map(preference => (