@@ -11,8 +11,25 @@ const localStorage = window.localStorage;
11
11
12
12
13
13
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 ( {
16
33
id : currentId ,
17
34
maxPoints,
18
35
playersPerTeam,
@@ -22,10 +39,10 @@ function saveOnLocalStorage(isLive = true) {
22
39
isLive,
23
40
autoSwitchTeamsPoints,
24
41
playedOn : new Date ( ) . toLocaleDateString ( ) ,
25
- } ,
26
- ]
42
+ } ) ;
43
+ }
27
44
28
- localStorage . setItem ( LOCAL_STORAGE_KEY , JSON . stringify ( gameDays ) ) ;
45
+ localStorage . setItem ( LOCAL_STORAGE_KEY , JSON . stringify ( allGames ) ) ;
29
46
}
30
47
31
48
function getFromLocalStorage ( ) {
@@ -454,7 +471,7 @@ $("#show-historic").click(function() {
454
471
gameDays . forEach ( gameDay => {
455
472
$ ( "#historic-days" ) . append ( `
456
473
<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>
458
475
</div>` ) ;
459
476
} ) ;
460
477
} ) ;
0 commit comments