Skip to content

Commit 890e423

Browse files
authored
Merge pull request #30 from ProLoser/fix/cleanup-styles-logs
Fix/cleanup styles logs
2 parents 72767d3 + 97305ca commit 890e423

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

src/Dialogues/Profile.tsx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// Import FirebaseAuth and firebase.
2-
import { useState, useCallback, ChangeEvent } from 'react';
2+
import { useState, useCallback, ChangeEvent, useEffect } from 'react';
33
import firebase from 'firebase/compat/app';
44
import 'firebase/compat/auth';
55
import 'firebase/compat/database';
66
import Avatar from '../Avatar';
77
import type { UserData } from '../Types';
88
import './Profile.css'
9+
import { saveMessagingDeviceToken } from '../firebase-messaging-setup';
910

1011
export 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

112113
export 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
}

src/firebase-messaging-setup.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const auth = firebaseApp.auth(); // Or firebase.auth()
2727

2828

2929
// Get the FCM registration token and save it to the database
30-
async function saveMessagingDeviceToken() {
30+
export async function saveMessagingDeviceToken() {
3131
const userId = auth.currentUser?.uid;
3232
if (!userId) {
3333
console.log('User not logged in, cannot save token.');
@@ -72,14 +72,3 @@ messaging.onMessage((payload) => {
7272
});
7373

7474
// see public/firebase-messaging-sw.js
75-
76-
// Call this function when the user logs in or perhaps when the app loads if they are already logged in
77-
auth.onAuthStateChanged((user) => { // Using compat auth instance
78-
if (user) {
79-
saveMessagingDeviceToken();
80-
} else {
81-
// User is signed out
82-
console.log('User signed out, token not needed or remove token from DB');
83-
// Optional: remove the token from the database if the user signs out
84-
}
85-
});

src/index.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import './Board/Board.css';
1818
import './Toolbar.css'
1919
import { calculate, newGame, rollDie, vibrate } from './Utils';
2020
import { registerSW } from 'virtual:pwa-register';
21+
import { saveMessagingDeviceToken } from './firebase-messaging-setup';
2122

2223

2324

@@ -43,6 +44,7 @@ export function App() {
4344
const database = firebase.database();
4445
const [game, setGame] = useState<GameType>(newGame);
4546
const [user, setUser] = useState<SnapshotOrNullType>(null);
47+
const [hasAttemptedNotificationPermission, setHasAttemptedNotificationPermission] = useState(false);
4648
const [state, setState] = useState<ModalState>(false);
4749
const [lastState, setLastState] = useState<ModalState>('friends');
4850
const [match, setMatch] = useState<Match | null>(null);
@@ -157,11 +159,29 @@ export function App() {
157159
} else {
158160
setUser(null);
159161
setMatch(null);
162+
setHasAttemptedNotificationPermission(false);
160163
}
161164
});
162165
return () => unregisterAuthObserver();
163166
}, []);
164167

168+
useEffect(() => {
169+
const requestPermission = async () => {
170+
if (state === 'friends' && user && Notification.permission === 'default' && !hasAttemptedNotificationPermission) {
171+
console.log("Friends modal opened, attempting notification permission request via useEffect..."); // Dev log
172+
try {
173+
await saveMessagingDeviceToken();
174+
} catch (error) {
175+
console.error("Error requesting notification permission from useEffect:", error); // Dev log
176+
} finally {
177+
setHasAttemptedNotificationPermission(true);
178+
}
179+
}
180+
};
181+
182+
requestPermission();
183+
}, [state, user, hasAttemptedNotificationPermission]);
184+
165185
// Subscribe to match
166186
useEffect(() => {
167187
if (match?.game) {

0 commit comments

Comments
 (0)