import { Component, OnDestroy, OnInit } from '@angular/core'; import { ActivatedRoute, ParamMap, Router } from '@angular/router'; import { Subscription } from 'rxjs'; import { Poll } from '../../core/models/poll.model'; import { ModalService } from '../../core/services/modal.service'; import { PollService } from '../../core/services/poll.service'; import { DateService } from '../../core/services/date.service'; import { PollUtilitiesService } from '../../core/services/poll.utilities.service'; import { StorageService } from '../../core/services/storage.service'; import { ApiService } from '../../core/services/api.service'; import { Stack } from '../../core/models/stack.model'; import { environment } from '../../../environments/environment'; import { ToastService } from '../../core/services/toast.service'; import { AxiosResponse } from 'axios'; import { HttpResponse } from '@angular/common/http'; @Component({ selector: 'app-consultation', templateUrl: './consultation.component.html', styleUrls: ['./consultation.component.scss'], }) export class ConsultationComponent implements OnInit, OnDestroy { public isCompactMode = true; public poll: Poll; public pollSlug: string; public passHash: string; public fetching = true; public isArchived: boolean; public myVoteStack: any = { id: '', }; public myTempVoteStack: any = { id: '', }; private routeSubscription: Subscription; window: any; constructor( private router: Router, private utils: PollUtilitiesService, private _Activatedroute: ActivatedRoute, public storageService: StorageService, public api: ApiService, public pollService: PollService, public dateService: DateService, public toastService: ToastService ) {} /** * fetch poll data on init */ ngOnInit(): void { console.log('constultation de poll'); this.pollService.poll.subscribe((newpoll: Poll) => { this.poll = newpoll; if (newpoll) { this.isArchived = new Date(newpoll.expiracy_date) < new Date(); this.poll.is_archived = this.isArchived; this.poll.choices_grouped.map((elem) => (elem.subSetToYes = false)); } }); this._Activatedroute.paramMap.subscribe((params: ParamMap) => { console.log('params _Activatedroute', params); this.pollSlug = params.get('custom_url'); this.passHash = params.get('pass_hash'); // if (this.passHash) { // this.pollService.loadPollBycustom_urlWithPasswordHash(this.pollSlug, this.passHash); // } else { this.pollService.loadPollBycustom_url(this.pollSlug).then((resp) => { console.log('resp', resp); this.fetching = false; }); // } }); } ngOnDestroy(): void { if (this.routeSubscription) { this.routeSubscription.unsubscribe(); } } /** * 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); } /** * 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.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); this.pollService.loadPollBycustom_url(this.poll.custom_url); } else { this.toastService.display('erreur à l enregistrement'); } } /** * export all the poll data available to the public as a CSV single file */ exportCSV(): void { this.utils.exportCSV(this.poll); } exportJson(): void { this.utils.download('export_poll_' + this.pollSlug + '.json', JSON.stringify(this.poll)); } duplicate(): void { alert('TODO'); } print(): void { alert('TODO'); } }