From f1e6b5955ee16f4ec65828f6bd48930b606b879e Mon Sep 17 00:00:00 2001 From: tykayn Date: Wed, 28 Apr 2021 12:15:49 +0200 Subject: [PATCH] set choices and toggle votes in votestack --- src/app/core/models/vote.model.ts | 6 +++++ src/app/core/services/poll.service.ts | 4 ++++ src/app/core/services/storage.service.ts | 22 +++++++++++++++++++ .../consultation/consultation.component.html | 6 +++-- .../poll-results-compact.component.html | 2 +- .../poll-results-compact.component.ts | 7 +++++- 6 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/app/core/models/vote.model.ts b/src/app/core/models/vote.model.ts index 2fab385b..5adb2969 100644 --- a/src/app/core/models/vote.model.ts +++ b/src/app/core/models/vote.model.ts @@ -1,4 +1,10 @@ export class Vote { public choice_id: number; public value: string; // valeur de réponse + + constructor(choice_id?) { + if (choice_id) { + this.choice_id = choice_id; + } + } } diff --git a/src/app/core/services/poll.service.ts b/src/app/core/services/poll.service.ts index 10b6aa42..d8dec5e1 100644 --- a/src/app/core/services/poll.service.ts +++ b/src/app/core/services/poll.service.ts @@ -12,6 +12,7 @@ import { UserService } from './user.service'; import { UuidService } from './uuid.service'; import { HttpClient } from '@angular/common/http'; import { environment } from '../../../environments/environment'; +import { StorageService } from './storage.service'; @Injectable({ providedIn: 'root', @@ -24,6 +25,7 @@ export class PollService implements Resolve { private http: HttpClient, private router: Router, private apiService: ApiService, + private storageService: StorageService, private userService: UserService, private uuidService: UuidService, private toastService: ToastService @@ -86,6 +88,8 @@ export class PollService implements Resolve { } public updateCurrentPoll(poll: Poll): void { + this.storageService.setChoicesForVoteStack(poll.choices); + console.log('poll', poll); this._poll.next(poll); } diff --git a/src/app/core/services/storage.service.ts b/src/app/core/services/storage.service.ts index 0fc20dd6..a8013efb 100644 --- a/src/app/core/services/storage.service.ts +++ b/src/app/core/services/storage.service.ts @@ -4,6 +4,8 @@ import { LocalStorage } from 'ngx-webstorage'; import { Language } from '../enums/language.enum'; import { Theme } from '../enums/theme.enum'; import { Stack } from '../models/stack.model'; +import { Choice } from '../models/choice.model'; +import { Vote } from '../models/vote.model'; @Injectable({ providedIn: 'root', @@ -20,4 +22,24 @@ export class StorageService { @LocalStorage() public vote_stack: Stack = new Stack(); + + setChoicesForVoteStack(choices_list: Choice[]) { + this.vote_stack.votes = []; + + for (let choice of choices_list) { + this.vote_stack.votes.push(new Vote(choice.id)); + } + } + + toggleAnswer(choice_id: number, value: string) { + for (let vote of this.vote_stack.votes) { + if (vote.choice_id == choice_id) { + if (vote.value) { + vote.value = ''; + } else { + vote.value = value; + } + } + } + } } diff --git a/src/app/features/consultation/consultation.component.html b/src/app/features/consultation/consultation.component.html index 8da92428..56dcac16 100644 --- a/src/app/features/consultation/consultation.component.html +++ b/src/app/features/consultation/consultation.component.html @@ -1,7 +1,9 @@
- - mon pseudo : {{ storageService.vote_stack.pseudo }} +
+ + debug: {{ storageService.vote_stack | json }} +
diff --git a/src/app/features/consultation/poll-results-compact/poll-results-compact.component.html b/src/app/features/consultation/poll-results-compact/poll-results-compact.component.html index 313fd2ab..56df03f1 100644 --- a/src/app/features/consultation/poll-results-compact/poll-results-compact.component.html +++ b/src/app/features/consultation/poll-results-compact/poll-results-compact.component.html @@ -24,7 +24,7 @@
-