Allow the NotificationToggle to be in sync with the selected option in the browser

This commit is contained in:
Deluan
2020-11-21 01:24:09 -05:00
committed by Deluan Quintão
parent 14525cd056
commit a288e7e858
2 changed files with 54 additions and 35 deletions
+2 -1
View File
@@ -263,7 +263,8 @@
"noPlaylistsAvailable": "None available", "noPlaylistsAvailable": "None available",
"delete_user_title": "Delete user '%{name}'", "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)?", "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": { "menu": {
"library": "Library", "library": "Library",
+52 -34
View File
@@ -1,15 +1,20 @@
import React from 'react' import React from 'react'
import { useDispatch, useSelector } from 'react-redux' import { useDispatch, useSelector } from 'react-redux'
import { Card } from '@material-ui/core' import {
Card,
FormControl,
FormHelperText,
FormControlLabel,
Switch,
} from '@material-ui/core'
import { import {
SelectInput, SelectInput,
SimpleForm, SimpleForm,
Title, Title,
useLocale, useLocale,
useNotify,
useSetLocale, useSetLocale,
useTranslate, useTranslate,
BooleanInput,
useNotify,
} from 'react-admin' } from 'react-admin'
import { makeStyles } from '@material-ui/core/styles' import { makeStyles } from '@material-ui/core/styles'
import HelpOutlineIcon from '@material-ui/icons/HelpOutline' import HelpOutlineIcon from '@material-ui/icons/HelpOutline'
@@ -121,45 +126,58 @@ const SelectDefaultView = (props) => {
) )
} }
const NotificationsToggle = (props) => { const NotificationsToggle = () => {
const translate = useTranslate() const translate = useTranslate()
const dispatch = useDispatch() const dispatch = useDispatch()
const notify = useNotify() const notify = useNotify()
const currentSetting = useSelector((state) => state.settings.notifications) const currentSetting = useSelector((state) => state.settings.notifications)
const current = (() => { const notAvailable = !('Notification' in window)
if (!('Notification' in window) || Notification.permission !== 'granted') {
return false 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 ( return (
<BooleanInput <FormControl>
{...props} <FormControlLabel
source="notifications" control={
label={translate('menu.personal.options.desktop_notifications')} <Switch
defaultValue={current} id={'notifications'}
onChange={async (notificationsEnabled) => { color="primary"
if (notificationsEnabled) { checked={currentSetting}
if ( disabled={notAvailable}
!('Notification' in window) || onChange={toggleNotifications}
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={
}} <span>
/> {translate('menu.personal.options.desktop_notifications')}
</span>
}
/>
{notAvailable && (
<FormHelperText id="notifications-disabled-helper-text">
{translate('message.notifications_not_available')}
</FormHelperText>
)}
</FormControl>
) )
} }