Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIXES full screen leaderboard out of order when webhook returns new data #757

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions client/src/pages/ScuntLeaderboard/ScuntLeaderboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,32 @@ const ScuntLeaderboard = () => {
socket.emit('getScores');
});
socket.on('scores', (scores) => {
setLeaderboard(
scores.map((team) => {
if (team.points < 0) {
team.points = 0;
}
return team;
}),
);
// Sort the scores before setting the leaderboard state
const sortedScores = scores.map((team) => ({
...team,
points: team.points < 0 ? 0 : team.points
})).sort((a, b) => b.points - a.points); // Sort by points in descending order

setLeaderboard(sortedScores);
});
socket.on('update', (teamNumber, points) => {
setLeaderboard((prevLeaderboard) => {
return prevLeaderboard.map((team) => {
if (team.number === teamNumber) {
team.points = points < 0 ? 0 : points;
}
return team;
});
const updatedLeaderboard = prevLeaderboard.map((team) => ({
...team,
points: team.number === teamNumber ? Math.max(points, 0) : team.points
}));
// Sort again after updating
return updatedLeaderboard.sort((a, b) => b.points - a.points);
});
});

return () => {
socket.off('connect');
socket.off('scores');
socket.disconnect();
};
}, []);


useEffect(() => {
if (scuntSettings) {
Expand Down Expand Up @@ -130,17 +130,17 @@ const ScuntLeaderboardShow = ({ leaderboard }) => {
</h2>

<FullScreen handle={handle}>
<ScuntLeaderboardFullScreen arr={computedLeaderboard} />
<ScuntLeaderboardFullScreen arr={leaderboard} />
</FullScreen>

<div className="display-only-desktop">
<div className="scunt-leaderboard">
<Button style={buttonStyle} label="View Fullscreen" onClick={handle.enter} />
</div>
<ScuntLeaderboardDesktop arr={computedLeaderboard} />
<ScuntLeaderboardDesktop arr={leaderboard} />
</div>
<div className="display-only-tablet">
<ScuntLeaderboardMobile arr={computedLeaderboard} />
<ScuntLeaderboardMobile arr={leaderboard} />
</div>
</>
);
Expand Down Expand Up @@ -192,7 +192,6 @@ const ScuntLeaderboardDesktop = ({ arr }) => {
};

const ScuntLeaderboardMobile = ({ arr }) => {
arr.sort((a, b) => b.computedPoints - a.computedPoints);

return (
<div className="leaderboard-page-mobile">
Expand Down