2020-06-18 16:15:26 +02:00
|
|
|
import { Component, OnDestroy, OnInit } from '@angular/core';
|
2021-06-07 11:30:10 +02:00
|
|
|
import { ActivatedRoute, ParamMap, Router } from '@angular/router';
|
2020-06-18 16:15:26 +02:00
|
|
|
import { Subscription } from 'rxjs';
|
2020-05-12 19:16:23 +02:00
|
|
|
import { Poll } from '../../core/models/poll.model';
|
2021-04-21 12:43:05 +02:00
|
|
|
import { PollService } from '../../core/services/poll.service';
|
2021-04-26 10:32:42 +02:00
|
|
|
import { DateService } from '../../core/services/date.service';
|
2021-04-29 10:45:22 +02:00
|
|
|
import { PollUtilitiesService } from '../../core/services/poll.utilities.service';
|
2021-04-28 12:01:09 +02:00
|
|
|
import { StorageService } from '../../core/services/storage.service';
|
2021-04-28 14:33:41 +02:00
|
|
|
import { ApiService } from '../../core/services/api.service';
|
2021-04-30 09:26:54 +02:00
|
|
|
import { ToastService } from '../../core/services/toast.service';
|
2022-02-07 13:58:23 +01:00
|
|
|
import { environment } from 'src/environments/environment';
|
2022-02-11 10:02:05 +01:00
|
|
|
import { Choice } from '../../core/models/choice.model';
|
2020-05-12 19:16:23 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-consultation',
|
|
|
|
templateUrl: './consultation.component.html',
|
|
|
|
styleUrls: ['./consultation.component.scss'],
|
|
|
|
})
|
2020-06-18 16:15:26 +02:00
|
|
|
export class ConsultationComponent implements OnInit, OnDestroy {
|
2021-04-26 10:32:42 +02:00
|
|
|
public fetching = true;
|
2022-02-07 13:58:23 +01:00
|
|
|
public environment = environment;
|
2020-06-18 16:15:26 +02:00
|
|
|
private routeSubscription: Subscription;
|
2021-04-30 16:51:51 +02:00
|
|
|
window: any;
|
2022-02-07 19:09:38 +01:00
|
|
|
public isArchived: boolean;
|
|
|
|
public poll: Poll;
|
|
|
|
public pollSlug: string;
|
|
|
|
public pass_hash: string;
|
2022-02-10 12:16:11 +01:00
|
|
|
public maxYesCount: any = 1;
|
|
|
|
public detailledDisplay: boolean = false;
|
|
|
|
public display_options_menu: boolean = false;
|
|
|
|
public show_admin_stuff: boolean = true;
|
2022-02-11 10:02:05 +01:00
|
|
|
public favChoicesList: Array<Choice> = [];
|
2020-05-12 19:16:23 +02:00
|
|
|
|
|
|
|
constructor(
|
2020-06-18 16:15:26 +02:00
|
|
|
private router: Router,
|
2021-04-29 10:45:22 +02:00
|
|
|
private utils: PollUtilitiesService,
|
2021-04-21 12:43:05 +02:00
|
|
|
private _Activatedroute: ActivatedRoute,
|
2021-04-28 12:01:09 +02:00
|
|
|
public storageService: StorageService,
|
2021-04-28 14:33:41 +02:00
|
|
|
public api: ApiService,
|
2021-04-21 12:43:05 +02:00
|
|
|
public pollService: PollService,
|
2021-04-26 10:32:42 +02:00
|
|
|
public dateService: DateService,
|
2021-04-30 09:26:54 +02:00
|
|
|
public toastService: ToastService
|
2020-05-12 19:16:23 +02:00
|
|
|
) {}
|
|
|
|
|
2021-06-07 11:30:10 +02:00
|
|
|
/**
|
|
|
|
* fetch poll data on init
|
|
|
|
*/
|
2020-05-12 19:16:23 +02:00
|
|
|
ngOnInit(): void {
|
2021-04-21 12:43:05 +02:00
|
|
|
console.log('constultation de poll');
|
|
|
|
this.pollService.poll.subscribe((newpoll: Poll) => {
|
|
|
|
this.poll = newpoll;
|
2021-04-26 10:32:42 +02:00
|
|
|
if (newpoll) {
|
|
|
|
this.isArchived = new Date(newpoll.expiracy_date) < new Date();
|
2021-05-20 13:25:45 +02:00
|
|
|
this.poll.choices_grouped.map((elem) => (elem.subSetToYes = false));
|
2022-02-11 10:02:05 +01:00
|
|
|
this.findFavouriteChoices(newpoll);
|
2021-04-26 10:32:42 +02:00
|
|
|
}
|
2021-04-21 12:43:05 +02:00
|
|
|
});
|
|
|
|
|
2021-06-07 11:30:10 +02:00
|
|
|
this._Activatedroute.paramMap.subscribe((params: ParamMap) => {
|
|
|
|
console.log('params _Activatedroute', params);
|
|
|
|
this.pollSlug = params.get('custom_url');
|
2021-06-10 11:43:17 +02:00
|
|
|
this.pass_hash = params.get('pass_hash');
|
2021-06-07 11:30:10 +02:00
|
|
|
|
2021-06-10 11:43:17 +02:00
|
|
|
console.log('this.pass_hash ', this.pass_hash);
|
|
|
|
if (this.pass_hash) {
|
2021-11-07 15:21:27 +01:00
|
|
|
this.pollService.loadPollByCustomUrlWithPasswordHash(this.pollSlug, this.pass_hash).then((resp) => {
|
2022-02-07 16:12:32 +01:00
|
|
|
console.log('loadPollByCustomUrlWithPasswordHash resp', this.pollService._poll.getValue());
|
2021-06-10 11:43:17 +02:00
|
|
|
this.fetching = false;
|
|
|
|
this.storageService.vote_stack.id = null;
|
2021-11-14 17:25:57 +01:00
|
|
|
this.storageService.setChoicesForVoteStack(this.pollService._poll.getValue().choices);
|
2021-06-10 11:43:17 +02:00
|
|
|
});
|
2021-06-10 10:52:32 +02:00
|
|
|
} else {
|
2021-11-07 15:21:27 +01:00
|
|
|
this.pollService.loadPollByCustomUrl(this.pollSlug).then((resp) => {
|
2021-11-14 17:25:57 +01:00
|
|
|
console.log('loadPollByCustomUrl resp', resp);
|
2021-06-10 10:52:32 +02:00
|
|
|
this.fetching = false;
|
2021-06-10 11:43:17 +02:00
|
|
|
this.storageService.vote_stack.id = null;
|
2021-11-14 17:25:57 +01:00
|
|
|
this.storageService.setChoicesForVoteStack(this.pollService._poll.getValue().choices);
|
2021-06-10 10:52:32 +02:00
|
|
|
});
|
|
|
|
}
|
2020-06-18 16:15:26 +02:00
|
|
|
});
|
2020-05-12 19:16:23 +02:00
|
|
|
}
|
|
|
|
|
2020-06-18 16:15:26 +02:00
|
|
|
ngOnDestroy(): void {
|
|
|
|
if (this.routeSubscription) {
|
|
|
|
this.routeSubscription.unsubscribe();
|
|
|
|
}
|
2020-05-12 19:16:23 +02:00
|
|
|
}
|
2021-04-26 11:27:44 +02:00
|
|
|
|
2022-02-07 12:09:59 +01:00
|
|
|
displayOptions() {
|
2022-02-10 12:16:11 +01:00
|
|
|
this.display_options_menu = !this.display_options_menu;
|
2021-04-26 11:27:44 +02:00
|
|
|
}
|
2022-02-07 15:33:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* create a new vote stack
|
|
|
|
*/
|
|
|
|
addVoteStack(): void {
|
2022-02-11 09:19:34 +01:00
|
|
|
this.storageService.vote_stack.custom_url = this.poll.custom_url;
|
2022-02-07 15:33:39 +01:00
|
|
|
this.pollService.pass_hash = this.pass_hash;
|
|
|
|
|
|
|
|
this.toastService.display('envoi du vote ....');
|
|
|
|
this.api
|
|
|
|
.sendNewVoteStackOfPoll(this.storageService.vote_stack)
|
|
|
|
.then((resp: any) => {
|
|
|
|
console.log('sendNewVoteStackOfPoll resp', resp);
|
|
|
|
this.storeVoteStackAndReloadPoll(resp);
|
|
|
|
})
|
|
|
|
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
|
|
.catch(this.api.ousideHandleError);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* store the updated vote stack
|
|
|
|
* @param voteStack
|
|
|
|
*/
|
|
|
|
storeVoteStackAndReloadPoll(voteStack: any) {
|
|
|
|
if (voteStack.status == 200) {
|
|
|
|
this.storageService.mapVotes(voteStack.data);
|
|
|
|
this.pollService.enrichVoteStackWithCurrentPollChoicesDefaultVotes(this.storageService.vote_stack);
|
|
|
|
if (this.pass_hash) {
|
|
|
|
this.pollService.loadPollByCustomUrlWithPasswordHash(this.poll.custom_url, this.pass_hash);
|
|
|
|
} else {
|
|
|
|
this.pollService.loadPollByCustomUrl(this.poll.custom_url);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.toastService.display('erreur à l enregistrement');
|
|
|
|
}
|
|
|
|
}
|
2022-02-11 10:02:05 +01:00
|
|
|
|
|
|
|
private findFavouriteChoices(newpoll: Poll) {
|
|
|
|
let tempFavChoiceList = [];
|
|
|
|
newpoll.choices.map((choice: Choice) => {
|
|
|
|
console.log('choice.score', choice.score, newpoll.max_score);
|
|
|
|
if (choice.score === newpoll.max_score) {
|
|
|
|
tempFavChoiceList.push(choice);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.favChoicesList = tempFavChoiceList;
|
|
|
|
}
|
2020-05-12 19:16:23 +02:00
|
|
|
}
|