diff --git a/backend/src/sockets/index.js b/backend/src/sockets/index.js index 1eb4d4c..c5a9242 100644 --- a/backend/src/sockets/index.js +++ b/backend/src/sockets/index.js @@ -69,20 +69,24 @@ export function getIO() { export const socketEvents = { // Emit to specific user emitToUser(userId, event, data) { + console.log(`🔔 Emitting ${event} to user:${userId}`); io.to(`user:${userId}`).emit(event, data); }, // Emit to all users in a challenge emitToChallenge(challengeId, event, data) { + console.log(`🔔 Emitting ${event} to challenge:${challengeId}`); io.to(`challenge:${challengeId}`).emit(event, data); }, // Prediction events predictionCreated(challengeId, prediction) { + console.log(`📤 Emitting prediction:created for challenge ${challengeId}`, prediction); this.emitToChallenge(challengeId, 'prediction:created', prediction); }, predictionValidated(challengeId, prediction) { + console.log(`📤 Emitting prediction:validated for challenge ${challengeId}`, prediction); this.emitToChallenge(challengeId, 'prediction:validated', prediction); }, diff --git a/frontend/src/pages/ChallengeDetail.jsx b/frontend/src/pages/ChallengeDetail.jsx index 9f60253..5c965ec 100644 --- a/frontend/src/pages/ChallengeDetail.jsx +++ b/frontend/src/pages/ChallengeDetail.jsx @@ -47,11 +47,23 @@ export default function ChallengeDetail() { if (!socket) return; const handlePredictionCreated = (prediction) => { - setPredictions(prev => [prediction, ...prev]); - toast.success(`New prediction from ${prediction.username}`); + console.log('📥 Received prediction:created event', prediction); + setPredictions(prev => { + // Avoid duplicates + if (prev.some(p => p.id === prediction.id)) { + return prev; + } + return [prediction, ...prev]; + }); + + // Don't show toast for your own predictions + if (prediction.user_id !== user.id) { + toast.success(`New prediction from ${prediction.username}`); + } }; const handlePredictionValidated = (prediction) => { + console.log('📥 Received prediction:validated event', prediction); setPredictions(prev => prev.map(p => p.id === prediction.id ? prediction : p) ); @@ -63,10 +75,13 @@ export default function ChallengeDetail() { ? '🎉 Your prediction was validated!' : '❌ Your prediction was invalidated' ); + } else { + toast(`${prediction.username}'s prediction ${prediction.status}`); } }; const handleInvitationResponse = (response) => { + console.log('📥 Received invitation response', response); if (response.status === 'accepted') { toast.success(`${response.username} joined the challenge!`); loadChallenge(); // Refresh participant list @@ -82,7 +97,7 @@ export default function ChallengeDetail() { socket.off('prediction:validated', handlePredictionValidated); socket.off('challenge:invitation_response', handleInvitationResponse); }; - }, [socket, user]); + }, [socket, user.id, loadLeaderboard, loadChallenge]); const loadChallenge = async () => { try {