11import { Component , OnInit } from '@angular/core' ;
2- import { StorageService } from "../../service/storage.service" ;
32import { Router } from "@angular/router" ;
43import { PathsEnum } from "../../model/enums/PathsEnum" ;
5- import { AppStorage , WeekScore } from "../../model/AppStorage" ;
6- import * as moment from "moment" ;
4+ import { WeekScore } from "../../model/AppStorage" ;
5+ import moment from "moment" ;
76import { TemplateService } from "../../service/template.service" ;
87import { TemplateEnum , WeekScoreTemplateParams } from "../../model/enums/Template" ;
8+ import { AppStorageService } from "../../service/app-storage.service" ;
99
1010@Component ( {
1111 selector : 'app-score-window' ,
@@ -17,31 +17,19 @@ export class ScoreWindowComponent implements OnInit {
1717 public currentWeek : number = 0 ;
1818 public rightAnswers : number = 0 ;
1919 public wrongAnswers : number = 0 ;
20+ public previousScores : Map < number , WeekScore > | null = null ;
2021 public clipboardText : string = '' ;
2122 public displayClipboardMessage : boolean = false ;
2223
2324 constructor (
2425 private readonly router : Router ,
25- private readonly storageService : StorageService ,
26- private readonly templateService : TemplateService
26+ private readonly templateService : TemplateService ,
27+ private readonly appStorageService : AppStorageService
2728 ) {
2829 }
2930
3031 public async ngOnInit ( ) : Promise < void > {
31- const currentWeek : number = moment ( ) . isoWeek ( ) ;
32- const appStorage : AppStorage = this . storageService . get ( ) ;
33- const currentWeekScoreMap : Map < number , WeekScore > | undefined = appStorage . weekScoreMap ;
34-
35- this . currentWeek = currentWeek ;
36-
37- if ( currentWeekScoreMap === undefined ) return ;
38-
39- const currentWeekScore : WeekScore = currentWeekScoreMap . get ( currentWeek ) ! ;
40-
41- this . currentScore = currentWeekScore . score ;
42- this . rightAnswers = currentWeekScore . rightAnswers ;
43- this . wrongAnswers = currentWeekScore . wrongAnswers ;
44-
32+ this . retrieveScore ( ) ;
4533 await this . assembleClipboardText ( ) ;
4634 }
4735
@@ -67,4 +55,18 @@ export class ScoreWindowComponent implements OnInit {
6755
6856 this . clipboardText = await this . templateService . render ( TemplateEnum . WEEK_SCORE , templateParams ) ;
6957 }
58+
59+ private retrieveScore ( ) : void {
60+ const currentWeek : number = moment ( ) . isoWeek ( ) ;
61+ const currentWeekScore : WeekScore = this . appStorageService . retrieveScoreByWeek ( currentWeek ) ;
62+ const previousScoresMap : Map < number , WeekScore > = this . appStorageService . retrieveAppStorage ( ) . weekScoreMap ! ;
63+
64+ previousScoresMap . delete ( currentWeek ) ;
65+
66+ this . previousScores = previousScoresMap ;
67+ this . currentScore = currentWeekScore . score ;
68+ this . rightAnswers = currentWeekScore . rightAnswers ;
69+ this . wrongAnswers = currentWeekScore . wrongAnswers ;
70+ this . currentWeek = currentWeek ;
71+ }
7072}
0 commit comments