2021-09-09 09:59:02 +02:00
|
|
|
import { Component, Input } from '@angular/core';
|
2020-05-12 19:16:23 +02:00
|
|
|
|
|
|
|
import { Poll } from '../../../core/models/poll.model';
|
2020-06-12 19:17:39 +02:00
|
|
|
import { PollService } from '../../../core/services/poll.service';
|
2021-09-09 09:59:02 +02:00
|
|
|
import { StorageService } from '../../../core/services/storage.service';
|
|
|
|
import { ToastService } from '../../../core/services/toast.service';
|
|
|
|
import { ApiService } from '../../../core/services/api.service';
|
2020-05-12 19:16:23 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-poll-results-detailed',
|
|
|
|
templateUrl: './poll-results-detailed.component.html',
|
|
|
|
styleUrls: ['./poll-results-detailed.component.scss'],
|
|
|
|
})
|
2021-04-30 09:26:54 +02:00
|
|
|
export class PollResultsDetailedComponent {
|
2020-05-12 19:16:23 +02:00
|
|
|
@Input() public poll: Poll;
|
|
|
|
|
2021-09-09 09:59:02 +02:00
|
|
|
constructor(
|
|
|
|
private pollService: PollService,
|
2021-09-09 21:14:41 +02:00
|
|
|
public storageService: StorageService,
|
2021-09-09 09:59:02 +02:00
|
|
|
private api: ApiService,
|
|
|
|
private toastService: ToastService
|
|
|
|
) {}
|
2020-05-12 19:16:23 +02:00
|
|
|
|
2021-04-27 12:14:57 +02:00
|
|
|
stackHasVotesForChoice(stack, choice: any) {
|
|
|
|
return undefined !== stack.votes[choice];
|
|
|
|
}
|
|
|
|
|
|
|
|
getValue(stack, choice: any) {
|
|
|
|
if (this.stackHasVotesForChoice(stack, choice)) {
|
|
|
|
return stack.votes[choice].value;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2021-05-20 13:25:45 +02:00
|
|
|
|
2021-09-09 09:59:02 +02:00
|
|
|
/**
|
|
|
|
* create a new vote stack
|
|
|
|
*/
|
|
|
|
addVoteStack(): void {
|
|
|
|
this.storageService.vote_stack.poll_custom_url = this.poll.custom_url;
|
|
|
|
this.toastService.display('envoi du vote ....');
|
|
|
|
this.api
|
|
|
|
.sendNewVoteStackOfPoll(this.storageService.vote_stack)
|
|
|
|
.then((resp: any) => {
|
|
|
|
console.log('sendNewVoteStackOfPoll resp', resp);
|
|
|
|
this.toastService.display('bien enregistré');
|
|
|
|
// reload stack
|
|
|
|
})
|
|
|
|
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
|
|
.catch(this.api.ousideHandleError);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* update existing vote stack
|
|
|
|
* @param Stack
|
|
|
|
*/
|
|
|
|
updateVoteStack(): void {
|
|
|
|
const vote_stack = this.storageService.vote_stack;
|
|
|
|
vote_stack.poll_custom_url = this.poll.custom_url;
|
|
|
|
|
|
|
|
console.log('updateVoteStack vote_stack.votes', vote_stack.votes.length, vote_stack.votes);
|
|
|
|
const handlingError = this.api.ousideHandleError;
|
|
|
|
|
|
|
|
this.api
|
|
|
|
.sendUpdateVoteStack(vote_stack)
|
|
|
|
.then((resp) => {
|
|
|
|
console.log('sendUpdateVoteStack updated resp', resp);
|
|
|
|
this.storeVoteStackAndReloadPoll(resp);
|
|
|
|
this.toastService.display('vote bien mis à jour', 'success');
|
|
|
|
})
|
|
|
|
.catch(handlingError);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* store the updated vote stack
|
|
|
|
* @param voteStack
|
|
|
|
*/
|
|
|
|
storeVoteStackAndReloadPoll(voteStack: any) {
|
|
|
|
if (voteStack.status == 200) {
|
|
|
|
this.storageService.mapVotes(voteStack.data);
|
2021-11-08 18:25:04 +01:00
|
|
|
this.pollService.enrichVoteStackWithCurrentPollChoicesDefaultVotes(this.storageService.vote_stack);
|
|
|
|
if (this.pollService.pass_hash) {
|
|
|
|
this.pollService.loadPollByCustomUrlWithPasswordHash(this.poll.custom_url, this.pollService.pass_hash);
|
|
|
|
} else {
|
|
|
|
this.pollService.loadPollByCustomUrl(this.poll.custom_url);
|
|
|
|
}
|
2021-09-09 09:59:02 +02:00
|
|
|
} else {
|
|
|
|
this.toastService.display('erreur à l enregistrement');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-20 13:25:45 +02:00
|
|
|
make_date(name: string) {
|
2021-09-09 09:59:02 +02:00
|
|
|
name = name.substr(0, 30);
|
2021-05-20 13:25:45 +02:00
|
|
|
return new Date(name);
|
|
|
|
}
|
2021-09-09 09:59:02 +02:00
|
|
|
|
|
|
|
make_display_range_time(name: string) {
|
|
|
|
return name.replace('>>>', 'de ');
|
|
|
|
}
|
2020-05-12 19:16:23 +02:00
|
|
|
}
|