diff --git a/src/app/core/services/poll.service.ts b/src/app/core/services/poll.service.ts index f4269c7d..939fc5bd 100644 --- a/src/app/core/services/poll.service.ts +++ b/src/app/core/services/poll.service.ts @@ -1,6 +1,6 @@ import { Injectable } from '@angular/core'; import { ActivatedRouteSnapshot, Resolve, Router, RouterStateSnapshot } from '@angular/router'; -import { BehaviorSubject, Observable, Subscription } from 'rxjs'; +import { BehaviorSubject, Observable } from 'rxjs'; import { Answer } from '../enums/answer.enum'; import { Choice } from '../models/choice.model'; @@ -15,6 +15,8 @@ import { environment } from '../../../environments/environment'; import { StorageService } from './storage.service'; import { Title } from '@angular/platform-browser'; import { DateUtilitiesService } from './date.utilities.service'; +import { Stack } from '../models/stack.model'; +import { Vote } from '../models/vote.model'; @Injectable({ providedIn: 'root', @@ -116,10 +118,7 @@ export class PollService implements Resolve { public updateCurrentPoll(poll: Poll): void { console.log('this.storageService.vote_stack.id', this.storageService.vote_stack.id); - if ( - !this.storageService.vote_stack.id - // || this.storageService.vote_stack.poll_custom_url !== poll.custom_url - ) { + if (!this.storageService.vote_stack.id || this.storageService.vote_stack.poll_custom_url !== poll.custom_url) { console.log('set base choices', poll.choices); // set the choices only the first time the poll loads this.storageService.setChoicesForVoteStack(poll.choices); @@ -150,7 +149,11 @@ export class PollService implements Resolve { return this.convertTextToSlug(str) + '-' + this.uuidService.getUUID(); } - public convertTextToSlug(str: string) { + /** + * convert a text to a slug + * @param str + */ + public convertTextToSlug(str: string): string { str = str.trim(); str = str.replace(/^\s+|\s+$/g, ''); // trim str = str.toLowerCase(); @@ -253,4 +256,33 @@ export class PollService implements Resolve { // TODO handle pass access return url; } + + /** + * enrich vote stack with missing default votes + * @param vote_stack + */ + enrichVoteStackWithCurrentPollChoicesDefaultVotes(vote_stack: Stack) { + if (this._poll && this._poll.getValue) { + const polltemp = this._poll.getValue(); + polltemp.choices.map((choice) => { + // for each vote, if it has the choice_id, do nothing, else, add a default vote + if (!this.findExistingVoteFromChoiceId(choice.id, vote_stack.votes)) { + vote_stack.votes.push(new Vote(choice.id)); + } + }); + } + } + + /** + * find an existing vote in vote_stack from its choice_id + * @param choice_id + * @param votes + */ + findExistingVoteFromChoiceId(choice_id: number, votes: Vote[]) { + return votes.find((vote: Vote) => { + if (vote.choice_id === choice_id) { + return vote; + } + }); + } } diff --git a/src/app/core/services/storage.service.ts b/src/app/core/services/storage.service.ts index ad601437..397026c9 100644 --- a/src/app/core/services/storage.service.ts +++ b/src/app/core/services/storage.service.ts @@ -132,11 +132,10 @@ export class StorageService { /** * update vote stack from the backend - * @param resp + * @param voteStack */ - mapVotes(resp) { - console.log('mapVotes resp.data', resp.data); - this.vote_stack.owner = resp.data.owner; - this.vote_stack.id = resp.data.id; + mapVotes(voteStack: Stack) { + console.log('mapVotes voteStack', voteStack); + this.vote_stack = voteStack; } } diff --git a/src/app/features/consultation/consultation.component.html b/src/app/features/consultation/consultation.component.html index 887709a3..d8b94e9e 100644 --- a/src/app/features/consultation/consultation.component.html +++ b/src/app/features/consultation/consultation.component.html @@ -7,6 +7,11 @@ ⚰️ Ce sondage a expiré, il n'est plus possible d'y ajouter de votes ou de commentaires +
+
+ vous êtes admin de ce sondage +
+
diff --git a/src/app/features/consultation/consultation.component.ts b/src/app/features/consultation/consultation.component.ts index 424a1f78..959d176b 100644 --- a/src/app/features/consultation/consultation.component.ts +++ b/src/app/features/consultation/consultation.component.ts @@ -127,7 +127,8 @@ export class ConsultationComponent implements OnInit, OnDestroy { */ storeVoteStackAndReloadPoll(voteStack: any) { if (voteStack.status == 200) { - this.storageService.mapVotes(voteStack); + this.storageService.mapVotes(voteStack.data); + this.pollService.enrichVoteStackWithCurrentPollChoicesDefaultVotes(this.storageService.vote_stack); this.pollService.loadPollBycustom_url(this.poll.custom_url); } else { this.toastService.display('erreur à l enregistrement'); diff --git a/src/app/routes-framadate.ts b/src/app/routes-framadate.ts index 0ae3d976..cd01d665 100644 --- a/src/app/routes-framadate.ts +++ b/src/app/routes-framadate.ts @@ -36,7 +36,7 @@ export const routes: Routes = [ { path: 'poll/:custom_url/participation', loadChildren: () => import('./features/participation/participation.module').then((m) => m.ParticipationModule), - resolve: { poll: PollService }, + // resolve: { poll: PollService }, }, { path: 'success',