11// Import FirebaseAuth and firebase.
2- import { useState , useCallback , ChangeEvent } from 'react' ;
2+ import { useState , useCallback , ChangeEvent , useEffect } from 'react' ;
33import firebase from 'firebase/compat/app' ;
44import 'firebase/compat/auth' ;
55import 'firebase/compat/database' ;
66import Avatar from '../Avatar' ;
77import type { UserData } from '../Types' ;
88import './Profile.css'
9+ import { saveMessagingDeviceToken } from '../firebase-messaging-setup' ;
910
1011export const LANGUAGES = [ "af" , "af-NA" , "af-ZA" , "agq" , "agq-CM" , "ak" , "ak-GH" , "am" ,
1112 "am-ET" , "ar" , "ar-001" , "ar-AE" , "ar-BH" , "ar-DJ" , "ar-DZ" ,
@@ -111,6 +112,7 @@ export const LANGUAGES = ["af", "af-NA", "af-ZA", "agq", "agq-CM", "ak", "ak-GH"
111112
112113export default function Profile ( { authUser, toggle } ) {
113114 const [ editing , setEditing ] = useState < UserData > ( authUser ?. val ( ) || { uid : '' , name : '' , language : '' , photoURL : '' } ) ;
115+ const [ currentNotificationPermission , setCurrentNotificationPermission ] = useState ( Notification . permission ) ;
114116
115117 const save = useCallback ( async ( event : React . FormEvent < HTMLFormElement > ) => {
116118 event . preventDefault ( ) ;
@@ -125,6 +127,15 @@ export default function Profile({ authUser, toggle }) {
125127 setEditing ( editing => ( { ...editing , [ key ] : event . target . value } ) ) ;
126128 } ;
127129
130+ const handleEnableNotificationsClick = useCallback ( async ( ) => {
131+ try {
132+ await saveMessagingDeviceToken ( ) ;
133+ } catch ( error )
134+ console . error ( "Error requesting notification permission from profile:" , error ) ;
135+ }
136+ setCurrentNotificationPermission ( Notification . permission ) ;
137+ } , [ ] ) ;
138+
128139 return < section id = "profile" >
129140 < form onSubmit = { save } >
130141 < header >
@@ -153,6 +164,18 @@ export default function Profile({ authUser, toggle }) {
153164 < input type = "text" name = "photoURL" value = { editing . photoURL || '' } onChange = { generateOnChange ( 'photoURL' ) } placeholder = "Photo URL" />
154165 </ label >
155166 < Avatar user = { editing } />
167+
168+ { ( currentNotificationPermission === 'default' || currentNotificationPermission === 'denied' ) && (
169+ < >
170+ < p > Enable notifications to stay updated on game events.</ p >
171+ < button type = "button" onClick = { handleEnableNotificationsClick } >
172+ Enable Notifications
173+ </ button >
174+ </ >
175+ ) }
176+ { currentNotificationPermission === 'granted' && (
177+ < p > Notifications are enabled.</ p >
178+ ) }
156179 </ form >
157180 </ section >
158181}
0 commit comments