Skip to content

Commit 520ab05

Browse files
committed
Refactor restoreUserToRoom function to remove user from queue on rejoin
- Updated the restoreUserToRoom function to remove a user from the queue when they rejoin, ensuring they need to raise their hand again. - Set handRaised property to false upon rejoining, reflecting the user's new state. Signed-off-by: Pete Cheslock <[email protected]>
1 parent d455e04 commit 520ab05

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/services/firebase.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,23 +62,27 @@ export const userExistsInRoom = async (roomCode, userId) => {
6262
* @returns {Promise<void>}
6363
*/
6464
export const restoreUserToRoom = async (roomCode, userId, userName) => {
65-
// Check if user is currently in the queue to preserve their hand state
65+
// Remove user from queue if they're in it (they'll need to raise hand again)
6666
const queueRef = ref(database, `rooms/${roomCode}/queue`);
6767
const queueSnapshot = await get(queueRef);
68-
let handRaised = false;
6968

7069
if (queueSnapshot.exists()) {
7170
const queue = queueSnapshot.val();
72-
// Check if this user is in the queue
73-
const userInQueue = Object.values(queue).some(queueItem => queueItem.userId === userId);
74-
handRaised = userInQueue;
71+
// Find and remove this user from the queue
72+
for (const [queueId, queueItem] of Object.entries(queue)) {
73+
if (queueItem.userId === userId) {
74+
await remove(ref(database, `rooms/${roomCode}/queue/${queueId}`));
75+
break;
76+
}
77+
}
7578
}
7679

80+
// Always set hand to down on rejoin - user needs to raise again
7781
const userRef = ref(database, `rooms/${roomCode}/users/${userId}`);
7882
await set(userRef, {
7983
name: userName,
8084
joinedAt: serverTimestamp(),
81-
handRaised: handRaised
85+
handRaised: false
8286
});
8387
};
8488

0 commit comments

Comments
 (0)