2021-11-07 15:21:27 +01:00
|
|
|
|
import { Inject, Injectable } from '@angular/core';
|
|
|
|
|
import { ActivatedRoute, ActivatedRouteSnapshot, Resolve, Router, RouterStateSnapshot } from '@angular/router';
|
|
|
|
|
import { BehaviorSubject, Observable, Subscription } from 'rxjs';
|
2020-04-19 14:22:10 +02:00
|
|
|
|
|
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';
|
2020-04-19 14:22:10 +02:00
|
|
|
|
import { Poll } from '../models/poll.model';
|
|
|
|
|
import { ApiService } from './api.service';
|
2020-06-16 18:40:48 +02:00
|
|
|
|
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';
|
2021-11-08 12:09:35 +01:00
|
|
|
|
import { StorageService } from './storage.service';
|
|
|
|
|
import { Title } from '@angular/platform-browser';
|
2021-11-07 15:21:27 +01:00
|
|
|
|
import { FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms';
|
|
|
|
|
|
|
|
|
|
import { DOCUMENT } from '@angular/common';
|
|
|
|
|
import { DateChoice, TimeSlices } from '../models/dateChoice.model';
|
2021-05-18 22:51:06 +02:00
|
|
|
|
import { DateUtilitiesService } from './date.utilities.service';
|
2021-11-07 15:21:27 +01:00
|
|
|
|
import { Owner } from '../models/owner.model';
|
2021-06-10 10:17:15 +02:00
|
|
|
|
import { Stack } from '../models/stack.model';
|
2021-11-08 12:09:35 +01:00
|
|
|
|
import { Vote } from '../models/vote.model';
|
2022-02-10 11:15:48 +01:00
|
|
|
|
import { ClipboardService } from 'ngx-clipboard';
|
|
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
2022-02-14 14:37:42 +01:00
|
|
|
|
import { CommentDTO } from '../models/comment.model';
|
2020-04-19 14:22:10 +02:00
|
|
|
|
|
|
|
|
|
@Injectable({
|
2020-04-21 17:26:25 +02:00
|
|
|
|
providedIn: 'root',
|
2020-04-19 14:22:10 +02:00
|
|
|
|
})
|
2020-06-18 16:15:26 +02:00
|
|
|
|
export class PollService implements Resolve<Poll> {
|
2021-11-07 21:08:38 +01:00
|
|
|
|
public _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();
|
2021-11-07 14:52:49 +01:00
|
|
|
|
public form: FormGroup;
|
|
|
|
|
public startDateInterval: string;
|
|
|
|
|
public endDateInterval: string;
|
2021-11-07 15:21:27 +01:00
|
|
|
|
public intervalDays: number = 1;
|
2021-11-07 14:52:49 +01:00
|
|
|
|
public intervalDaysDefault = 7;
|
2021-11-17 16:25:24 +01:00
|
|
|
|
public dateChoiceList: DateChoice[] = []; // sets of days as strings, config to set identical time for days in a special days poll
|
2022-02-14 15:51:17 +01:00
|
|
|
|
public timeList: TimeSlices[] = [{ literal: 'example' }]; // ranges of time expressed as strings
|
2021-11-07 14:52:49 +01:00
|
|
|
|
public previousRouteName: string = '/administration';
|
|
|
|
|
public nextRouteName: string = '/administration/step/2';
|
|
|
|
|
public step_current: number = 1;
|
2021-11-18 13:07:01 +01:00
|
|
|
|
public step_max: number = 7;
|
2021-11-07 15:21:27 +01:00
|
|
|
|
public round: Function;
|
|
|
|
|
public pass_hash: string;
|
2021-11-07 21:08:38 +01:00
|
|
|
|
public admin_key: string;
|
|
|
|
|
public urlPrefix: string = window.location.origin;
|
2021-11-07 15:21:27 +01:00
|
|
|
|
public advancedDisplayEnabled = false;
|
|
|
|
|
public showDateInterval = false;
|
|
|
|
|
public allowSeveralHours = false;
|
|
|
|
|
public richTextMode = false;
|
2022-02-14 10:58:03 +01:00
|
|
|
|
public mode_calendar = true;
|
2021-11-16 11:16:01 +01:00
|
|
|
|
public calendar: Date[] = [new Date()];
|
2021-11-16 17:30:31 +01:00
|
|
|
|
public disabled_dates: Date[] = [];
|
2020-04-19 14:22:10 +02:00
|
|
|
|
|
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,
|
2021-11-08 12:09:35 +01:00
|
|
|
|
private storageService: StorageService,
|
2020-06-25 22:42:26 +02:00
|
|
|
|
private userService: UserService,
|
|
|
|
|
private uuidService: UuidService,
|
2021-11-07 15:21:27 +01:00
|
|
|
|
private toastService: ToastService,
|
2021-11-08 12:09:35 +01:00
|
|
|
|
private titleService: Title,
|
2021-11-07 15:21:27 +01:00
|
|
|
|
public DateUtilitiesService: DateUtilitiesService,
|
|
|
|
|
public route: ActivatedRoute,
|
2022-02-10 11:15:48 +01:00
|
|
|
|
private _clipboardService: ClipboardService,
|
|
|
|
|
private translate: TranslateService,
|
2021-11-07 15:21:27 +01:00
|
|
|
|
@Inject(DOCUMENT) private document: any,
|
|
|
|
|
private fb: FormBuilder
|
2021-05-03 12:36:03 +02:00
|
|
|
|
) {
|
2021-11-07 15:21:27 +01:00
|
|
|
|
this.createFormGroup();
|
2021-11-12 12:50:21 +01:00
|
|
|
|
|
2021-11-12 15:59:44 +01:00
|
|
|
|
// fill in the next 3 days of the calendar date picker
|
2021-11-17 16:25:24 +01:00
|
|
|
|
this.calendar = this.DateUtilitiesService.makeDefaultCalendarDateChoices();
|
|
|
|
|
this.dateChoiceList = this.DateUtilitiesService.makeDefaultDateChoices();
|
|
|
|
|
|
2021-11-16 17:30:31 +01:00
|
|
|
|
// disable days before today
|
|
|
|
|
for (let i = 1; i < 31; i++) {
|
|
|
|
|
this.disabled_dates.push(this.DateUtilitiesService.addDaysToDate(-i, new Date()));
|
|
|
|
|
}
|
2021-11-12 12:09:48 +01:00
|
|
|
|
if (environment.autofill_creation) {
|
2021-11-07 15:21:27 +01:00
|
|
|
|
this.setDemoValues();
|
2021-11-12 12:50:21 +01:00
|
|
|
|
}
|
|
|
|
|
if (environment.autoSendNewPoll) {
|
|
|
|
|
this.createPoll();
|
2021-11-07 15:21:27 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-14 10:37:38 +01:00
|
|
|
|
updateTitle() {
|
|
|
|
|
let suppl = environment.production ? ' [DEV]' : '';
|
|
|
|
|
let apptitle = environment.appTitle + suppl;
|
|
|
|
|
let step_current;
|
|
|
|
|
if (this.step_current) {
|
|
|
|
|
// in creation tunnel
|
|
|
|
|
let stepsTitle = {
|
|
|
|
|
date: [
|
|
|
|
|
'creation.title',
|
|
|
|
|
'creation.want',
|
|
|
|
|
'dates.title',
|
|
|
|
|
'hours.title',
|
|
|
|
|
'advanced.title',
|
|
|
|
|
'owner.title',
|
|
|
|
|
'resume.title',
|
|
|
|
|
],
|
|
|
|
|
text: [
|
|
|
|
|
'creation.title',
|
|
|
|
|
'creation.want',
|
|
|
|
|
'dates.title',
|
|
|
|
|
'hours.title',
|
|
|
|
|
'advanced.title',
|
|
|
|
|
'owner.title',
|
|
|
|
|
'resume.title',
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let kind = 'date';
|
|
|
|
|
step_current = this.step_current | 1;
|
|
|
|
|
if (this.form.value.isAboutDate) {
|
|
|
|
|
kind = 'text';
|
|
|
|
|
}
|
|
|
|
|
let keyToTranslate = stepsTitle[kind][(step_current - 1) | 0];
|
|
|
|
|
|
|
|
|
|
this.translate.get(keyToTranslate).subscribe(
|
|
|
|
|
(resp) => {
|
|
|
|
|
this.titleService.setTitle(environment.appTitle + ' - ' + resp + ' - ' + this.form.value.title);
|
|
|
|
|
},
|
|
|
|
|
(err) => {
|
|
|
|
|
console.error(err);
|
|
|
|
|
this.toastService.display(err.message);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
this.titleService.setTitle(apptitle);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-07 15:21:27 +01:00
|
|
|
|
/**
|
2021-11-12 15:59:44 +01:00
|
|
|
|
* add example values to the form for demo env
|
2021-11-07 15:21:27 +01:00
|
|
|
|
*/
|
|
|
|
|
setDemoValues(): void {
|
|
|
|
|
this.form.patchValue({
|
2021-11-16 16:16:30 +01:00
|
|
|
|
title: 'Mon titre de sondage du ' + this.DateUtilitiesService.formateDateToInputStringNg(new Date()),
|
2021-11-07 15:21:27 +01:00
|
|
|
|
description: 'répondez SVP <3 ! *-* ',
|
|
|
|
|
custom_url: this.uuidService.getUUID(),
|
|
|
|
|
creatorPseudo: 'Chuck Norris',
|
|
|
|
|
creatorEmail: 'chucknorris@example.com',
|
|
|
|
|
isAboutDate: true,
|
|
|
|
|
whoModifiesAnswers: 'everybody',
|
|
|
|
|
whoCanChangeAnswers: 'everybody',
|
|
|
|
|
isProtectedByPassword: false,
|
|
|
|
|
richTextMode: false,
|
|
|
|
|
areResultsPublic: true,
|
2021-11-14 17:43:18 +01:00
|
|
|
|
expiresDaysDelay: environment.expiresDaysDelay,
|
2021-11-07 15:21:27 +01:00
|
|
|
|
});
|
|
|
|
|
this.automaticSlug();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* set the poll slug from other data of the poll
|
|
|
|
|
*/
|
|
|
|
|
automaticSlug() {
|
|
|
|
|
this.form.patchValue({ custom_url: this.makeSlug(this.form) });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public createFormGroup() {
|
2021-11-12 13:27:18 +01:00
|
|
|
|
let minlengthValidation = environment.production ? 12 : 0;
|
2021-11-07 15:21:27 +01:00
|
|
|
|
let form = this.fb.group({
|
2021-12-06 12:13:47 +01:00
|
|
|
|
title: ['', [Validators.required, Validators.minLength(minlengthValidation)]],
|
2021-11-30 11:25:30 +01:00
|
|
|
|
creatorPseudo: ['', []],
|
2021-11-07 15:21:27 +01:00
|
|
|
|
created_at: [new Date(), [Validators.required]],
|
2021-11-30 18:14:07 +01:00
|
|
|
|
creatorEmail: ['', [environment.creation_email_is_required ? Validators.required : null]],
|
2021-11-07 15:21:27 +01:00
|
|
|
|
custom_url: [this.uuidService.getUUID(), [Validators.required]],
|
2021-11-30 11:25:30 +01:00
|
|
|
|
description: ['', []],
|
2021-11-12 13:27:18 +01:00
|
|
|
|
password: ['', []],
|
2022-02-04 15:57:21 +01:00
|
|
|
|
password_repeat: ['', []],
|
2021-11-07 15:21:27 +01:00
|
|
|
|
choices: new FormArray([]),
|
2022-02-04 15:57:21 +01:00
|
|
|
|
whoModifiesAnswers: ['self', [Validators.required]],
|
|
|
|
|
whoCanChangeAnswers: ['self', [Validators.required]],
|
2022-02-09 15:56:34 +01:00
|
|
|
|
isAboutDate: [true, [Validators.required]],
|
2021-11-14 17:43:18 +01:00
|
|
|
|
expiresDaysDelay: [environment.expiresDaysDelay, []],
|
2022-02-04 15:57:21 +01:00
|
|
|
|
expiracy_date: [this.DateUtilitiesService.addDaysToDate(environment.expiresDaysDelay, new Date()), []],
|
2021-11-08 12:09:35 +01:00
|
|
|
|
isZeroKnoledge: [false, [Validators.required]],
|
2021-11-07 15:21:27 +01:00
|
|
|
|
isProtectedByPassword: [false, [Validators.required]],
|
2021-11-08 12:09:35 +01:00
|
|
|
|
isOwnerNotifiedByEmailOnNewVote: [true, [Validators.required]],
|
|
|
|
|
isOwnerNotifiedByEmailOnNewComment: [true, [Validators.required]],
|
2021-11-07 15:21:27 +01:00
|
|
|
|
areResultsPublic: [true, [Validators.required]],
|
|
|
|
|
richTextMode: [false, [Validators.required]],
|
2021-11-08 12:09:35 +01:00
|
|
|
|
isYesAnswerAvailable: [true, [Validators.required]],
|
|
|
|
|
isMaybeAnswerAvailable: [true, [Validators.required]],
|
|
|
|
|
isNoAnswerAvailable: [true, [Validators.required]],
|
|
|
|
|
allowComments: [true, [Validators.required]],
|
2021-11-29 13:00:01 +01:00
|
|
|
|
maxCountOfAnswers: [300, []],
|
2021-11-08 12:09:35 +01:00
|
|
|
|
hasMaxCountOfAnswers: [300, [Validators.required]],
|
|
|
|
|
useVoterUniqueLink: [false, [Validators.required]],
|
2021-11-12 13:27:18 +01:00
|
|
|
|
voterEmailList: ['', []],
|
2021-11-30 18:01:18 +01:00
|
|
|
|
hasSeveralHours: [true, []],
|
2021-11-23 10:21:02 +01:00
|
|
|
|
hideResults: [false, []],
|
2021-11-14 17:43:18 +01:00
|
|
|
|
allowNewDateTime: [true, [Validators.required]],
|
2021-11-07 15:21:27 +01:00
|
|
|
|
});
|
|
|
|
|
this.form = form;
|
|
|
|
|
return form;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-12 15:59:44 +01:00
|
|
|
|
/**
|
|
|
|
|
* set default configs to the form
|
|
|
|
|
*/
|
2021-11-12 12:50:21 +01:00
|
|
|
|
public patchFormDefaultValues() {
|
|
|
|
|
this.form.patchValue({
|
2021-11-29 13:00:01 +01:00
|
|
|
|
title: 'Mon titre de sondage',
|
2021-11-12 12:50:21 +01:00
|
|
|
|
description: '',
|
|
|
|
|
custom_url: this.uuidService.getUUID(),
|
|
|
|
|
creatorPseudo: '',
|
|
|
|
|
creatorEmail: '',
|
|
|
|
|
isAboutDate: true,
|
|
|
|
|
whoModifiesAnswers: 'everybody',
|
|
|
|
|
whoCanChangeAnswers: 'everybody',
|
|
|
|
|
isProtectedByPassword: false,
|
|
|
|
|
richTextMode: false,
|
|
|
|
|
areResultsPublic: true,
|
2021-11-14 17:43:18 +01:00
|
|
|
|
expiresDaysDelay: environment.expiresDaysDelay,
|
2021-11-12 12:50:21 +01:00
|
|
|
|
maxCountOfAnswers: 300,
|
|
|
|
|
voterEmailList: '',
|
|
|
|
|
password: '',
|
|
|
|
|
});
|
2021-11-12 13:27:18 +01:00
|
|
|
|
this.setDefaultDatesForInterval();
|
2021-11-12 12:50:21 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-12 15:59:44 +01:00
|
|
|
|
/**
|
|
|
|
|
* get a new slug from form title and creation date
|
|
|
|
|
*/
|
2021-11-07 15:21:27 +01:00
|
|
|
|
public updateSlug(): void {
|
|
|
|
|
this.form.patchValue({ custom_url: this.makeSlug(this.form) });
|
2021-05-03 12:36:03 +02:00
|
|
|
|
}
|
2020-04-19 14:22:10 +02:00
|
|
|
|
|
2020-11-13 09:38:42 +01:00
|
|
|
|
/**
|
|
|
|
|
* auto fetch a poll when route is looking for one in the administration pattern
|
2021-11-12 15:59:44 +01:00
|
|
|
|
* DO NOT USE - needs refacto
|
2020-11-13 09:38:42 +01:00
|
|
|
|
* @param route
|
|
|
|
|
* @param state
|
|
|
|
|
*/
|
2020-06-18 16:15:26 +02:00
|
|
|
|
public async resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<Poll> {
|
2021-11-08 12:09:35 +01:00
|
|
|
|
console.log('resolve route,state', route, state);
|
2020-06-25 22:42:26 +02:00
|
|
|
|
const segments: string[] = state.url.split('/');
|
2021-11-08 12:09:35 +01:00
|
|
|
|
const wantedcustom_url: string = segments.includes('poll') ? segments[segments.indexOf('poll') + 1] : '';
|
|
|
|
|
|
2021-04-30 10:59:46 +02:00
|
|
|
|
if (
|
|
|
|
|
!this._poll.getValue() ||
|
|
|
|
|
!this._poll.getValue().custom_url ||
|
2021-11-08 12:09:35 +01:00
|
|
|
|
this._poll.getValue().custom_url !== wantedcustom_url
|
2021-04-30 10:59:46 +02:00
|
|
|
|
) {
|
2021-11-08 12:09:35 +01:00
|
|
|
|
if (this.pass_hash) {
|
|
|
|
|
this.storageService.vote_stack.pass_hash = this.pass_hash;
|
2021-11-08 18:25:04 +01:00
|
|
|
|
await this.loadPollByCustomUrlWithPasswordHash(wantedcustom_url, this.pass_hash);
|
2021-11-08 12:09:35 +01:00
|
|
|
|
} else {
|
2021-11-08 18:25:04 +01:00
|
|
|
|
await this.loadPollByCustomUrl(wantedcustom_url);
|
2021-11-08 12:09:35 +01:00
|
|
|
|
}
|
2020-06-18 16:15:26 +02:00
|
|
|
|
}
|
2021-11-08 12:09:35 +01:00
|
|
|
|
const loadedPoll = this._poll.getValue();
|
|
|
|
|
if (loadedPoll) {
|
2022-02-11 09:19:34 +01:00
|
|
|
|
this.storageService.vote_stack.custom_url = loadedPoll.custom_url;
|
2021-11-08 12:09:35 +01:00
|
|
|
|
return loadedPoll;
|
2020-06-18 16:15:26 +02:00
|
|
|
|
} else {
|
|
|
|
|
this.router.navigate(['page-not-found']);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-04-21 17:26:25 +02:00
|
|
|
|
}
|
2020-04-19 14:22:10 +02:00
|
|
|
|
|
2021-11-08 12:09:35 +01:00
|
|
|
|
/**
|
|
|
|
|
* get all polls
|
|
|
|
|
*/
|
|
|
|
|
getAllAvailablePolls(): void {
|
2020-11-13 09:38:42 +01:00
|
|
|
|
const baseHref = environment.api.version.apiV1.baseHref;
|
2021-11-07 15:21:27 +01:00
|
|
|
|
console.log('getAllAvailablePolls baseHref', baseHref);
|
2020-11-13 09:38:42 +01:00
|
|
|
|
const headers = ApiService.makeHeaders();
|
2021-11-07 15:21:27 +01:00
|
|
|
|
console.log('getAllAvailablePolls headers', headers);
|
2020-11-13 09:38:42 +01:00
|
|
|
|
try {
|
|
|
|
|
this.http.get(`${baseHref}/poll`, headers).subscribe((res: Observable<any>) => {
|
|
|
|
|
console.log('getAllAvailablePolls res', res);
|
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
2021-11-07 15:21:27 +01:00
|
|
|
|
console.log('getAllAvailablePolls e', e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-14 14:37:42 +01:00
|
|
|
|
/**
|
|
|
|
|
* load a poll data and update the current poll of PollService
|
|
|
|
|
* @param custom_url
|
|
|
|
|
*/
|
2021-11-08 18:25:04 +01:00
|
|
|
|
public async loadPollByCustomUrl(custom_url: string): Promise<void> {
|
2021-11-08 12:09:35 +01:00
|
|
|
|
if (custom_url) {
|
|
|
|
|
const poll: Poll | undefined = await this.apiService.getPollByCustomUrl(custom_url);
|
|
|
|
|
|
|
|
|
|
if (poll) {
|
|
|
|
|
this.updateCurrentPoll(poll);
|
|
|
|
|
this.titleService.setTitle(`☑️ ${poll.title} - ${environment.appTitle}`);
|
|
|
|
|
} else {
|
|
|
|
|
this.toastService.display(`sondage ${custom_url} non trouvé`);
|
|
|
|
|
this.router.navigate(['page-not-found']);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
this.toastService.display(`sondage sans custom url : ${custom_url}`);
|
2021-11-07 15:21:27 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-08 12:09:35 +01:00
|
|
|
|
|
2021-11-08 18:25:04 +01:00
|
|
|
|
public async loadPollByCustomUrlWithPasswordHash(custom_url: string, hash: string): Promise<void> {
|
2021-11-08 12:09:35 +01:00
|
|
|
|
if (custom_url) {
|
|
|
|
|
const poll: Poll | undefined = await this.apiService.getPollByCustomUrlWithHash(custom_url, hash);
|
|
|
|
|
|
|
|
|
|
if (poll) {
|
|
|
|
|
this.updateCurrentPoll(poll);
|
|
|
|
|
this.titleService.setTitle(`☑️ ${poll.title} - ${environment.appTitle}`);
|
|
|
|
|
} else {
|
|
|
|
|
this.toastService.display(`sondage ${custom_url} non trouvé`);
|
|
|
|
|
this.router.navigate(['page-not-found']);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
this.toastService.display(`sondage sans custom url : ${custom_url}`);
|
2020-11-13 09:38:42 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-08 12:09:35 +01:00
|
|
|
|
/**
|
|
|
|
|
* update poll and parse its fields
|
|
|
|
|
* @param poll
|
|
|
|
|
*/
|
2022-02-11 09:19:34 +01:00
|
|
|
|
public updateCurrentPoll(poll: Poll, displayToast = false): Poll {
|
2021-11-08 12:09:35 +01:00
|
|
|
|
console.log('this.storageService.vote_stack.id', this.storageService.vote_stack.id);
|
2021-11-07 15:21:27 +01:00
|
|
|
|
this._poll.next(poll);
|
2021-11-14 17:25:57 +01:00
|
|
|
|
console.log('next poll', poll);
|
|
|
|
|
|
|
|
|
|
this.storageService.setChoicesForVoteStack(poll.choices);
|
2022-02-11 09:19:34 +01:00
|
|
|
|
if (displayToast) {
|
|
|
|
|
this.toastService.display(`sondage ${poll.title} bien mis à jour`, 'success');
|
|
|
|
|
}
|
2021-11-12 15:59:44 +01:00
|
|
|
|
return poll;
|
2021-11-07 15:21:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* add all the dates between the start and end dates in the interval section
|
|
|
|
|
*/
|
|
|
|
|
addIntervalOfDates(): void {
|
|
|
|
|
const newIntervalArray = this.DateUtilitiesService.getDatesInRange(
|
|
|
|
|
this.DateUtilitiesService.parseInputDateToDateObject(new Date(this.startDateInterval)),
|
|
|
|
|
this.DateUtilitiesService.parseInputDateToDateObject(new Date(this.endDateInterval)),
|
|
|
|
|
1
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const converted = [];
|
|
|
|
|
newIntervalArray.forEach((element) => {
|
|
|
|
|
converted.push({
|
|
|
|
|
literal: element.literal,
|
|
|
|
|
date_object: element.date_object,
|
2021-11-22 15:30:23 +01:00
|
|
|
|
timeList: [
|
|
|
|
|
{
|
|
|
|
|
literal: 'matin',
|
|
|
|
|
},
|
|
|
|
|
],
|
2021-11-07 15:21:27 +01:00
|
|
|
|
});
|
|
|
|
|
});
|
2021-11-17 16:25:24 +01:00
|
|
|
|
this.dateChoiceList = [...new Set(converted)];
|
2021-11-07 15:21:27 +01:00
|
|
|
|
// add only dates that are not already present with a Set of unique items
|
2021-11-17 16:25:24 +01:00
|
|
|
|
console.log('this.dateChoiceList', this.dateChoiceList);
|
2021-11-07 15:21:27 +01:00
|
|
|
|
this.showDateInterval = false;
|
2021-04-29 12:49:23 +02:00
|
|
|
|
|
2021-11-17 16:25:24 +01:00
|
|
|
|
this.form.patchValue({ choices: this.dateChoiceList });
|
2021-11-07 15:21:27 +01:00
|
|
|
|
|
|
|
|
|
this.toastService.display(`les dates ont été ajoutées aux réponses possibles.`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* handle keyboard shortcuts
|
|
|
|
|
* @param $event
|
|
|
|
|
* @param choice_number
|
|
|
|
|
*/
|
|
|
|
|
keyOnChoice($event: KeyboardEvent, choice_number: number): void {
|
|
|
|
|
$event.preventDefault();
|
|
|
|
|
|
|
|
|
|
const lastChoice = this.choices.length - 1 === choice_number;
|
|
|
|
|
// reset field with Ctrl + D
|
|
|
|
|
// add a field with Ctrl + N
|
2021-11-22 15:30:23 +01:00
|
|
|
|
// go to previous timeSlice with arrow up
|
|
|
|
|
// go to next timeSlice with arrow down
|
2021-11-07 15:21:27 +01:00
|
|
|
|
|
|
|
|
|
if ($event.key == 'ArrowUp' && choice_number > 0) {
|
|
|
|
|
this.focusOnChoice(choice_number - 1);
|
|
|
|
|
}
|
|
|
|
|
if ($event.key == 'ArrowDown') {
|
2021-11-22 15:30:23 +01:00
|
|
|
|
// add a field if we are on the last timeSlice
|
2021-11-07 15:21:27 +01:00
|
|
|
|
if (lastChoice) {
|
|
|
|
|
this.addChoice();
|
|
|
|
|
this.toastService.display('choix ajouté par raccourci "flèche bas"');
|
2021-04-29 10:17:13 +02:00
|
|
|
|
} else {
|
2021-11-07 15:21:27 +01:00
|
|
|
|
this.focusOnChoice(choice_number + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($event.ctrlKey && $event.key == 'Backspace') {
|
|
|
|
|
this.deleteChoiceField(choice_number);
|
|
|
|
|
this.toastService.display('choix supprimé par raccourci "Ctrl + retour"');
|
|
|
|
|
this.focusOnChoice(Math.min(choice_number - 1, 0));
|
|
|
|
|
}
|
|
|
|
|
if ($event.ctrlKey && $event.key == 'Enter') {
|
|
|
|
|
// go to other fields
|
|
|
|
|
const elem = this.document.querySelector('#creatorEmail');
|
|
|
|
|
if (elem) {
|
|
|
|
|
elem.focus();
|
2021-04-29 10:17:13 +02:00
|
|
|
|
}
|
2021-04-21 12:17:05 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-30 23:24:48 +02:00
|
|
|
|
|
2021-11-07 15:21:27 +01:00
|
|
|
|
/**
|
|
|
|
|
* change time spans
|
|
|
|
|
*/
|
|
|
|
|
addTime() {
|
|
|
|
|
this.timeList.push({
|
|
|
|
|
literal: '',
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-06-08 14:30:05 +02:00
|
|
|
|
|
2021-11-07 15:21:27 +01:00
|
|
|
|
removeAllTimes() {
|
|
|
|
|
this.timeList = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resetTimes() {
|
|
|
|
|
this.timeList = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-11-22 15:30:23 +01:00
|
|
|
|
* add a time period to a specific date timeSlice,
|
2021-11-07 15:21:27 +01:00
|
|
|
|
* focus on the new input
|
|
|
|
|
* @param config
|
|
|
|
|
* @param id
|
|
|
|
|
*/
|
|
|
|
|
addTimeToDate(config: any, id: number) {
|
|
|
|
|
this.timeList.push({
|
|
|
|
|
literal: '',
|
|
|
|
|
});
|
|
|
|
|
const selector = '[ng-reflect-choice_label="dateTime_' + id + '_Choices_' + (this.timeList.length - 1) + '"]';
|
|
|
|
|
const elem = this.document.querySelector(selector);
|
|
|
|
|
if (elem) {
|
|
|
|
|
elem.focus();
|
2020-10-17 11:12:53 +02:00
|
|
|
|
}
|
2020-04-21 17:26:25 +02:00
|
|
|
|
}
|
2020-04-19 14:22:10 +02:00
|
|
|
|
|
2021-11-14 17:02:20 +01:00
|
|
|
|
/**
|
|
|
|
|
* convert form data to DTO to create a new poll, and store the admin key
|
|
|
|
|
*/
|
2021-11-12 13:27:18 +01:00
|
|
|
|
public createPoll(): Promise<any> {
|
2021-11-12 12:50:21 +01:00
|
|
|
|
this.toastService.display('sending...');
|
|
|
|
|
const newpoll = this.newPollFromForm();
|
2021-11-12 13:27:18 +01:00
|
|
|
|
return this.apiService.createPoll(newpoll).then(
|
|
|
|
|
(resp: any) => {
|
|
|
|
|
console.log('poll created resp', resp);
|
|
|
|
|
this.admin_key = resp.data.poll.admin_key;
|
2021-11-14 17:02:20 +01:00
|
|
|
|
this.storageService.userPolls.push(resp.data.poll);
|
2021-11-12 13:27:18 +01:00
|
|
|
|
},
|
2021-11-14 17:02:20 +01:00
|
|
|
|
(error) => {
|
|
|
|
|
this.toastService.display('BOOM, the createPoll went wrong');
|
|
|
|
|
this.apiService.ousideHandleError(error);
|
|
|
|
|
}
|
2021-11-12 13:27:18 +01:00
|
|
|
|
);
|
2021-11-07 15:21:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-30 14:15:21 +02:00
|
|
|
|
/**
|
2021-11-07 15:21:27 +01:00
|
|
|
|
* default interval of dates proposed is from today to 7 days more
|
2021-04-30 14:15:21 +02:00
|
|
|
|
*/
|
2021-11-07 15:21:27 +01:00
|
|
|
|
setDefaultDatesForInterval(): void {
|
|
|
|
|
const dateCurrent = new Date();
|
|
|
|
|
const dateJson = dateCurrent.toISOString();
|
|
|
|
|
this.startDateInterval = dateJson.substring(0, 10);
|
|
|
|
|
this.endDateInterval = this.DateUtilitiesService.addDaysToDate(this.intervalDaysDefault, dateCurrent)
|
|
|
|
|
.toISOString()
|
|
|
|
|
.substring(0, 10);
|
|
|
|
|
this.form.patchValue({
|
|
|
|
|
startDateInterval: this.startDateInterval,
|
|
|
|
|
endDateInterval: this.endDateInterval,
|
|
|
|
|
});
|
|
|
|
|
this.countDays();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
askInitFormDefault(): void {
|
2021-11-12 12:09:48 +01:00
|
|
|
|
this.initFormDefault(environment.autofill_creation);
|
2021-11-07 15:21:27 +01:00
|
|
|
|
this.toastService.display('formulaire réinitialisé');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
countDays(): void {
|
|
|
|
|
this.intervalDays = this.DateUtilitiesService.countDays(
|
|
|
|
|
this.DateUtilitiesService.parseInputDateToDateObject(new Date(this.startDateInterval)),
|
|
|
|
|
this.DateUtilitiesService.parseInputDateToDateObject(new Date(this.endDateInterval))
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
focusOnChoice(index): void {
|
|
|
|
|
const selector = '#choice_label_' + index;
|
|
|
|
|
const elem = this.document.querySelector(selector);
|
|
|
|
|
if (elem) {
|
|
|
|
|
elem.focus();
|
2021-06-08 10:41:46 +02:00
|
|
|
|
}
|
2021-11-07 15:21:27 +01:00
|
|
|
|
}
|
2021-04-28 12:15:49 +02:00
|
|
|
|
|
2021-11-07 15:21:27 +01:00
|
|
|
|
deleteChoiceField(index: number): void {
|
|
|
|
|
if (this.choices.length !== 1) {
|
|
|
|
|
this.choices.removeAt(index);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
initFormDefault(showDemoValues = true): void {
|
|
|
|
|
this.form = this.createFormGroup();
|
2021-11-12 12:50:21 +01:00
|
|
|
|
this.patchFormDefaultValues();
|
2021-11-07 15:21:27 +01:00
|
|
|
|
this.setDefaultDatesForInterval();
|
|
|
|
|
|
|
|
|
|
if (showDemoValues) {
|
|
|
|
|
this.setDemoValues();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get choices(): FormArray {
|
|
|
|
|
return this.form.get('choices') as FormArray;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reinitChoices(): void {
|
|
|
|
|
this.choices.setValue([]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addChoice(optionalLabel = ''): void {
|
|
|
|
|
const newControlGroup = this.fb.group({
|
|
|
|
|
label: this.fb.control('', [Validators.required]),
|
|
|
|
|
imageUrl: ['', [Validators.required]],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (optionalLabel) {
|
|
|
|
|
newControlGroup.patchValue({
|
|
|
|
|
label: optionalLabel,
|
|
|
|
|
imageUrl: 'mon url',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
this.choices.push(newControlGroup);
|
|
|
|
|
|
|
|
|
|
this.focusOnChoice(this.choices.length - 1);
|
2020-06-18 16:15:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-09 11:32:12 +01:00
|
|
|
|
/**
|
2021-11-07 15:21:27 +01:00
|
|
|
|
* make a uniq slug for the current poll creation
|
|
|
|
|
* @param form
|
2020-11-09 11:32:12 +01:00
|
|
|
|
*/
|
2021-11-07 15:21:27 +01:00
|
|
|
|
makeSlug(form: FormGroup): string {
|
2020-11-09 11:32:12 +01:00
|
|
|
|
let str = '';
|
|
|
|
|
str =
|
2021-11-07 15:21:27 +01:00
|
|
|
|
form.value.created_at.getFullYear() +
|
2020-11-09 11:32:12 +01:00
|
|
|
|
'_' +
|
2021-11-07 15:21:27 +01:00
|
|
|
|
(form.value.created_at.getMonth() + 1) +
|
2020-11-09 11:32:12 +01:00
|
|
|
|
'_' +
|
2021-11-07 15:21:27 +01:00
|
|
|
|
form.value.created_at.getDate() +
|
2020-11-09 11:32:12 +01:00
|
|
|
|
'_' +
|
2021-11-07 15:21:27 +01:00
|
|
|
|
form.value.creatorPseudo +
|
2020-11-09 11:32:12 +01:00
|
|
|
|
'_' +
|
2021-11-07 15:21:27 +01:00
|
|
|
|
form.value.title;
|
2020-11-09 11:32:12 +01:00
|
|
|
|
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
|
2021-11-07 15:21:27 +01:00
|
|
|
|
|
|
|
|
|
return str + '-' + this.uuidService.getUUID();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async saveCurrentPoll(): Promise<void> {
|
|
|
|
|
const pollUrl: Subscription = await this.apiService.createPoll(this._poll.getValue());
|
|
|
|
|
// TODO: Maybe handle the url to update currentPoll according to backend response
|
|
|
|
|
if (pollUrl) {
|
|
|
|
|
this.toastService.display('Le sondage a été enregistré.');
|
|
|
|
|
} else {
|
|
|
|
|
this.toastService.display('Le sondage n’a été correctement enregistré, veuillez ré-essayer.');
|
|
|
|
|
}
|
2020-04-21 17:26:25 +02:00
|
|
|
|
}
|
2020-04-19 14:22:10 +02:00
|
|
|
|
|
2021-04-28 12:01:09 +02:00
|
|
|
|
public saveParticipation(choice: Choice, user: Owner, response: Answer): void {
|
2021-11-07 15:21:27 +01:00
|
|
|
|
const currentPoll = this._poll.getValue();
|
|
|
|
|
currentPoll.choices.find((c) => c.name === choice.name)?.updateParticipation(user, response);
|
|
|
|
|
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-04-21 17:26:25 +02:00
|
|
|
|
}
|
2020-04-19 14:22:10 +02:00
|
|
|
|
|
2020-05-12 19:16:23 +02:00
|
|
|
|
public async deleteAllAnswers(): Promise<void> {
|
2021-04-30 10:59:46 +02:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2022-02-14 14:37:42 +01:00
|
|
|
|
public async addComment(comment: CommentDTO): Promise<void> {
|
|
|
|
|
await this.apiService.createComment(this._poll.getValue().custom_url, comment).then(
|
|
|
|
|
(resp) => {
|
|
|
|
|
console.log('resp', resp);
|
|
|
|
|
this.loadPollByCustomUrl(this._poll.getValue().custom_url);
|
|
|
|
|
console.log('resp', resp);
|
|
|
|
|
this.toastService.display('Votre commentaire a été enregistré.');
|
|
|
|
|
},
|
|
|
|
|
(err) => this.apiService.ousideHandleError(err)
|
|
|
|
|
);
|
2020-05-12 19:16:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async deleteComments(): Promise<void> {
|
2021-04-30 10:59:46 +02:00
|
|
|
|
await this.apiService.deletePollComments(this._poll.getValue().custom_url);
|
2020-06-16 18:40:48 +02:00
|
|
|
|
this.toastService.display('Les commentaires de ce sondage ont été supprimés.');
|
2020-04-21 17:26:25 +02:00
|
|
|
|
}
|
2020-06-12 19:17:39 +02:00
|
|
|
|
|
2021-11-07 15:21:27 +01:00
|
|
|
|
public buildAnswersByChoiceLabelByPseudo(poll: Poll): Map<string, Map<string, Answer>> {
|
|
|
|
|
const pseudos: Set<string> = new Set();
|
|
|
|
|
poll.choices.forEach((choice: Choice) => {
|
|
|
|
|
choice.participants.forEach((users: Set<Owner>) => {
|
|
|
|
|
users.forEach((user: Owner) => {
|
|
|
|
|
pseudos.add(user.pseudo);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2021-04-30 23:24:48 +02:00
|
|
|
|
|
2021-11-07 15:21:27 +01:00
|
|
|
|
const list = new Map<string, Map<string, Answer>>();
|
|
|
|
|
pseudos.forEach((pseudo: string) => {
|
|
|
|
|
list.set(
|
|
|
|
|
pseudo,
|
|
|
|
|
new Map<string, Answer>(
|
|
|
|
|
poll.choices.map((choice: Choice) => {
|
|
|
|
|
return [choice.name, undefined];
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
});
|
2021-05-20 12:34:49 +02:00
|
|
|
|
|
2021-11-07 15:21:27 +01:00
|
|
|
|
poll.choices.forEach((choice: Choice) => {
|
|
|
|
|
choice.participants.forEach((users: Set<Owner>, answer: Answer) => {
|
|
|
|
|
users.forEach((user: Owner) => {
|
|
|
|
|
list.get(user.pseudo).set(choice.name, answer);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2021-04-30 23:24:48 +02:00
|
|
|
|
|
2021-11-07 15:21:27 +01:00
|
|
|
|
return list;
|
2021-04-30 22:49:55 +02:00
|
|
|
|
}
|
2021-06-10 10:17:15 +02:00
|
|
|
|
|
2021-11-08 12:36:18 +01:00
|
|
|
|
public getParticipationUrlFromForm(): string {
|
|
|
|
|
return `${environment.frontDomain}#/poll/${this.form.value.custom_url}/consultation`;
|
|
|
|
|
}
|
2021-11-12 12:50:21 +01:00
|
|
|
|
|
2021-11-08 12:36:18 +01:00
|
|
|
|
public getAdministrationUrlFromForm(): string {
|
|
|
|
|
// admin_key is filled after creation
|
2021-11-14 17:25:57 +01:00
|
|
|
|
// example http://localhost:4200/#/administration/key/8Ubcg2YI99f69xz946cn4O64bQAeb
|
2021-11-12 15:59:44 +01:00
|
|
|
|
|
2021-11-14 17:25:57 +01:00
|
|
|
|
return `${environment.frontDomain}#/administration/key/${this.admin_key}`;
|
2021-11-08 12:36:18 +01:00
|
|
|
|
}
|
2021-11-12 12:50:21 +01:00
|
|
|
|
|
2021-11-08 12:09:35 +01:00
|
|
|
|
public getParticipationUrl(): string {
|
2021-11-08 10:56:03 +01:00
|
|
|
|
// http://localhost:4200/#/poll/dessin-anime/consultation
|
|
|
|
|
|
2022-02-10 12:16:11 +01:00
|
|
|
|
// handle secure access
|
2021-11-08 10:56:03 +01:00
|
|
|
|
// http://localhost:4200/#/poll/citron/consultation/secure/1c01ed9c94fc640a1be864f197ff808c
|
2022-02-10 12:16:11 +01:00
|
|
|
|
// http://localhost:4200/#/poll/citron/consultation/prompt pour entrer le pass à double hasher en md5
|
2021-11-08 10:56:03 +01:00
|
|
|
|
|
2021-11-08 12:09:35 +01:00
|
|
|
|
let url = '';
|
2022-02-10 11:15:48 +01:00
|
|
|
|
let suffix_password = '';
|
2021-11-08 12:09:35 +01:00
|
|
|
|
if (this._poll && this._poll.getValue) {
|
2022-02-10 11:15:48 +01:00
|
|
|
|
const currentPoll = this._poll.getValue();
|
|
|
|
|
if (currentPoll.password) {
|
|
|
|
|
// handle pass access
|
|
|
|
|
suffix_password = '/prompt';
|
|
|
|
|
}
|
|
|
|
|
if (currentPoll) {
|
|
|
|
|
url = `${environment.frontDomain}/#/poll/${currentPoll.custom_url}/consultation${suffix_password}`;
|
2022-02-09 11:30:22 +01:00
|
|
|
|
} else {
|
2022-02-10 11:15:48 +01:00
|
|
|
|
url = `${environment.frontDomain}/#/poll/${this.form.value.custom_url}/consultation${suffix_password}`;
|
2021-11-08 12:09:35 +01:00
|
|
|
|
}
|
2021-11-08 12:36:18 +01:00
|
|
|
|
} else {
|
2022-02-10 11:15:48 +01:00
|
|
|
|
url = `${environment.frontDomain}/#/poll/${this.form.value.custom_url}/consultation${suffix_password}`;
|
2021-11-08 12:09:35 +01:00
|
|
|
|
}
|
2021-11-08 12:36:18 +01:00
|
|
|
|
|
2021-11-08 12:09:35 +01:00
|
|
|
|
return url;
|
|
|
|
|
}
|
2021-11-12 12:50:21 +01:00
|
|
|
|
|
2021-11-08 12:09:35 +01:00
|
|
|
|
public getAdministrationUrl(): string {
|
2021-11-08 12:36:18 +01:00
|
|
|
|
// http://localhost:4200/#/admin/9S75b70ECXI5J5xDc058d3H40H9r2CHfO0Kj8T02EK2U8rY8fYTn-eS659j2Dhp794Oa6R1b9V70e3WGaE30iD9h45zwdm76C85SWB4LcUCrc7e0Ncc0
|
|
|
|
|
|
2021-11-08 12:09:35 +01:00
|
|
|
|
let url = '';
|
|
|
|
|
if (this._poll && this._poll.getValue) {
|
|
|
|
|
const polltemp = this._poll.getValue();
|
|
|
|
|
if (polltemp) {
|
2021-11-22 11:51:21 +01:00
|
|
|
|
url = `${environment.frontDomain}/#/admin/${polltemp.admin_key}`;
|
2021-11-08 12:09:35 +01:00
|
|
|
|
}
|
2021-11-08 12:36:18 +01:00
|
|
|
|
} else {
|
2021-11-22 11:51:21 +01:00
|
|
|
|
url = `${environment.frontDomain}/#/admin/${this.form.value.admin_key}`;
|
2021-11-08 12:09:35 +01:00
|
|
|
|
}
|
|
|
|
|
return url;
|
2021-11-08 10:56:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-08 12:09:35 +01:00
|
|
|
|
/**
|
|
|
|
|
* enrich vote stack with missing default votes
|
|
|
|
|
* @param vote_stack
|
|
|
|
|
*/
|
|
|
|
|
enrichVoteStackWithCurrentPollChoicesDefaultVotes(vote_stack: Stack) {
|
|
|
|
|
if (this._poll && this._poll.getValue) {
|
|
|
|
|
const polltemp = this._poll.getValue();
|
|
|
|
|
polltemp.choices.map((choice) => {
|
|
|
|
|
// for each vote, if it has the choice_id, do nothing, else, add a default vote
|
|
|
|
|
if (!this.findExistingVoteFromChoiceId(choice.id, vote_stack.votes)) {
|
|
|
|
|
vote_stack.votes.push(new Vote(choice.id));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-12 12:50:21 +01:00
|
|
|
|
|
2021-11-08 12:09:35 +01:00
|
|
|
|
/**
|
|
|
|
|
* find an existing vote in vote_stack from its choice_id
|
|
|
|
|
* @param choice_id
|
|
|
|
|
* @param votes
|
|
|
|
|
*/
|
|
|
|
|
findExistingVoteFromChoiceId(choice_id: number, votes: Vote[]) {
|
|
|
|
|
return votes.find((vote: Vote) => {
|
|
|
|
|
if (vote.choice_id === choice_id) {
|
|
|
|
|
return vote;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-11-12 12:50:21 +01:00
|
|
|
|
|
2021-11-17 16:25:24 +01:00
|
|
|
|
/**
|
|
|
|
|
* convertir les dates de la propriété Calendar en objets de saisie de texte
|
|
|
|
|
*/
|
|
|
|
|
convertCalendarToText() {
|
2022-02-14 10:50:19 +01:00
|
|
|
|
console.log('this.dateChoiceList', this.dateChoiceList);
|
2021-11-17 16:25:24 +01:00
|
|
|
|
|
2022-02-14 10:54:06 +01:00
|
|
|
|
if (this.calendar && this.calendar.length) {
|
|
|
|
|
let converted = [];
|
|
|
|
|
for (let someDate of this.calendar) {
|
|
|
|
|
converted.push(this.DateUtilitiesService.convertDateToDateChoiceObject(someDate));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.dateChoiceList = converted.sort((first: any, second: any) => {
|
|
|
|
|
return first.date_object - second.date_object;
|
|
|
|
|
});
|
|
|
|
|
return converted;
|
|
|
|
|
} else {
|
|
|
|
|
this.dateChoiceList = [];
|
|
|
|
|
}
|
|
|
|
|
return this.dateChoiceList;
|
2021-11-17 16:25:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* convert the DateChoices to an arrray of Dates for calendar picker
|
|
|
|
|
*/
|
2021-11-22 10:09:50 +01:00
|
|
|
|
convertTextToCalendar(): Date[] {
|
2021-11-17 16:52:16 +01:00
|
|
|
|
console.log('convert text to calendar', this.dateChoiceList);
|
2021-11-17 16:25:24 +01:00
|
|
|
|
let converted = [];
|
|
|
|
|
for (let someDateChoice of this.dateChoiceList) {
|
2021-11-17 16:52:16 +01:00
|
|
|
|
let dateObj = new Date(someDateChoice.date_input);
|
|
|
|
|
console.log('dateObj', dateObj);
|
|
|
|
|
// check that date is not part of the disabled dates
|
|
|
|
|
if (this.disabled_dates.indexOf(dateObj) === -1) {
|
|
|
|
|
converted.push(dateObj);
|
|
|
|
|
}
|
2021-11-17 16:25:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-17 16:52:16 +01:00
|
|
|
|
console.log('converted', converted);
|
2021-11-17 16:25:24 +01:00
|
|
|
|
this.calendar = converted;
|
2021-11-22 10:09:50 +01:00
|
|
|
|
return converted;
|
2021-11-08 09:32:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-12 15:59:44 +01:00
|
|
|
|
patchFormWithPoll(poll: Poll) {
|
|
|
|
|
this.form.patchValue({
|
|
|
|
|
...poll,
|
|
|
|
|
isAboutDate: poll.kind == 'date',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-08 12:09:35 +01:00
|
|
|
|
/**
|
|
|
|
|
* @description convert to API version 1 data transition object
|
|
|
|
|
*/
|
2021-11-12 12:50:21 +01:00
|
|
|
|
newPollFromForm(): Poll {
|
|
|
|
|
let form = this.form;
|
2021-11-08 12:09:35 +01:00
|
|
|
|
const newOwner = this.storageService.vote_stack.owner;
|
|
|
|
|
|
2022-02-09 10:31:06 +01:00
|
|
|
|
newOwner.pseudo = form.value.creatorPseudo;
|
|
|
|
|
newOwner.email = form.value.creatorEmail;
|
2021-11-08 12:09:35 +01:00
|
|
|
|
const newpoll = new Poll(newOwner, form.value.custom_url, form.value.title);
|
|
|
|
|
|
|
|
|
|
const pollKeys = Object.keys(newpoll);
|
|
|
|
|
const formFields = Object.keys(form.value);
|
|
|
|
|
newpoll.allowed_answers = [];
|
|
|
|
|
|
2021-11-12 12:50:21 +01:00
|
|
|
|
// comparer les champs de formulaire avec le DTO de création de sondage
|
2021-11-08 12:09:35 +01:00
|
|
|
|
for (const pk of pollKeys) {
|
|
|
|
|
if (formFields.indexOf(pk) !== -1) {
|
|
|
|
|
const field = form.value[pk];
|
|
|
|
|
newpoll[pk] = field;
|
|
|
|
|
} else {
|
2021-11-17 16:25:24 +01:00
|
|
|
|
console.log('newPollFromForm : manque pollKey', pk);
|
2021-11-08 12:09:35 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (form.value.isYesAnswerAvailable) {
|
|
|
|
|
newpoll.allowed_answers.push('yes');
|
|
|
|
|
}
|
|
|
|
|
if (form.value.isMaybeAnswerAvailable) {
|
|
|
|
|
newpoll.allowed_answers.push('maybe');
|
|
|
|
|
}
|
|
|
|
|
if (form.value.isNoAnswerAvailable) {
|
|
|
|
|
newpoll.allowed_answers.push('no');
|
|
|
|
|
}
|
|
|
|
|
newpoll.description = form.value.description;
|
|
|
|
|
newpoll.has_several_hours = form.value.hasSeveralHours;
|
2021-11-12 12:50:21 +01:00
|
|
|
|
newpoll.max_count_of_answers = form.value.maxCountOfAnswers;
|
2021-11-08 12:09:35 +01:00
|
|
|
|
newpoll.maxCountOfAnswers = form.value.maxCountOfAnswers;
|
|
|
|
|
newpoll.password = form.value.password;
|
2021-11-12 12:50:21 +01:00
|
|
|
|
newpoll.kind = form.value.isAboutDate ? 'date' : 'classic';
|
2021-11-08 12:09:35 +01:00
|
|
|
|
newpoll.allow_comments = form.value.allowComments;
|
|
|
|
|
// merge choices from storage
|
2021-11-12 12:50:21 +01:00
|
|
|
|
if (form.value.isAboutDate) {
|
2021-11-22 10:09:50 +01:00
|
|
|
|
// first we convert calendar picker dates.
|
|
|
|
|
// we want a list of date object, and we want the kind of dates who was lastly edited by the user
|
|
|
|
|
// depending on the manual or datepicker mode, we need to get a converted list of dates
|
|
|
|
|
let convertedDates = [];
|
|
|
|
|
if (this.mode_calendar) {
|
|
|
|
|
// mode calendar date picker, we take the list of date objects in calendar property
|
|
|
|
|
convertedDates = this.calendar;
|
|
|
|
|
} else {
|
|
|
|
|
// mode text, we convert to calendar list, and take that list
|
|
|
|
|
convertedDates = this.convertTextToCalendar();
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-12 12:50:21 +01:00
|
|
|
|
console.log('this.calendar', this.calendar);
|
|
|
|
|
|
2021-11-22 10:09:50 +01:00
|
|
|
|
for (let elem of convertedDates) {
|
2021-11-12 12:50:21 +01:00
|
|
|
|
console.log('elem', elem);
|
2021-11-17 16:52:16 +01:00
|
|
|
|
let converted_day = this.DateUtilitiesService.convertDateToDateChoiceObject(elem);
|
2021-11-12 12:50:21 +01:00
|
|
|
|
newpoll.dateChoices.push(converted_day);
|
|
|
|
|
}
|
|
|
|
|
console.log('newpoll.dateChoices', newpoll.dateChoices);
|
2022-02-09 12:25:08 +01:00
|
|
|
|
} else {
|
|
|
|
|
// text choices
|
|
|
|
|
newpoll.choicesText = Object.assign([], this.storageService.choicesText);
|
2021-11-08 12:36:18 +01:00
|
|
|
|
}
|
2021-11-08 12:09:35 +01:00
|
|
|
|
newpoll.choices = Object.assign([], this.storageService.choices);
|
2021-11-12 12:50:21 +01:00
|
|
|
|
// newpoll.dateChoices = Object.assign([], this.storageService.dateChoices);
|
2021-11-08 12:09:35 +01:00
|
|
|
|
newpoll.timeSlices = Object.assign([], this.storageService.timeSlices);
|
2021-11-08 12:36:18 +01:00
|
|
|
|
console.log('newpoll', newpoll);
|
2021-11-08 12:09:35 +01:00
|
|
|
|
return newpoll;
|
2021-06-10 10:17:15 +02:00
|
|
|
|
}
|
2022-02-10 11:15:48 +01:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* copy public url of new poll
|
|
|
|
|
* @param textToCopy
|
|
|
|
|
*/
|
|
|
|
|
copyText(textToCopy: string) {
|
|
|
|
|
this._clipboardService.copyFromContent(textToCopy);
|
2022-02-10 12:16:11 +01:00
|
|
|
|
|
2022-02-10 11:15:48 +01:00
|
|
|
|
this.translate.get('success.copy_message').subscribe((resp) => {
|
|
|
|
|
console.log('resp', resp);
|
|
|
|
|
this.toastService.display(`${resp} ${textToCopy}`);
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-04-19 14:22:10 +02:00
|
|
|
|
}
|