set choices and toggle votes in votestack

This commit is contained in:
tykayn 2021-04-28 12:15:49 +02:00 committed by Baptiste Lemoine
parent 620a7b99fa
commit f1e6b5955e
6 changed files with 43 additions and 4 deletions

View File

@ -1,4 +1,10 @@
export class Vote { export class Vote {
public choice_id: number; public choice_id: number;
public value: string; // valeur de réponse public value: string; // valeur de réponse
constructor(choice_id?) {
if (choice_id) {
this.choice_id = choice_id;
}
}
} }

View File

@ -12,6 +12,7 @@ import { UserService } from './user.service';
import { UuidService } from './uuid.service'; import { UuidService } from './uuid.service';
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { environment } from '../../../environments/environment'; import { environment } from '../../../environments/environment';
import { StorageService } from './storage.service';
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
@ -24,6 +25,7 @@ export class PollService implements Resolve<Poll> {
private http: HttpClient, private http: HttpClient,
private router: Router, private router: Router,
private apiService: ApiService, private apiService: ApiService,
private storageService: StorageService,
private userService: UserService, private userService: UserService,
private uuidService: UuidService, private uuidService: UuidService,
private toastService: ToastService private toastService: ToastService
@ -86,6 +88,8 @@ export class PollService implements Resolve<Poll> {
} }
public updateCurrentPoll(poll: Poll): void { public updateCurrentPoll(poll: Poll): void {
this.storageService.setChoicesForVoteStack(poll.choices);
console.log('poll', poll); console.log('poll', poll);
this._poll.next(poll); this._poll.next(poll);
} }

View File

@ -4,6 +4,8 @@ import { LocalStorage } from 'ngx-webstorage';
import { Language } from '../enums/language.enum'; import { Language } from '../enums/language.enum';
import { Theme } from '../enums/theme.enum'; import { Theme } from '../enums/theme.enum';
import { Stack } from '../models/stack.model'; import { Stack } from '../models/stack.model';
import { Choice } from '../models/choice.model';
import { Vote } from '../models/vote.model';
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
@ -20,4 +22,24 @@ export class StorageService {
@LocalStorage() @LocalStorage()
public vote_stack: Stack = new Stack(); 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;
}
}
}
}
} }

View File

@ -1,7 +1,9 @@
<section class="loading_poll" *ngIf="fetching"></section> <section class="loading_poll" *ngIf="fetching"></section>
<section class="poll_loaded padded" *ngIf="!fetching && poll"> <section class="poll_loaded padded" *ngIf="!fetching && poll">
<div class="well debug">
<!-- infos locales storage--> <!-- infos locales storage-->
mon pseudo : {{ storageService.vote_stack.pseudo }} debug: {{ storageService.vote_stack | json }}
</div>
<!-- messages--> <!-- messages-->

View File

@ -24,7 +24,7 @@
</div> </div>
<div class="column is-narrow"> <div class="column is-narrow">
<div class="buttons has-addons is-right"> <div class="buttons has-addons is-right">
<button class="button is-white"> <button class="button is-white" (click)="toggleAnswer(choice.id, 'yes')">
<img class="image is-24x24" src="../../../assets/img/icon_voter_YES.svg" /> <img class="image is-24x24" src="../../../assets/img/icon_voter_YES.svg" />
<span class="counter" *ngIf="choice[answerEnum.YES].count * 1 > 0"> <span class="counter" *ngIf="choice[answerEnum.YES].count * 1 > 0">

View File

@ -6,6 +6,7 @@ import { Choice } from '../../../core/models/choice.model';
import { Poll } from '../../../core/models/poll.model'; import { Poll } from '../../../core/models/poll.model';
import { ModalService } from '../../../core/services/modal.service'; import { ModalService } from '../../../core/services/modal.service';
import { ChoiceDetailsComponent } from '../../../shared/components/choice-details/choice-details.component'; import { ChoiceDetailsComponent } from '../../../shared/components/choice-details/choice-details.component';
import { StorageService } from '../../../core/services/storage.service';
@Component({ @Component({
selector: 'app-poll-results-compact', selector: 'app-poll-results-compact',
@ -16,7 +17,7 @@ export class PollResultsCompactComponent implements OnInit {
@Input() public poll: Poll; @Input() public poll: Poll;
public answerEnum = Answer; public answerEnum = Answer;
constructor(private modalService: ModalService) {} constructor(private modalService: ModalService, private storageService: StorageService) {}
ngOnInit(): void { ngOnInit(): void {
console.log('this.poll', this.poll); console.log('this.poll', this.poll);
@ -26,4 +27,8 @@ export class PollResultsCompactComponent implements OnInit {
const config: MatDialogConfig<any> = { data: choice }; const config: MatDialogConfig<any> = { data: choice };
this.modalService.openModal<ChoiceDetailsComponent, Choice>(ChoiceDetailsComponent, config); this.modalService.openModal<ChoiceDetailsComponent, Choice>(ChoiceDetailsComponent, config);
} }
toggleAnswer(choice_id: number, value: string) {
this.storageService.toggleAnswer(choice_id, value);
}
} }