diff --git a/web/modals/threads/notifications/notifications-modal.react.js b/web/modals/threads/notifications/notifications-modal.react.js index 25aa13063..8537b47cc 100644 --- a/web/modals/threads/notifications/notifications-modal.react.js +++ b/web/modals/threads/notifications/notifications-modal.react.js @@ -1,170 +1,181 @@ // @flow import * as React from 'react'; import { updateSubscription, updateSubscriptionActionTypes, } from 'lib/actions/user-actions'; import { threadInfoSelector } from 'lib/selectors/thread-selectors'; import { useServerCall, useDispatchActionPromise, } from 'lib/utils/action-utils'; import Button from '../../../components/button.react'; import { useSelector } from '../../../redux/redux-utils'; import SWMansionIcon from '../../../SWMansionIcon.react'; import Modal from '../../modal.react'; import css from './notifications-modal.css'; import NotificationsOption from './notifications-option.react'; type NotificationSettings = 'focused' | 'badge-only' | 'background'; type Props = { +threadID: string, +onClose: () => void, }; function NotificationsModal(props: Props): React.Node { const { onClose, threadID } = props; const threadInfo = useSelector(state => threadInfoSelector(state)[threadID]); const { subscription } = threadInfo.currentUser; const initialThreadSetting = React.useMemo(() => { if (!subscription.home) { return 'background'; } if (!subscription.pushNotifs) { return 'badge-only'; } return 'focused'; }, [subscription.home, subscription.pushNotifs]); - const [ - notificationSettings, - setNotificationSettings, - ] = React.useState(initialThreadSetting); + const [notificationSettings, setNotificationSettings] = + React.useState(initialThreadSetting); + + const onFocusedSelected = React.useCallback( + () => setNotificationSettings('focused'), + [], + ); + const onBadgeOnlySelected = React.useCallback( + () => setNotificationSettings('badge-only'), + [], + ); + const onBackgroundSelected = React.useCallback( + () => setNotificationSettings('background'), + [], + ); const notificationIconStyle = React.useMemo(() => ({ width: 'auto' }), []); const isFocusedSelected = notificationSettings === 'focused'; const focusedItem = React.useMemo(() => { const description = [ ['Banner notifs', true], ['Badge count', true], ['Lives in Focused tab', true], ]; const icon = ( ); return ( setNotificationSettings('focused')} + onSelect={onFocusedSelected} /> ); - }, [isFocusedSelected, notificationIconStyle]); + }, [isFocusedSelected, notificationIconStyle, onFocusedSelected]); const isFocusedBadgeOnlySelected = notificationSettings === 'badge-only'; const focusedBadgeOnlyItem = React.useMemo(() => { const description = [ ['Banner notifs', false], ['Badge count', true], ['Lives in Focused tab', true], ]; const icon = ( ); return ( setNotificationSettings('badge-only')} + onSelect={onBadgeOnlySelected} /> ); - }, [isFocusedBadgeOnlySelected, notificationIconStyle]); + }, [isFocusedBadgeOnlySelected, notificationIconStyle, onBadgeOnlySelected]); const isBackgroundSelected = notificationSettings === 'background'; const backgroundItem = React.useMemo(() => { const description = [ ['Banner notifs', false], ['Badge count', false], ['Lives in Backgound tab', true], ]; const icon = ( ); return ( setNotificationSettings('background')} + onSelect={onBackgroundSelected} /> ); - }, [isBackgroundSelected, notificationIconStyle]); + }, [isBackgroundSelected, notificationIconStyle, onBackgroundSelected]); const dispatchActionPromise = useDispatchActionPromise(); const callUpdateSubscription = useServerCall(updateSubscription); const onClickSave = React.useCallback(() => { dispatchActionPromise( updateSubscriptionActionTypes, callUpdateSubscription({ threadID: threadID, updatedFields: { home: notificationSettings !== 'background', pushNotifs: notificationSettings === 'focused', }, }), ); onClose(); }, [ callUpdateSubscription, dispatchActionPromise, notificationSettings, onClose, threadID, ]); return (
{focusedItem} {focusedBadgeOnlyItem} {backgroundItem}
); } export default NotificationsModal;