This commit is contained in:
2026-01-29 02:24:55 -05:00
parent b567fda0ae
commit f262abff16
2 changed files with 22 additions and 3 deletions

View File

@@ -69,20 +69,24 @@ export function getIO() {
export const socketEvents = { export const socketEvents = {
// Emit to specific user // Emit to specific user
emitToUser(userId, event, data) { emitToUser(userId, event, data) {
console.log(`🔔 Emitting ${event} to user:${userId}`);
io.to(`user:${userId}`).emit(event, data); io.to(`user:${userId}`).emit(event, data);
}, },
// Emit to all users in a challenge // Emit to all users in a challenge
emitToChallenge(challengeId, event, data) { emitToChallenge(challengeId, event, data) {
console.log(`🔔 Emitting ${event} to challenge:${challengeId}`);
io.to(`challenge:${challengeId}`).emit(event, data); io.to(`challenge:${challengeId}`).emit(event, data);
}, },
// Prediction events // Prediction events
predictionCreated(challengeId, prediction) { predictionCreated(challengeId, prediction) {
console.log(`📤 Emitting prediction:created for challenge ${challengeId}`, prediction);
this.emitToChallenge(challengeId, 'prediction:created', prediction); this.emitToChallenge(challengeId, 'prediction:created', prediction);
}, },
predictionValidated(challengeId, prediction) { predictionValidated(challengeId, prediction) {
console.log(`📤 Emitting prediction:validated for challenge ${challengeId}`, prediction);
this.emitToChallenge(challengeId, 'prediction:validated', prediction); this.emitToChallenge(challengeId, 'prediction:validated', prediction);
}, },

View File

@@ -47,11 +47,23 @@ export default function ChallengeDetail() {
if (!socket) return; if (!socket) return;
const handlePredictionCreated = (prediction) => { const handlePredictionCreated = (prediction) => {
setPredictions(prev => [prediction, ...prev]); 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}`); toast.success(`New prediction from ${prediction.username}`);
}
}; };
const handlePredictionValidated = (prediction) => { const handlePredictionValidated = (prediction) => {
console.log('📥 Received prediction:validated event', prediction);
setPredictions(prev => setPredictions(prev =>
prev.map(p => p.id === prediction.id ? prediction : p) prev.map(p => p.id === prediction.id ? prediction : p)
); );
@@ -63,10 +75,13 @@ export default function ChallengeDetail() {
? '🎉 Your prediction was validated!' ? '🎉 Your prediction was validated!'
: '❌ Your prediction was invalidated' : '❌ Your prediction was invalidated'
); );
} else {
toast(`${prediction.username}'s prediction ${prediction.status}`);
} }
}; };
const handleInvitationResponse = (response) => { const handleInvitationResponse = (response) => {
console.log('📥 Received invitation response', response);
if (response.status === 'accepted') { if (response.status === 'accepted') {
toast.success(`${response.username} joined the challenge!`); toast.success(`${response.username} joined the challenge!`);
loadChallenge(); // Refresh participant list loadChallenge(); // Refresh participant list
@@ -82,7 +97,7 @@ export default function ChallengeDetail() {
socket.off('prediction:validated', handlePredictionValidated); socket.off('prediction:validated', handlePredictionValidated);
socket.off('challenge:invitation_response', handleInvitationResponse); socket.off('challenge:invitation_response', handleInvitationResponse);
}; };
}, [socket, user]); }, [socket, user.id, loadLeaderboard, loadChallenge]);
const loadChallenge = async () => { const loadChallenge = async () => {
try { try {