Skip to content

Commit a9cfeb3

Browse files
correctly save matches on local storage
1 parent 44d5695 commit a9cfeb3

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,5 @@ <h1>Histórico de Partidas</h1>
117117
src="https://code.jquery.com/jquery-3.7.1.min.js"
118118
integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo="
119119
crossorigin="anonymous"></script>
120-
<script src="./index.js?v=5"></script>
120+
<script src="./index.js?v=6"></script>
121121
</html>

index.js

+23-6
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,25 @@ const localStorage = window.localStorage;
1111

1212

1313
function saveOnLocalStorage(isLive = true) {
14-
const gameDays = [
15-
{
14+
const allGames = getFromLocalStorage();
15+
16+
// Update current game day
17+
const currentGameDay = allGames.find(gameDay => gameDay.id === currentId);
18+
if (currentGameDay) {
19+
currentGameDay.maxPoints = maxPoints;
20+
currentGameDay.playersPerTeam = playersPerTeam;
21+
currentGameDay.players = players;
22+
currentGameDay.playingTeams = playingTeams;
23+
currentGameDay.matches = matches;
24+
currentGameDay.isLive = isLive;
25+
currentGameDay.autoSwitchTeamsPoints = autoSwitchTeamsPoints;
26+
currentGameDay.playedOn = new Date().toLocaleDateString();
27+
28+
// Update all games with new curret game day
29+
const currentGameIndex = allGames.findIndex(gameDay => gameDay.id === currentId);
30+
allGames[currentGameIndex] = currentGameDay;
31+
} else {
32+
allGames.push({
1633
id: currentId,
1734
maxPoints,
1835
playersPerTeam,
@@ -22,10 +39,10 @@ function saveOnLocalStorage(isLive = true) {
2239
isLive,
2340
autoSwitchTeamsPoints,
2441
playedOn: new Date().toLocaleDateString(),
25-
},
26-
]
42+
});
43+
}
2744

28-
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(gameDays));
45+
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(allGames));
2946
}
3047

3148
function getFromLocalStorage() {
@@ -454,7 +471,7 @@ $("#show-historic").click(function() {
454471
gameDays.forEach(gameDay => {
455472
$("#historic-days").append(`
456473
<div class='column match-historic' id='${gameDay.id}'>
457-
<button class='button is-large'>[${gameDay.id}] Jogo de ${gameDay.playedOn || new Date("2024-09-07")}</button>
474+
<button class='button is-large'>[${gameDay.id}] Jogo de ${gameDay.playedOn || new Date("2024-09-07").toLocaleString()}</button>
458475
</div>`);
459476
});
460477
});

0 commit comments

Comments
 (0)