2020-11-06 14:33:20 +01:00
|
|
|
import { ChangeDetectorRef, Component, Inject, Input, OnInit } from '@angular/core';
|
2020-10-29 18:43:19 +01:00
|
|
|
import { Poll } from '../../../core/models/poll.model';
|
2021-02-08 11:32:58 +01:00
|
|
|
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
|
2020-10-29 18:43:19 +01:00
|
|
|
import { ApiService } from '../../../core/services/api.service';
|
2020-11-03 16:13:47 +01:00
|
|
|
import { ToastService } from '../../../core/services/toast.service';
|
2020-11-05 19:13:43 +01:00
|
|
|
import { PollService } from '../../../core/services/poll.service';
|
2020-11-06 14:33:20 +01:00
|
|
|
import { DOCUMENT } from '@angular/common';
|
2020-12-16 17:21:01 +01:00
|
|
|
import { Router } from '@angular/router';
|
2021-04-30 10:59:46 +02:00
|
|
|
import { environment } from '../../../../environments/environment';
|
|
|
|
import { PollUtilitiesService } from '../../../core/services/poll.utilities.service';
|
2021-05-03 12:57:59 +02:00
|
|
|
import { StorageService } from '../../../core/services/storage.service';
|
2020-10-29 18:43:19 +01:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-admin-form',
|
|
|
|
templateUrl: './form.component.html',
|
|
|
|
styleUrls: ['./form.component.scss'],
|
|
|
|
})
|
|
|
|
export class FormComponent implements OnInit {
|
|
|
|
@Input()
|
|
|
|
public poll?: Poll;
|
2020-11-03 15:44:08 +01:00
|
|
|
public form: FormGroup;
|
2020-10-29 21:30:33 +01:00
|
|
|
|
2021-05-03 16:10:13 +02:00
|
|
|
public advancedDisplayEnabled = true;
|
2021-03-24 18:39:18 +01:00
|
|
|
public show_debug_data = false;
|
2021-05-03 12:36:03 +02:00
|
|
|
public currentStep = 'base';
|
|
|
|
public steps = ['base', 'choices', 'advanced'];
|
2021-05-03 16:10:13 +02:00
|
|
|
|
|
|
|
public environment = environment;
|
|
|
|
|
2020-11-03 16:13:47 +01:00
|
|
|
constructor(
|
|
|
|
private fb: FormBuilder,
|
2020-11-06 14:33:20 +01:00
|
|
|
private cd: ChangeDetectorRef,
|
2021-04-30 10:59:46 +02:00
|
|
|
private pollUtilitiesService: PollUtilitiesService,
|
2020-11-03 16:13:47 +01:00
|
|
|
private toastService: ToastService,
|
2020-11-05 19:13:43 +01:00
|
|
|
private pollService: PollService,
|
2021-05-03 12:57:59 +02:00
|
|
|
private storageService: StorageService,
|
2021-02-12 14:51:17 +01:00
|
|
|
public apiService: ApiService,
|
2020-12-16 17:21:01 +01:00
|
|
|
private router: Router,
|
2020-11-06 14:33:20 +01:00
|
|
|
@Inject(DOCUMENT) private document: any
|
2020-11-03 16:13:47 +01:00
|
|
|
) {}
|
2021-02-04 16:14:07 +01:00
|
|
|
|
2020-10-29 18:43:19 +01:00
|
|
|
ngOnInit(): void {
|
2020-11-03 15:44:08 +01:00
|
|
|
this.initFormDefault();
|
2021-05-03 12:36:03 +02:00
|
|
|
// this.goNextStep();
|
2020-10-29 18:43:19 +01:00
|
|
|
}
|
|
|
|
|
2021-04-30 10:59:46 +02:00
|
|
|
initFormDefault(showDemoValues = environment.autofill): void {
|
2021-02-08 18:55:31 +01:00
|
|
|
const creationDate = new Date();
|
|
|
|
|
2020-11-03 15:44:08 +01:00
|
|
|
this.form = this.fb.group({
|
2021-04-30 12:37:04 +02:00
|
|
|
title: ['', [Validators.required, Validators.minLength(5)]],
|
2020-11-03 15:44:08 +01:00
|
|
|
creatorPseudo: ['', [Validators.required]],
|
|
|
|
creatorEmail: ['', [Validators.required]],
|
2021-04-30 10:59:46 +02:00
|
|
|
custom_url: [this.pollUtilitiesService.makeUuid(), [Validators.required]],
|
2020-11-03 15:44:08 +01:00
|
|
|
description: ['', [Validators.required]],
|
2021-02-08 11:32:58 +01:00
|
|
|
choices: this.fb.array([
|
|
|
|
this.fb.group({
|
|
|
|
label: ['', [Validators.required]],
|
|
|
|
imageUrl: ['', [Validators.required]],
|
|
|
|
}),
|
|
|
|
]),
|
2021-02-08 18:55:31 +01:00
|
|
|
dateChoices: this.fb.array([
|
|
|
|
this.fb.group({
|
|
|
|
label: ['', [Validators.required]],
|
|
|
|
// if we have enabled detailed time choices per date choice, we have to make a time property for each date choice
|
|
|
|
timeChoices: this.fb.array([
|
|
|
|
this.fb.group({
|
|
|
|
label: ['', [Validators.required]],
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
}),
|
|
|
|
]),
|
2021-04-30 12:37:04 +02:00
|
|
|
// these time choice are meant to be the same for each day
|
2021-02-08 18:55:31 +01:00
|
|
|
timeChoices: this.fb.array([
|
|
|
|
this.fb.group({
|
|
|
|
label: ['', [Validators.required]],
|
|
|
|
}),
|
|
|
|
]),
|
2021-03-27 21:55:05 +01:00
|
|
|
kind: ['', [Validators.required]],
|
2021-04-30 12:37:04 +02:00
|
|
|
areResultsPublic: [true, [Validators.required]],
|
|
|
|
whoCanChangeAnswers: ['everybody', [Validators.required]],
|
|
|
|
isProtectedByPassword: [false, [Validators.required]],
|
2021-05-03 16:10:13 +02:00
|
|
|
allowNewDateTime: [false, [Validators.required]],
|
2021-04-30 12:37:04 +02:00
|
|
|
isOwnerNotifiedByEmailOnNewVote: [false, [Validators.required]],
|
|
|
|
isOwnerNotifiedByEmailOnNewComment: [false, [Validators.required]],
|
2021-05-03 16:10:13 +02:00
|
|
|
isYesAnswerAvailable: [false, [Validators.required]],
|
2021-04-30 12:37:04 +02:00
|
|
|
isMaybeAnswerAvailable: [false, [Validators.required]],
|
2021-05-03 16:10:13 +02:00
|
|
|
isNoAnswerAvailable: [false, [Validators.required]],
|
2021-04-30 12:37:04 +02:00
|
|
|
isAboutDate: [true, [Validators.required]],
|
|
|
|
isZeroKnoledge: [false, [Validators.required]],
|
|
|
|
expiresDaysDelay: [60, [Validators.required, Validators.min(1)]],
|
|
|
|
maxCountOfAnswers: [150, [Validators.required, Validators.min(1)]],
|
|
|
|
allowComments: [true, [Validators.required]],
|
2021-05-04 09:33:28 +02:00
|
|
|
password: ['', []],
|
2021-04-30 12:37:04 +02:00
|
|
|
dateCreated: [creationDate, [Validators.required]],
|
2021-05-03 15:19:06 +02:00
|
|
|
hasSeveralHours: [false, [Validators.required]],
|
2021-04-30 12:37:04 +02:00
|
|
|
hasMaxCountOfAnswers: [true, [Validators.required, Validators.min(1)]],
|
2020-11-06 14:33:20 +01:00
|
|
|
startDateInterval: ['', [Validators.required]],
|
|
|
|
endDateInterval: ['', [Validators.required]],
|
2020-11-03 15:44:08 +01:00
|
|
|
});
|
|
|
|
console.log('this.form ', this.form);
|
2020-11-06 14:33:20 +01:00
|
|
|
|
|
|
|
if (showDemoValues) {
|
|
|
|
this.setDemoValues();
|
2021-02-08 11:32:58 +01:00
|
|
|
this.toastService.display('default values filled for demo');
|
2020-11-06 14:33:20 +01:00
|
|
|
}
|
2021-04-30 11:27:54 +02:00
|
|
|
|
|
|
|
if (environment.autoSendNewPoll) {
|
|
|
|
this.createPoll();
|
|
|
|
}
|
2020-11-06 14:33:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-05 23:03:49 +01:00
|
|
|
* add example values to the form, overrides defaults of PollConfiguration
|
2020-11-06 14:33:20 +01:00
|
|
|
*/
|
2020-11-06 12:04:38 +01:00
|
|
|
setDemoValues(): void {
|
2021-05-03 12:36:03 +02:00
|
|
|
const title = 'le titre de démo oh oh ' + new Date().getTime();
|
2020-11-03 15:44:08 +01:00
|
|
|
this.form.patchValue({
|
2021-04-30 10:59:46 +02:00
|
|
|
title: title,
|
|
|
|
custom_url: this.pollUtilitiesService.makeSlugFromString(title),
|
2021-04-30 12:37:04 +02:00
|
|
|
description: 'répondez SVP <3 ! *-*',
|
2020-11-03 16:13:47 +01:00
|
|
|
creatorPseudo: 'Chuck Norris',
|
2020-11-09 12:44:24 +01:00
|
|
|
creatorEmail: 'chucknorris@example.com',
|
2020-11-03 16:13:47 +01:00
|
|
|
isAboutDate: true,
|
2021-05-03 15:19:06 +02:00
|
|
|
hasSeveralHours: false,
|
2021-03-27 21:55:05 +01:00
|
|
|
kind: 'date',
|
2021-04-30 23:24:48 +02:00
|
|
|
password: '',
|
2021-04-30 12:37:04 +02:00
|
|
|
whoCanChangeAnswers: 'everybody',
|
|
|
|
isProtectedByPassword: false,
|
|
|
|
isOwnerNotifiedByEmailOnNewVote: false,
|
|
|
|
isOwnerNotifiedByEmailOnNewComment: false,
|
2021-05-03 16:10:13 +02:00
|
|
|
isYesAnswerAvailable: true,
|
2021-04-30 12:37:04 +02:00
|
|
|
isMaybeAnswerAvailable: false,
|
2021-05-03 16:10:13 +02:00
|
|
|
isNoAnswerAvailable: false,
|
|
|
|
isZeroKnoledge: true,
|
2021-04-30 12:37:04 +02:00
|
|
|
areResultsPublic: true,
|
2021-04-30 23:24:48 +02:00
|
|
|
allowComments: true,
|
2021-04-30 12:37:04 +02:00
|
|
|
expiresDaysDelay: 60,
|
2021-04-30 23:24:48 +02:00
|
|
|
maxCountOfAnswers: 150,
|
2021-05-03 16:10:13 +02:00
|
|
|
allowNewDateTime: false,
|
2021-05-04 09:33:28 +02:00
|
|
|
startDateInterval: new Date(),
|
|
|
|
endDateInterval: new Date(),
|
2021-02-05 15:34:00 +01:00
|
|
|
comments: [],
|
2021-04-30 10:59:46 +02:00
|
|
|
choices: [
|
|
|
|
{
|
|
|
|
name: 'un choix',
|
|
|
|
url: 'https://placekitten.com/120/150',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
dateChoices: [
|
|
|
|
{
|
|
|
|
name: new Date(),
|
|
|
|
},
|
|
|
|
],
|
2021-02-05 15:34:00 +01:00
|
|
|
timeChoices: [],
|
2020-11-03 15:44:08 +01:00
|
|
|
});
|
2020-11-09 11:32:12 +01:00
|
|
|
}
|
2021-04-30 10:59:46 +02:00
|
|
|
|
|
|
|
goPreviousStep() {
|
|
|
|
alert('todo');
|
|
|
|
}
|
2021-05-03 16:10:13 +02:00
|
|
|
|
2021-04-30 10:59:46 +02:00
|
|
|
goNextStep() {
|
2021-05-03 12:36:03 +02:00
|
|
|
console.log('this.steps', this.steps);
|
|
|
|
let indexCurrentStep = this.steps.indexOf(this.currentStep);
|
|
|
|
indexCurrentStep += 1;
|
|
|
|
this.currentStep = this.steps[indexCurrentStep];
|
|
|
|
window.scrollTo(0, 0);
|
2021-04-30 10:59:46 +02:00
|
|
|
}
|
2021-04-30 11:27:54 +02:00
|
|
|
|
|
|
|
public createPoll(): void {
|
|
|
|
console.log('this.form', this.form);
|
|
|
|
const newpoll = this.pollService.newPollFromForm(this.form);
|
|
|
|
console.log('newpoll', newpoll);
|
|
|
|
const router = this.router;
|
|
|
|
|
|
|
|
if (!environment.production) {
|
|
|
|
this.toastService.display('mode dev : envoi du form sans validation');
|
2021-05-03 12:57:59 +02:00
|
|
|
this.apiService.createPoll(newpoll).then(
|
|
|
|
(resp: any) => {
|
|
|
|
this.pollService.updateCurrentPoll(resp.data.poll);
|
|
|
|
this.storageService.userPolls.push(resp.data.poll);
|
|
|
|
this.storageService.vote_stack.owner.polls.push(resp.data.poll);
|
|
|
|
this.toastService.display('sauvegarde du nouveau sondage réussie');
|
|
|
|
router.navigate(['success']);
|
|
|
|
},
|
|
|
|
(err) => {
|
|
|
|
this.toastService.display('erreur lors de la sauvegarde');
|
|
|
|
}
|
|
|
|
);
|
2021-04-30 11:27:54 +02:00
|
|
|
} else {
|
|
|
|
if (this.form.valid) {
|
2021-05-03 12:57:59 +02:00
|
|
|
this.apiService.createPoll(newpoll).then(
|
|
|
|
(resp: any) => {
|
|
|
|
this.pollService.updateCurrentPoll(resp.data.poll);
|
|
|
|
this.storageService.userPolls.push(resp.data.poll);
|
|
|
|
this.storageService.vote_stack.owner.polls.push(resp.data.poll);
|
|
|
|
this.toastService.display('sauvegarde du nouveau sondage réussie');
|
|
|
|
router.navigate(['success']);
|
|
|
|
},
|
|
|
|
(err) => {
|
|
|
|
this.toastService.display('erreur lors de la sauvegarde');
|
|
|
|
}
|
|
|
|
);
|
2021-04-30 11:27:54 +02:00
|
|
|
} else {
|
|
|
|
this.toastService.display('invalid form');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-29 18:43:19 +01:00
|
|
|
}
|