import React, { useCallback } from 'react'; import PropTypes from 'prop-types'; import classNames from 'classnames'; import { useDispatch, useSelector } from 'react-redux'; import { useIntl } from '@edx/frontend-platform/i18n'; import { NavItem } from '@openedx/paragon'; import messages from './messages'; import ToggleSwitch from './ToggleSwitch'; import { selectNonEditablePreferences, selectSelectedCourseId, selectUpdatePreferencesStatus, selectPreferencesOfApp, } from './data/selectors'; import { updatePreferenceToggle, updateChannelPreferenceToggle } from './data/thunks'; import { LOADING_STATUS } from '../constants'; import EmailCadences from './EmailCadences'; import { useIsOnMobile } from '../hooks'; const NotificationPreferenceColumn = ({ appId, channel, appPreference }) => { const dispatch = useDispatch(); const intl = useIntl(); const courseId = useSelector(selectSelectedCourseId()); const appPreferences = useSelector(selectPreferencesOfApp(appId)); const nonEditable = useSelector(selectNonEditablePreferences(appId)); const updatePreferencesStatus = useSelector(selectUpdatePreferencesStatus()); const mobileView = useIsOnMobile(); const onChannelToggle = useCallback((event) => { const { id: notificationChannel } = event.target; const isPreferenceNonEditable = (preference) => nonEditable?.[preference.id]?.includes(notificationChannel); const hasActivePreferences = appPreferences.some( (preference) => preference[notificationChannel] && !isPreferenceNonEditable(preference), ); dispatch(updateChannelPreferenceToggle(courseId, appId, notificationChannel, !hasActivePreferences)); }, [appId, appPreferences, courseId, dispatch, nonEditable]); const onToggle = useCallback((event, notificationType) => { const { name: notificationChannel } = event.target; const value = notificationChannel === 'email_cadence' ? event.target.innerText : event.target.checked; dispatch(updatePreferenceToggle( courseId, appId, notificationType, notificationChannel, value, )); // eslint-disable-next-line react-hooks/exhaustive-deps }, [appId]); const renderPreference = (preference) => ( (preference.coreNotificationTypes.length > 0 || preference.id !== 'core') && (