diff --git a/src/notification-preferences/data/thunks.js b/src/notification-preferences/data/thunks.js index 6894afb..b90cef1 100644 --- a/src/notification-preferences/data/thunks.js +++ b/src/notification-preferences/data/thunks.js @@ -137,43 +137,68 @@ export const updatePreferenceToggle = ( notificationChannel, value, emailCadence, -) => async (dispatch) => { - try { - dispatch(updatePreferenceValue(notificationApp, notificationType, notificationChannel, !value)); - - const togglePreference = courseId - ? (cId, app, type, channel, val) => patchPreferenceToggle(cId, app, type, channel, val) - : (app, type, channel, val, cadence) => postPreferenceToggle(app, type, channel, val, cadence); - - let data = courseId - ? await togglePreference(courseId, notificationApp, notificationType, notificationChannel, value) - : await togglePreference(notificationApp, notificationType, notificationChannel, value, emailCadence); - - let normalizedData = courseId ? normalizePreferences(camelCaseObject(data), courseId) : camelCaseObject(data); - dispatch(fetchNotificationPreferenceSuccess(courseId, normalizedData, !courseId)); - - if (notificationChannel === EMAIL && value) { - data = courseId - ? await togglePreference( +) => ( + async (dispatch) => { + try { + dispatch(updatePreferenceValue( + notificationApp, + notificationType, + notificationChannel, + !value, + )); + let data = null; + if (courseId) { + data = await patchPreferenceToggle( courseId, notificationApp, notificationType, - EMAIL_CADENCE, - EMAIL_CADENCE_PREFERENCES.DAILY, - ) - : await togglePreference( + notificationChannel, + value, + ); + let normalizedData = normalizePreferences(camelCaseObject(data), courseId); + dispatch(fetchNotificationPreferenceSuccess(courseId, normalizedData)); + + if (notificationChannel === EMAIL && value) { + data = await patchPreferenceToggle( + courseId, + notificationApp, + notificationType, + EMAIL_CADENCE, + EMAIL_CADENCE_PREFERENCES.DAILY, + ); + normalizedData = normalizePreferences(camelCaseObject(data), courseId); + dispatch(fetchNotificationPreferenceSuccess(courseId, normalizedData)); + } + } else { + data = await postPreferenceToggle( notificationApp, notificationType, - EMAIL_CADENCE, - undefined, - EMAIL_CADENCE_PREFERENCES.DAILY, + notificationChannel, + value, + emailCadence, ); + dispatch(fetchNotificationPreferenceSuccess(courseId, camelCaseObject(data), true)); - normalizedData = courseId ? normalizePreferences(camelCaseObject(data), courseId) : camelCaseObject(data); - dispatch(fetchNotificationPreferenceSuccess(courseId, normalizedData, !courseId)); + if (notificationChannel === EMAIL && value) { + data = await postPreferenceToggle( + notificationApp, + notificationType, + EMAIL_CADENCE, + undefined, + EMAIL_CADENCE_PREFERENCES.DAILY, + ); + + dispatch(fetchNotificationPreferenceSuccess(courseId, camelCaseObject(data), true)); + } + } + } catch (errors) { + dispatch(updatePreferenceValue( + notificationApp, + notificationType, + notificationChannel, + !value, + )); + dispatch(fetchNotificationPreferenceFailed()); } - } catch (errors) { - dispatch(updatePreferenceValue(notificationApp, notificationType, notificationChannel, !value)); - dispatch(fetchNotificationPreferenceFailed()); } -}; +);