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

129 lines
3.6 KiB
TypeScript
Raw Normal View History

2020-06-18 16:15:26 +02:00
import { Component, OnDestroy, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { Subscription } from 'rxjs';
2020-05-12 19:16:23 +02:00
import { Poll } from '../../core/models/poll.model';
import { ModalService } from '../../core/services/modal.service';
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-29 10:17:13 +02:00
import { Stack } from '../../core/models/stack.model';
2021-04-29 10:56:36 +02:00
import { environment } from '../../../environments/environment';
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-04-27 12:38:12 +02:00
public isCompactMode = true;
2020-06-18 16:15:26 +02:00
public poll: Poll;
2021-04-21 12:43:05 +02:00
public pollSlug: string;
public passHash: string;
2021-04-26 10:32:42 +02:00
public fetching = true;
2020-06-18 16:15:26 +02:00
public isArchived: boolean;
2021-04-26 11:27:44 +02:00
public myVoteStack: any = {
id: '',
};
public myTempVoteStack: any = {
id: '',
};
2020-06-18 16:15:26 +02:00
private routeSubscription: Subscription;
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
) {}
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-04-29 10:56:36 +02:00
if (!environment.production) {
this.addVoteStack();
}
2021-04-26 10:32:42 +02:00
}
2021-04-21 12:43:05 +02:00
});
this._Activatedroute.paramMap.subscribe((params) => {
console.log('params', params);
this.pollSlug = params.get('slug');
this.passHash = params.get('hash');
2021-04-21 12:43:05 +02:00
if (this.passHash) {
2021-04-30 11:27:54 +02:00
this.pollService.loadPollBycustom_urlWithPasswordHash(this.pollSlug, this.passHash);
2020-06-18 16:15:26 +02:00
} else {
2021-04-30 11:27:54 +02:00
this.pollService.loadPollBycustom_url(this.pollSlug).then((resp) => {
2021-04-21 12:43:05 +02:00
console.log('resp', resp);
this.fetching = false;
});
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
updateVoteStack(vote_stack: Stack): void {
2021-04-26 11:27:44 +02:00
alert('TODO');
this.api.sendUpdateVoteStack(vote_stack).then((resp) => {
console.log('sendUpdateVoteStack resp', resp);
this.toastService.display('vote bien mis à jour', 'success');
});
2021-04-26 11:27:44 +02:00
}
addVoteStack(): void {
this.api.sendNewVoteStackOfPoll(this.poll, this.storageService.vote_stack).subscribe((resp) => {
console.log('sendNewVoteStackOfPoll resp', resp);
this.toastService.display('vote ajouté', 'success');
2021-04-29 10:56:36 +02:00
if (resp) {
const response: Promise<Poll | undefined> = this.api.getPollByCustomUrl(this.poll.custom_url);
response.then((res: Poll | undefined) => {
res.choices = this.pollService.parseDateChoices(res.choices);
this.pollService._poll.next(res);
});
2021-04-30 09:26:54 +02:00
} else {
this.toastService.display('erreur à la réception du nouveau vote', 'error');
2021-04-29 10:56:36 +02:00
}
});
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
}