-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implented notification preference UI for cadence (#1013)
* feat: implented notification preference UI for cadence * refactor: refactored code * refactor: refactored code * refactor: clean code after adding email cadence * refactor: refactored and restructured notificationPreferences page * refactor: refactored and implemented mobile view --------- Co-authored-by: Awais Ansari <[email protected]>
- Loading branch information
1 parent
6cc50a9
commit a266e3d
Showing
12 changed files
with
354 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import React, { useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { useIntl } from '@edx/frontend-platform/i18n'; | ||
import { | ||
Dropdown, ModalPopup, Button, useToggle, | ||
} from '@openedx/paragon'; | ||
import { ExpandLess, ExpandMore } from '@openedx/paragon/icons'; | ||
import messages from './messages'; | ||
import { EMAIL_CADENCE } from './data/constants'; | ||
|
||
const EmailCadences = ({ | ||
email, onToggle, emailCadence, notificationType, | ||
}) => { | ||
const intl = useIntl(); | ||
const [isOpen, open, close] = useToggle(false); | ||
const [target, setTarget] = useState(null); | ||
|
||
return ( | ||
<> | ||
<Button | ||
ref={setTarget} | ||
variant="outline-primary" | ||
onClick={open} | ||
disabled={email} | ||
size="sm" | ||
iconAfter={isOpen ? ExpandLess : ExpandMore} | ||
className="border-light-300 text-primary-500 justify-content-between ml-3.5 cadence-button" | ||
> | ||
{intl.formatMessage(messages.emailCadence, { text: emailCadence })} | ||
</Button> | ||
<ModalPopup | ||
onClose={close} | ||
positionRef={target} | ||
isOpen={isOpen} | ||
> | ||
<div | ||
className="bg-white shadow d-flex flex-column" | ||
data-testid="email-cadence-dropdown" | ||
> | ||
{Object.values(EMAIL_CADENCE).map((cadence) => ( | ||
<Dropdown.Item | ||
key={cadence} | ||
name="email_cadence" | ||
className="d-flex justify-content-start py-1.5" | ||
as={Button} | ||
variant="primary" | ||
size="inline" | ||
autoFocus={cadence === emailCadence} | ||
onClick={(event) => onToggle(event, notificationType)} | ||
> | ||
{intl.formatMessage(messages.emailCadence, { text: cadence })} | ||
</Dropdown.Item> | ||
))} | ||
</div> | ||
</ModalPopup> | ||
</> | ||
); | ||
}; | ||
|
||
EmailCadences.propTypes = { | ||
email: PropTypes.bool.isRequired, | ||
onToggle: PropTypes.func.isRequired, | ||
emailCadence: PropTypes.oneOf(Object.values(EMAIL_CADENCE)).isRequired, | ||
notificationType: PropTypes.string.isRequired, | ||
}; | ||
|
||
export default React.memo(EmailCadences); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
src/notification-preferences/NotificationPreferenceColumn.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
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) => ( | ||
<div | ||
key={`${preference.id}-${channel}`} | ||
id={`${preference.id}-${channel}`} | ||
className={classNames( | ||
'd-flex align-items-center justify-content-center mb-2 h-4.5 column-padding', | ||
{ | ||
'pr-0': channel === 'email', | ||
'pl-0': channel === 'web' && mobileView, | ||
}, | ||
)} | ||
> | ||
<ToggleSwitch | ||
name={channel} | ||
value={preference[channel]} | ||
onChange={(event) => onToggle(event, preference.id)} | ||
disabled={nonEditable?.[preference.id]?.includes(channel) || updatePreferencesStatus === LOADING_STATUS} | ||
id={`${preference.id}-${channel}`} | ||
className="my-1" | ||
/> | ||
{channel === 'email' && ( | ||
<EmailCadences | ||
email={preference.email} | ||
onToggle={onToggle} | ||
emailCadence={preference.emailCadence} | ||
notificationType={preference.id} | ||
/> | ||
)} | ||
</div> | ||
); | ||
|
||
return ( | ||
<div className={classNames('d-flex flex-column', { 'border-right': channel !== 'email' })}> | ||
<NavItem | ||
id={channel} | ||
key={channel} | ||
role="button" | ||
onClick={onChannelToggle} | ||
className={classNames('mb-3 header-label column-padding', { | ||
'pr-0': channel === 'email', | ||
'pl-0': channel === 'web' && mobileView, | ||
})} | ||
> | ||
{intl.formatMessage(messages.notificationChannel, { text: channel })} | ||
</NavItem> | ||
{appPreference | ||
? renderPreference(appPreference) | ||
: appPreferences.map((preference) => (renderPreference(preference)))} | ||
</div> | ||
); | ||
}; | ||
|
||
NotificationPreferenceColumn.propTypes = { | ||
appId: PropTypes.string.isRequired, | ||
channel: PropTypes.string.isRequired, | ||
appPreference: PropTypes.shape({ | ||
id: PropTypes.string, | ||
emailCadence: PropTypes.string, | ||
appId: PropTypes.string, | ||
info: PropTypes.string, | ||
email: PropTypes.bool, | ||
push: PropTypes.bool, | ||
web: PropTypes.bool, | ||
}), | ||
}; | ||
|
||
NotificationPreferenceColumn.defaultProps = { | ||
appPreference: null, | ||
}; | ||
|
||
export default React.memo(NotificationPreferenceColumn); |
Oops, something went wrong.