update of a vote stack and fill missing default votes

This commit is contained in:
Tykayn 2021-06-10 10:17:15 +02:00 committed by tykayn
parent 09ff4be2f6
commit 16c527d649
5 changed files with 50 additions and 13 deletions

View File

@ -1,6 +1,6 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Resolve, Router, RouterStateSnapshot } from '@angular/router'; 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 { Answer } from '../enums/answer.enum';
import { Choice } from '../models/choice.model'; import { Choice } from '../models/choice.model';
@ -15,6 +15,8 @@ import { environment } from '../../../environments/environment';
import { StorageService } from './storage.service'; import { StorageService } from './storage.service';
import { Title } from '@angular/platform-browser'; import { Title } from '@angular/platform-browser';
import { DateUtilitiesService } from './date.utilities.service'; import { DateUtilitiesService } from './date.utilities.service';
import { Stack } from '../models/stack.model';
import { Vote } from '../models/vote.model';
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
@ -116,10 +118,7 @@ export class PollService implements Resolve<Poll> {
public updateCurrentPoll(poll: Poll): void { public updateCurrentPoll(poll: Poll): void {
console.log('this.storageService.vote_stack.id', this.storageService.vote_stack.id); console.log('this.storageService.vote_stack.id', this.storageService.vote_stack.id);
if ( if (!this.storageService.vote_stack.id || this.storageService.vote_stack.poll_custom_url !== poll.custom_url) {
!this.storageService.vote_stack.id
// || this.storageService.vote_stack.poll_custom_url !== poll.custom_url
) {
console.log('set base choices', poll.choices); console.log('set base choices', poll.choices);
// set the choices only the first time the poll loads // set the choices only the first time the poll loads
this.storageService.setChoicesForVoteStack(poll.choices); this.storageService.setChoicesForVoteStack(poll.choices);
@ -150,7 +149,11 @@ export class PollService implements Resolve<Poll> {
return this.convertTextToSlug(str) + '-' + this.uuidService.getUUID(); 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.trim();
str = str.replace(/^\s+|\s+$/g, ''); // trim str = str.replace(/^\s+|\s+$/g, ''); // trim
str = str.toLowerCase(); str = str.toLowerCase();
@ -253,4 +256,33 @@ export class PollService implements Resolve<Poll> {
// TODO handle pass access // TODO handle pass access
return url; 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;
}
});
}
} }

View File

@ -132,11 +132,10 @@ export class StorageService {
/** /**
* update vote stack from the backend * update vote stack from the backend
* @param resp * @param voteStack
*/ */
mapVotes(resp) { mapVotes(voteStack: Stack) {
console.log('mapVotes resp.data', resp.data); console.log('mapVotes voteStack', voteStack);
this.vote_stack.owner = resp.data.owner; this.vote_stack = voteStack;
this.vote_stack.id = resp.data.id;
} }
} }

View File

@ -7,6 +7,11 @@
⚰️ Ce sondage a expiré, il n'est plus possible d'y ajouter de votes ou de commentaires ⚰️ Ce sondage a expiré, il n'est plus possible d'y ajouter de votes ou de commentaires
</div> </div>
</div> </div>
<div class="message is-warning" *ngIf="poll && poll.admin_key">
<div class="message-body">
vous êtes admin de ce sondage
</div>
</div>
<div class="message is-info" *ngIf="poll.modification_policy == 'self'"> <div class="message is-info" *ngIf="poll.modification_policy == 'self'">
<div class="message-body"> <div class="message-body">

View File

@ -127,7 +127,8 @@ export class ConsultationComponent implements OnInit, OnDestroy {
*/ */
storeVoteStackAndReloadPoll(voteStack: any) { storeVoteStackAndReloadPoll(voteStack: any) {
if (voteStack.status == 200) { 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); this.pollService.loadPollBycustom_url(this.poll.custom_url);
} else { } else {
this.toastService.display('erreur à l enregistrement'); this.toastService.display('erreur à l enregistrement');

View File

@ -36,7 +36,7 @@ export const routes: Routes = [
{ {
path: 'poll/:custom_url/participation', path: 'poll/:custom_url/participation',
loadChildren: () => import('./features/participation/participation.module').then((m) => m.ParticipationModule), loadChildren: () => import('./features/participation/participation.module').then((m) => m.ParticipationModule),
resolve: { poll: PollService }, // resolve: { poll: PollService },
}, },
{ {
path: 'success', path: 'success',