@ -7,6 +7,10 @@ import useSettings from '@app/hooks/useSettings';
import { useUser } from '@app/hooks/useUser' ;
import { useUser } from '@app/hooks/useUser' ;
import globalMessages from '@app/i18n/globalMessages' ;
import globalMessages from '@app/i18n/globalMessages' ;
import { ArrowDownOnSquareIcon } from '@heroicons/react/24/outline' ;
import { ArrowDownOnSquareIcon } from '@heroicons/react/24/outline' ;
import {
CloudArrowDownIcon ,
CloudArrowUpIcon ,
} from '@heroicons/react/24/solid' ;
import type { UserSettingsNotificationsResponse } from '@server/interfaces/api/userSettingsInterfaces' ;
import type { UserSettingsNotificationsResponse } from '@server/interfaces/api/userSettingsInterfaces' ;
import axios from 'axios' ;
import axios from 'axios' ;
import { Form , Formik } from 'formik' ;
import { Form , Formik } from 'formik' ;
@ -19,6 +23,8 @@ import useSWR, { mutate } from 'swr';
const messages = defineMessages ( {
const messages = defineMessages ( {
webpushsettingssaved : 'Web push notification settings saved successfully!' ,
webpushsettingssaved : 'Web push notification settings saved successfully!' ,
webpushsettingsfailed : 'Web push notification settings failed to save.' ,
webpushsettingsfailed : 'Web push notification settings failed to save.' ,
enablewebpush : 'Enable web push' ,
disablewebpush : 'Disable web push' ,
} ) ;
} ) ;
const UserWebPushSettings = ( ) = > {
const UserWebPushSettings = ( ) = > {
@ -27,6 +33,7 @@ const UserWebPushSettings = () => {
const router = useRouter ( ) ;
const router = useRouter ( ) ;
const { user } = useUser ( { id : Number ( router . query . userId ) } ) ;
const { user } = useUser ( { id : Number ( router . query . userId ) } ) ;
const { currentSettings } = useSettings ( ) ;
const { currentSettings } = useSettings ( ) ;
const [ webPushEnabled , setWebPushEnabled ] = useState ( false ) ;
const {
const {
data ,
data ,
error ,
error ,
@ -34,8 +41,9 @@ const UserWebPushSettings = () => {
} = useSWR < UserSettingsNotificationsResponse > (
} = useSWR < UserSettingsNotificationsResponse > (
user ? ` /api/v1/user/ ${ user ? . id } /settings/notifications ` : null
user ? ` /api/v1/user/ ${ user ? . id } /settings/notifications ` : null
) ;
) ;
const [ webPushEnabled , setWebPushEnabled ] = useState ( false ) ;
// Subscribes to the push manager
// Will only add to the database if subscribing for the first time
const enablePushNotifications = ( ) = > {
const enablePushNotifications = ( ) = > {
if ( 'serviceWorker' in navigator && user ? . id ) {
if ( 'serviceWorker' in navigator && user ? . id ) {
navigator . serviceWorker
navigator . serviceWorker
@ -67,6 +75,7 @@ const UserWebPushSettings = () => {
}
}
} )
} )
. catch ( function ( error ) {
. catch ( function ( error ) {
// eslint-disable-next-line no-console
console . log (
console . log (
'[SW] Failure subscribing to push manager, error:' ,
'[SW] Failure subscribing to push manager, error:' ,
error
error
@ -75,6 +84,7 @@ const UserWebPushSettings = () => {
}
}
} ;
} ;
// Unsubscribes to the push manager
const disablePushNotifications = ( ) = > {
const disablePushNotifications = ( ) = > {
if ( 'serviceWorker' in navigator && user ? . id ) {
if ( 'serviceWorker' in navigator && user ? . id ) {
navigator . serviceWorker . getRegistration ( '/sw.js' ) . then ( ( registration ) = > {
navigator . serviceWorker . getRegistration ( '/sw.js' ) . then ( ( registration ) = > {
@ -85,6 +95,7 @@ const UserWebPushSettings = () => {
? . unsubscribe ( )
? . unsubscribe ( )
. then ( ( ) = > setWebPushEnabled ( false ) )
. then ( ( ) = > setWebPushEnabled ( false ) )
. catch ( function ( error ) {
. catch ( function ( error ) {
// eslint-disable-next-line no-console
console . log (
console . log (
'[SW] Failure unsubscribing to push manager, error:' ,
'[SW] Failure unsubscribing to push manager, error:' ,
error
error
@ -95,6 +106,8 @@ const UserWebPushSettings = () => {
}
}
} ;
} ;
// Checks our current subscription on page load
// Will set the web push state to true if subscribed
useEffect ( ( ) = > {
useEffect ( ( ) = > {
if ( 'serviceWorker' in navigator && user ? . id ) {
if ( 'serviceWorker' in navigator && user ? . id ) {
navigator . serviceWorker
navigator . serviceWorker
@ -105,6 +118,13 @@ const UserWebPushSettings = () => {
setWebPushEnabled ( true ) ;
setWebPushEnabled ( true ) ;
}
}
} ) ;
} ) ;
} )
. catch ( function ( error ) {
// eslint-disable-next-line no-console
console . log (
'[SW] Failure retrieving push manager subscription, error:' ,
error
) ;
} ) ;
} ) ;
}
}
} , [ user ? . id ] ) ;
} , [ user ? . id ] ) ;
@ -158,64 +178,63 @@ const UserWebPushSettings = () => {
setFieldTouched ,
setFieldTouched ,
} ) = > {
} ) = > {
return (
return (
< >
< Form className = "section" >
{ webPushEnabled ? (
{ webPushEnabled && (
< Form className = "section" >
< NotificationTypeSelector
< NotificationTypeSelector
user = { user }
user = { user }
currentTypes = { values . types }
currentTypes = { values . types }
onUpdate = { ( newTypes ) = > {
onUpdate = { ( newTypes ) = > {
setFieldValue ( 'types' , newTypes ) ;
setFieldValue ( 'types' , newTypes ) ;
setFieldTouched ( 'types' ) ;
setFieldTouched ( 'types' ) ;
} }
} }
error = {
error = {
errors . types && touched . types
errors . types && touched . types
? ( errors . types as string )
? ( errors . types as string )
: undefined
: undefined
}
}
/ >
/ >
) }
< div className = "actions" >
< div className = "actions" >
< div className = "flex justify-end" >
< div className = "flex justify-end" >
< span className = "ml-3 inline-flex rounded-md shadow-sm" >
< span className = "ml-3 inline-flex rounded-md shadow-sm" >
< Button
< Button
buttonType = "primary"
buttonType = { ` ${ webPushEnabled ? 'danger' : 'warning' } ` }
type = "submit"
type = "button"
disabled = { isSubmitting || ! isValid }
onClick = { ( ) = >
>
webPushEnabled
< ArrowDownOnSquareIcon / >
? disablePushNotifications ( )
< span >
: enablePushNotifications ( )
{ isSubmitting
}
? intl . formatMessage ( globalMessages . saving )
>
: intl . formatMessage ( globalMessages . save ) }
{ webPushEnabled ? (
< / span >
< CloudArrowDownIcon / >
< / Button >
) : (
< CloudArrowUpIcon / >
) }
< span >
{ webPushEnabled
? intl . formatMessage ( messages . disablewebpush )
: intl . formatMessage ( messages . enablewebpush ) }
< / span >
< / span >
< / div >
< / Button >
< / div >
< / span >
< / Form >
< span className = "ml-3 inline-flex rounded-md shadow-sm" >
) : (
< Button
< div className = "actions" >
buttonType = "primary"
< div className = "flex justify-end" >
type = "submit"
< span className = "ml-3 inline-flex rounded-md shadow-sm" >
disabled = { isSubmitting || ! isValid }
< Button
>
buttonType = "primary"
< ArrowDownOnSquareIcon / >
onClick = { ( ) = > enablePushNotifications ( ) }
< span >
>
{ isSubmitting
< ArrowDownOnSquareIcon / >
? intl . formatMessage ( globalMessages . saving )
< span > Enable Web Push < / span >
: intl . formatMessage ( globalMessages . save ) }
< / Butto n>
< / spa n>
< / spa n>
< / Butto n>
< / div >
< / span >
< / div >
< / div >
) }
< / div >
< Button
< / Form >
buttonType = "primary"
onClick = { ( ) = > disablePushNotifications ( ) }
>
< ArrowDownOnSquareIcon / >
< span > Disable Web Push < / span >
< / Button >
< / >
) ;
) ;
} }
} }
< / Formik >
< / Formik >