funky-framadate-front/src/app/features/consultation/consultation.component.ts

161 lines
4.8 KiB
TypeScript
Raw Normal View History

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';
import { StorageService } from '../../core/services/storage.service';
import { ApiService } from '../../core/services/api.service';
2021-04-30 09:26:54 +02:00
import { ToastService } from '../../core/services/toast.service';
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-06-10 12:35:14 +02:00
public isCompactMode = false;
2020-06-18 16:15:26 +02:00
public poll: Poll;
2021-04-21 12:43:05 +02:00
public pollSlug: string;
public pass_hash: string;
2021-04-26 10:32:42 +02:00
public fetching = true;
2020-06-18 16:15:26 +02:00
public isArchived: boolean;
2021-06-10 10:52:32 +02:00
public isAdmin: boolean;
2021-04-26 11:27:44 +02:00
2020-06-18 16:15:26 +02:00
private routeSubscription: Subscription;
2021-04-30 16:51:51 +02:00
window: any;
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,
public storageService: StorageService,
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();
this.poll.is_archived = this.isArchived;
2021-06-10 10:52:32 +02:00
this.isAdmin = this.poll.admin_key !== null;
this.poll.choices_grouped.map((elem) => (elem.subSetToYes = false));
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');
this.pass_hash = params.get('pass_hash');
2021-06-07 11:30:10 +02:00
console.log('this.pass_hash ', this.pass_hash);
if (this.pass_hash) {
this.pollService.loadPollBycustom_urlWithPasswordHash(this.pollSlug, this.pass_hash).then((resp) => {
console.log('resp', resp);
this.fetching = false;
this.storageService.vote_stack.id = null;
this.storageService.setChoicesForVoteStack(this.pollService._poll.getValue().choices);
});
2021-06-10 10:52:32 +02:00
} else {
this.pollService.loadPollBycustom_url(this.pollSlug).then((resp) => {
console.log('resp', resp);
this.fetching = false;
this.storageService.vote_stack.id = null;
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
2021-06-07 11:30:10 +02:00
/**
* update existing vote stack
2021-06-08 10:41:46 +02:00
* @param Stack
2021-06-07 11:30:10 +02:00
*/
2021-06-07 12:16:56 +02:00
updateVoteStack(): void {
const vote_stack = this.storageService.vote_stack;
2021-06-07 11:30:10 +02:00
vote_stack.poll_custom_url = this.poll.custom_url;
2021-06-08 10:41:46 +02:00
console.log('updateVoteStack vote_stack.votes', vote_stack.votes.length, vote_stack.votes);
const handlingError = this.api.ousideHandleError;
2021-06-07 11:30:10 +02:00
2021-06-08 10:41:46 +02:00
this.api
.sendUpdateVoteStack(vote_stack)
.then((resp) => {
console.log('sendUpdateVoteStack updated resp', resp);
2021-06-08 14:30:05 +02:00
this.storeVoteStackAndReloadPoll(resp);
2021-06-08 10:41:46 +02:00
this.toastService.display('vote bien mis à jour', 'success');
})
.catch(handlingError);
2021-04-26 11:27:44 +02:00
}
2021-06-07 11:30:10 +02:00
/**
* create a new vote stack
*/
addVoteStack(): void {
2021-06-07 11:30:10 +02:00
this.storageService.vote_stack.poll_custom_url = this.poll.custom_url;
this.pollService.pass_hash = this.pass_hash;
2021-06-07 11:30:10 +02:00
2021-05-20 14:23:57 +02:00
this.toastService.display('envoi du vote ....');
2021-06-07 12:36:49 +02:00
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);
2021-06-07 11:30:10 +02:00
}
2021-04-26 11:27:44 +02:00
2021-06-07 11:30:10 +02:00
/**
* 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.loadPollBycustom_urlWithPasswordHash(this.poll.custom_url, this.pass_hash);
} else {
this.pollService.loadPollBycustom_url(this.poll.custom_url);
}
2021-06-07 11:30:10 +02:00
} else {
this.toastService.display('erreur à l enregistrement');
}
2021-04-26 11:27:44 +02:00
}
/**
* export all the poll data available to the public as a CSV single file
*/
exportCSV(): void {
2021-04-29 10:56:36 +02:00
this.utils.exportCSV(this.poll);
2021-04-26 11:27:44 +02:00
}
exportJson(): void {
2021-04-29 10:56:36 +02:00
this.utils.download('export_poll_' + this.pollSlug + '.json', JSON.stringify(this.poll));
2021-04-26 11:27:44 +02:00
}
duplicate(): void {
alert('TODO');
}
print(): void {
alert('TODO');
}
2020-05-12 19:16:23 +02:00
}