funky-framadate-front/src/app/core/services/poll.service.ts

241 lines
7.8 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core';
2020-06-18 16:15:26 +02:00
import { ActivatedRouteSnapshot, Resolve, Router, RouterStateSnapshot } from '@angular/router';
2020-11-13 09:38:42 +01:00
import { BehaviorSubject, Observable, Subscription } from 'rxjs';
2020-06-12 19:17:39 +02:00
import { Answer } from '../enums/answer.enum';
2020-05-12 19:16:23 +02:00
import { Choice } from '../models/choice.model';
import { Poll } from '../models/poll.model';
import { Owner } from '../models/owner.model';
import { ApiService } from './api.service';
import { ToastService } from './toast.service';
2020-06-25 22:42:26 +02:00
import { UserService } from './user.service';
import { UuidService } from './uuid.service';
2020-11-13 09:38:42 +01:00
import { HttpClient } from '@angular/common/http';
import { environment } from '../../../environments/environment';
import { StorageService } from './storage.service';
import { Title } from '@angular/platform-browser';
2021-04-30 12:37:04 +02:00
import { TimeSlices } from '../../../../mocks/old-stuff/config/defaultConfigs';
@Injectable({
providedIn: 'root',
})
2020-06-18 16:15:26 +02:00
export class PollService implements Resolve<Poll> {
_poll: BehaviorSubject<Poll | undefined> = new BehaviorSubject<Poll | undefined>(undefined);
2020-04-22 12:56:18 +02:00
public readonly poll: Observable<Poll | undefined> = this._poll.asObservable();
2020-06-25 22:42:26 +02:00
constructor(
2020-11-13 09:38:42 +01:00
private http: HttpClient,
2020-06-25 22:42:26 +02:00
private router: Router,
private apiService: ApiService,
private storageService: StorageService,
2020-06-25 22:42:26 +02:00
private userService: UserService,
private uuidService: UuidService,
private titleService: Title,
2020-06-25 22:42:26 +02:00
private toastService: ToastService
) {}
2020-11-13 09:38:42 +01:00
/**
* auto fetch a poll when route is looking for one in the administration pattern
* @param route
* @param state
*/
2020-06-18 16:15:26 +02:00
public async resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<Poll> {
2020-06-25 22:42:26 +02:00
const segments: string[] = state.url.split('/');
const wantedcustom_url: string = segments.includes('poll') ? segments[segments.indexOf('poll') + 1] : '';
// FIXME should be handled by routing
if (!wantedcustom_url && state.url.includes('administration')) {
2020-06-18 16:15:26 +02:00
// creation of new poll
2020-06-25 22:42:26 +02:00
const poll = new Poll(this.userService.getCurrentUser(), this.uuidService.getUUID(), '');
this._poll.next(poll);
this.router.navigate(['poll/' + poll.custom_url + '/administration']);
2020-06-18 16:15:26 +02:00
}
if (
!this._poll.getValue() ||
!this._poll.getValue().custom_url ||
this._poll.getValue().custom_url !== wantedcustom_url
) {
await this.loadPollBycustom_url(wantedcustom_url);
2020-06-18 16:15:26 +02:00
}
if (this._poll.getValue()) {
return this._poll.getValue();
} else {
this.router.navigate(['page-not-found']);
return;
}
}
2020-11-13 09:38:42 +01:00
getAllAvailablePolls() {
const baseHref = environment.api.version.apiV1.baseHref;
console.log('getAllAvailablePolls baseHref', baseHref);
const headers = ApiService.makeHeaders();
console.log('getAllAvailablePolls headers', headers);
try {
this.http.get(`${baseHref}/poll`, headers).subscribe((res: Observable<any>) => {
console.log('getAllAvailablePolls res', res);
});
} catch (e) {
console.error('getAllAvailablePolls e', e);
2020-11-13 09:38:42 +01:00
}
}
public async loadPollBycustom_url(custom_url: string): Promise<void> {
console.log('custom_url', custom_url);
if (custom_url) {
const poll: Poll | undefined = await this.apiService.getPollByCustomUrl(custom_url);
console.log({ loadPollBycustom_urlResponse: poll });
2021-04-29 10:17:13 +02:00
if (poll) {
this.updateCurrentPoll(poll);
this.titleService.setTitle(`☑️ ${poll.title} - ${environment.appTitle}`);
2021-04-29 10:17:13 +02:00
} else {
this.toastService.display(`sondage ${custom_url} non trouvé`);
2021-04-29 10:17:13 +02:00
}
2021-04-21 12:17:05 +02:00
}
}
public async loadPollBycustom_urlWithPasswordHash(custom_url: string, hash: string): Promise<void> {
console.log('custom_url', custom_url);
if (custom_url) {
const poll: Poll | undefined = await this.apiService.getPollByCustomUrlWithHash(custom_url, hash);
console.log({ loadPollBycustom_urlResponse: poll });
2020-10-17 11:12:53 +02:00
this.updateCurrentPoll(poll);
}
}
/**
* update poll and parse its fields
* @param poll
*/
2020-06-18 16:15:26 +02:00
public updateCurrentPoll(poll: Poll): void {
if (poll.kind == 'date') {
poll.choices = this.parseDateChoices(poll.choices);
}
this.storageService.setChoicesForVoteStack(poll.choices);
this.toastService.display('sondage bien mis à jour', 'success');
console.log('poll', poll);
2020-06-18 16:15:26 +02:00
this._poll.next(poll);
}
/**
* make a uniq custom_url for the current poll creation
* @param config
*/
makecustom_url(config: Poll): string {
2021-02-08 18:55:31 +01:00
console.log('config', config);
let str = '';
str =
config.configuration.dateCreated.getFullYear() +
'_' +
(config.configuration.dateCreated.getMonth() + 1) +
'_' +
config.configuration.dateCreated.getDate() +
'_' +
config.owner.pseudo +
'_' +
config.title;
str = str.replace(/^\s+|\s+$/g, ''); // trim
str = str.toLowerCase();
// remove accents, swap ñ for n, etc
const from = 'àáäâèéëêìíïîòóöôùúüûñç·/_,:;';
const to = 'aaaaeeeeiiiioooouuuunc------';
for (let i = 0, l = from.length; i < l; i++) {
str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
}
str = str
.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
.replace(/\s+/g, '-') // collapse whitespace and replace by -
.replace(/-+/g, '-'); // collapse dashes
return str + '-' + this.uuidService.getUUID();
}
2021-04-30 11:27:54 +02:00
public async saveCurrentPoll(): Promise<Subscription> {
// .then((resp) => {
// console.log('resp', resp);
// if (pollUrl) {
// console.log('pollUrl', pollUrl);
// this.toastService.display('Le sondage a été enregistré.');
// // TODO: redirect to view
// } else {
// this.toastService.display('Le sondage na été correctement enregistré, veuillez ré-essayer.');
// }
// })
// .catch((err) => {
// console.error('err', err);
// });
return await this.apiService.createPoll(this._poll.getValue());
}
public saveParticipation(choice: Choice, user: Owner, response: Answer): void {
2020-05-12 19:16:23 +02:00
const currentPoll = this._poll.getValue();
currentPoll.choices.find((c) => c.name === choice.name)?.updateParticipation(user, response);
2020-05-12 19:16:23 +02:00
this.updateCurrentPoll(currentPoll);
this.apiService.createParticipation(currentPoll.custom_url, choice.name, user.pseudo, response);
2020-06-25 22:42:26 +02:00
this.toastService.display('Votre participation au sondage a été enregistrée.');
}
2020-05-12 19:16:23 +02:00
public async deleteAllAnswers(): Promise<void> {
await this.apiService.deletePollAnswers(this._poll.getValue().custom_url);
2020-06-25 22:42:26 +02:00
this.toastService.display('Les participations des votants à ce sondage ont été supprimées.');
2020-05-01 19:10:17 +02:00
}
2020-05-12 19:16:23 +02:00
public async addComment(comment: string): Promise<void> {
await this.apiService.createComment(this._poll.getValue().custom_url, comment);
this.toastService.display('Votre commentaire a été enregistré.');
2020-05-12 19:16:23 +02:00
}
public async deleteComments(): Promise<void> {
await this.apiService.deletePollComments(this._poll.getValue().custom_url);
this.toastService.display('Les commentaires de ce sondage ont été supprimés.');
}
2020-06-12 19:17:39 +02:00
/**
* @description convert to API version 1 data transition object
* @param form
*/
newPollFromForm(form: any): Poll {
const newOwner = this.storageService.vote_stack.owner;
const newpoll = new Poll(newOwner, form.value.custom_url, form.value.title);
const pollKeys = Object.keys(newpoll);
const formFields = Object.keys(form.value);
for (const fieldOfForm of formFields) {
if (pollKeys.indexOf(fieldOfForm) !== -1) {
newpoll[fieldOfForm] = form.value[fieldOfForm];
}
}
2021-04-30 12:37:04 +02:00
newpoll.dateChoices = this.storageService.dateList;
newpoll.timeSlices = this.storageService.timeSlices;
console.log('newpoll', newpoll);
return newpoll;
2020-11-05 19:13:43 +01:00
}
public parseDateChoices(choices: Choice[]) {
const SEPARATOR_DATE_TIME_SLICE = ' >>> ';
const parsedChoices = [];
for (const c of choices) {
const time_list = [];
const choice = { name: c.name };
if (c.name.includes(SEPARATOR_DATE_TIME_SLICE)) {
const date_string = '';
const time_slice = '';
choice.name = time_slice;
}
parsedChoices.push({
date_string: choice,
choices: c,
});
}
console.log('parsedChoices', parsedChoices);
return [];
}
}