From a288e7e8584b2a5e50ef4f7110dc38069d60ef0e Mon Sep 17 00:00:00 2001 From: Deluan Date: Sat, 21 Nov 2020 01:24:09 -0500 Subject: [PATCH] Allow the NotificationToggle to be in sync with the selected option in the browser --- ui/src/i18n/en.json | 3 +- ui/src/personal/Personal.js | 86 ++++++++++++++++++++++--------------- 2 files changed, 54 insertions(+), 35 deletions(-) diff --git a/ui/src/i18n/en.json b/ui/src/i18n/en.json index 4bda1c13..2ac1cb4f 100644 --- a/ui/src/i18n/en.json +++ b/ui/src/i18n/en.json @@ -263,7 +263,8 @@ "noPlaylistsAvailable": "None available", "delete_user_title": "Delete user '%{name}'", "delete_user_content": "Are you sure you want to delete this user and all their data (including playlists and preferences)?", - "notifications_blocked": "You have blocked Notifications for this site in your browser's settings or your browser does not support notifications." + "notifications_blocked": "You have blocked Notifications for this site in your browser's settings", + "notifications_not_available": "This browser does not support desktop notifications" }, "menu": { "library": "Library", diff --git a/ui/src/personal/Personal.js b/ui/src/personal/Personal.js index 3d560252..242d02f0 100644 --- a/ui/src/personal/Personal.js +++ b/ui/src/personal/Personal.js @@ -1,15 +1,20 @@ import React from 'react' import { useDispatch, useSelector } from 'react-redux' -import { Card } from '@material-ui/core' +import { + Card, + FormControl, + FormHelperText, + FormControlLabel, + Switch, +} from '@material-ui/core' import { SelectInput, SimpleForm, Title, useLocale, + useNotify, useSetLocale, useTranslate, - BooleanInput, - useNotify, } from 'react-admin' import { makeStyles } from '@material-ui/core/styles' import HelpOutlineIcon from '@material-ui/icons/HelpOutline' @@ -121,45 +126,58 @@ const SelectDefaultView = (props) => { ) } -const NotificationsToggle = (props) => { +const NotificationsToggle = () => { const translate = useTranslate() const dispatch = useDispatch() const notify = useNotify() const currentSetting = useSelector((state) => state.settings.notifications) - const current = (() => { - if (!('Notification' in window) || Notification.permission !== 'granted') { - return false + const notAvailable = !('Notification' in window) + + if ( + (currentSetting && Notification.permission !== 'granted') || + notAvailable + ) { + dispatch(setNotificationsState(false)) + } + + const toggleNotifications = (event) => { + if (currentSetting && !event.target.checked) { + dispatch(setNotificationsState(false)) + } else { + if (Notification.permission === 'denied') { + notify(translate('message.notifications_blocked'), 'warning') + } else { + Notification.requestPermission().then((permission) => { + dispatch(setNotificationsState(permission === 'granted')) + }) + } } - return currentSetting - })() + } return ( - { - if (notificationsEnabled) { - if ( - !('Notification' in window) || - Notification.permission === 'denied' - ) { - notify(translate('message.notifications_blocked'), 'warning') - notificationsEnabled = false - } else { - const response = await Notification.requestPermission() - if (response !== 'granted') { - notificationsEnabled = false - } - } - if (!notificationsEnabled) { - // Need to turn switch off - } + + } - dispatch(setNotificationsState(notificationsEnabled)) - }} - /> + label={ + + {translate('menu.personal.options.desktop_notifications')} + + } + /> + {notAvailable && ( + + {translate('message.notifications_not_available')} + + )} + ) }