From a3497adb6d5a5b33dd9869ce393ef2579b95daba Mon Sep 17 00:00:00 2001 From: eemaanamir Date: Tue, 23 Jan 2024 15:07:40 +0500 Subject: [PATCH] chore: refactoring the code for readability according to ESLint --- .../NotificationPreferenceApp.jsx | 50 +++++++------------ .../data/selectors.js | 2 +- src/notification-preferences/data/service.js | 13 +---- src/notification-preferences/data/thunks.js | 17 ++----- src/notification-preferences/messages.js | 11 ++++ 5 files changed, 35 insertions(+), 58 deletions(-) diff --git a/src/notification-preferences/NotificationPreferenceApp.jsx b/src/notification-preferences/NotificationPreferenceApp.jsx index f6a84c7..97f0252 100644 --- a/src/notification-preferences/NotificationPreferenceApp.jsx +++ b/src/notification-preferences/NotificationPreferenceApp.jsx @@ -7,7 +7,8 @@ import classNames from 'classnames'; import messages from './messages'; import ToggleSwitch from './ToggleSwitch'; import { - selectPreferenceAppToggleValue, selectPreferenceNonEditable, + selectPreferenceAppToggleValue, + selectNonEditablePreferences, selectPreferencesOfApp, selectSelectedCourseId, selectUpdatePreferencesStatus, @@ -24,32 +25,19 @@ const NotificationPreferenceApp = ({ appId }) => { const appPreferences = useSelector(selectPreferencesOfApp(appId)); const appToggle = useSelector(selectPreferenceAppToggleValue(appId)); const updatePreferencesStatus = useSelector(selectUpdatePreferencesStatus()); - const nonEditable = useSelector(selectPreferenceNonEditable(appId)); + const nonEditable = useSelector(selectNonEditablePreferences(appId)); - const onColumnToggle = useCallback( + const onChannelToggle = useCallback( (event) => { - const { - id: notificationChannel, - } = event.target; - const truePreferences = appPreferences.filter((preference) => { - const isPreferenceNonEditable = nonEditable?.[preference.id]?.includes(notificationChannel) || false; - return preference[notificationChannel] === true && !isPreferenceNonEditable; - }); - if (truePreferences.length > 0) { - dispatch(updateChannelPreferenceToggle( - courseId, - appId, - notificationChannel, - false, - )); - } else { - dispatch(updateChannelPreferenceToggle( - courseId, - appId, - notificationChannel, - true, - )); - } + 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], ); @@ -70,6 +58,7 @@ const NotificationPreferenceApp = ({ appId }) => { if (!courseId) { return null; } + return ( @@ -95,6 +84,7 @@ const NotificationPreferenceApp = ({ appId }) => { {NOTIFICATION_CHANNELS.map((channel) => ( { { 'ml-auto mr-0': channel === 'push' }, )} role="button" - onClick={onColumnToggle} + onClick={onChannelToggle} > - { - // eslint-disable-next-line no-nested-ternary - channel === 'web' ? intl.formatMessage(messages.webLabel) - // eslint-disable-next-line no-nested-ternary - : channel === 'email' ? intl.formatMessage(messages.notificationHelpEmail) - : channel === 'push' ? intl.formatMessage(messages.notificationHelpPush) : null - } + {intl.formatMessage(messages.notificationChannel, { text: channel })} ))} diff --git a/src/notification-preferences/data/selectors.js b/src/notification-preferences/data/selectors.js index d6f7ee4..ffbc5d9 100644 --- a/src/notification-preferences/data/selectors.js +++ b/src/notification-preferences/data/selectors.js @@ -54,7 +54,7 @@ export const selectPreferenceNonEditableChannels = (appId, name) => state => ( state?.notificationPreferences.preferences.nonEditable[appId]?.[name] || [] ); -export const selectPreferenceNonEditable = (appId) => state => ( +export const selectNonEditablePreferences = appId => state => ( state?.notificationPreferences.preferences.nonEditable[appId] || [] ); diff --git a/src/notification-preferences/data/service.js b/src/notification-preferences/data/service.js index 6e357b1..50a1e04 100644 --- a/src/notification-preferences/data/service.js +++ b/src/notification-preferences/data/service.js @@ -43,17 +43,8 @@ export const patchPreferenceToggle = async ( return data; }; -export const patchChannelPreferenceToggle = async ( - courseId, - notificationApp, - notificationChannel, - value, -) => { - const patchData = snakeCaseObject({ - notificationApp, - notificationChannel, - value, - }); +export const patchChannelPreferenceToggle = async (courseId, notificationApp, notificationChannel, value) => { + const patchData = snakeCaseObject({ notificationApp, notificationChannel, value }); const url = `${getConfig().LMS_BASE_URL}/api/notifications/channel/configurations/${courseId}`; const { data } = await getAuthenticatedHttpClient().patch(url, patchData); return data; diff --git a/src/notification-preferences/data/thunks.js b/src/notification-preferences/data/thunks.js index 7b257c8..222201b 100644 --- a/src/notification-preferences/data/thunks.js +++ b/src/notification-preferences/data/thunks.js @@ -13,7 +13,8 @@ import { import { getCourseList, getCourseNotificationPreferences, - patchAppPreferenceToggle, patchChannelPreferenceToggle, + patchAppPreferenceToggle, + patchChannelPreferenceToggle, patchPreferenceToggle, } from './service'; @@ -149,20 +150,10 @@ export const updatePreferenceToggle = ( } ); -export const updateChannelPreferenceToggle = ( - courseId, - notificationApp, - notificationChannel, - value, -) => ( +export const updateChannelPreferenceToggle = (courseId, notificationApp, notificationChannel, value) => ( async (dispatch) => { try { - const data = await patchChannelPreferenceToggle( - courseId, - notificationApp, - notificationChannel, - value, - ); + const data = await patchChannelPreferenceToggle(courseId, notificationApp, notificationChannel, value); const normalizedData = normalizePreferences(camelCaseObject(data)); dispatch(fetchNotificationPreferenceSuccess(courseId, normalizedData)); } catch (errors) { diff --git a/src/notification-preferences/messages.js b/src/notification-preferences/messages.js index 91780fb..1ba1733 100644 --- a/src/notification-preferences/messages.js +++ b/src/notification-preferences/messages.js @@ -27,6 +27,17 @@ const messages = defineMessages({ }`, description: 'Display text for Notification Types', }, + notificationChannel: { + id: 'notification.preference.channel', + defaultMessage: `{ + text, select, + web {Web} + email {Email} + push {Push} + other {{text}} + }`, + description: 'Display text for Notification Channel', + }, typeLabel: { id: 'notification.preference.type.label', defaultMessage: 'Type',