176 lines
5.5 KiB
TypeScript
176 lines
5.5 KiB
TypeScript
import { ChangeDetectorRef, Component, Inject, Input, OnInit } from '@angular/core';
|
|
import { Poll } from '../../../core/models/poll.model';
|
|
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
|
|
import { UuidService } from '../../../core/services/uuid.service';
|
|
import { ApiService } from '../../../core/services/api.service';
|
|
import { ToastService } from '../../../core/services/toast.service';
|
|
import { PollService } from '../../../core/services/poll.service';
|
|
import { DOCUMENT } from '@angular/common';
|
|
import { Router } from '@angular/router';
|
|
import { environment } from '../../../../environments/environment';
|
|
import { PollUtilitiesService } from '../../../core/services/poll.utilities.service';
|
|
|
|
@Component({
|
|
selector: 'app-admin-form',
|
|
templateUrl: './form.component.html',
|
|
styleUrls: ['./form.component.scss'],
|
|
})
|
|
export class FormComponent implements OnInit {
|
|
@Input()
|
|
public poll?: Poll;
|
|
public form: FormGroup;
|
|
|
|
public advancedDisplayEnabled = false;
|
|
public show_debug_data = false;
|
|
|
|
constructor(
|
|
private fb: FormBuilder,
|
|
private cd: ChangeDetectorRef,
|
|
private pollUtilitiesService: PollUtilitiesService,
|
|
private toastService: ToastService,
|
|
private pollService: PollService,
|
|
public apiService: ApiService,
|
|
private router: Router,
|
|
@Inject(DOCUMENT) private document: any
|
|
) {}
|
|
|
|
ngOnInit(): void {
|
|
this.initFormDefault();
|
|
}
|
|
|
|
initFormDefault(showDemoValues = environment.autofill): void {
|
|
const creationDate = new Date();
|
|
|
|
this.form = this.fb.group({
|
|
title: ['', [Validators.required, Validators.minLength(5)]],
|
|
creatorPseudo: ['', [Validators.required]],
|
|
creatorEmail: ['', [Validators.required]],
|
|
custom_url: [this.pollUtilitiesService.makeUuid(), [Validators.required]],
|
|
description: ['', [Validators.required]],
|
|
choices: this.fb.array([
|
|
this.fb.group({
|
|
label: ['', [Validators.required]],
|
|
imageUrl: ['', [Validators.required]],
|
|
}),
|
|
]),
|
|
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]],
|
|
}),
|
|
]),
|
|
}),
|
|
]),
|
|
// these time choice are meant to be the same for each day
|
|
timeChoices: this.fb.array([
|
|
this.fb.group({
|
|
label: ['', [Validators.required]],
|
|
}),
|
|
]),
|
|
kind: ['', [Validators.required]],
|
|
areResultsPublic: [true, [Validators.required]],
|
|
whoCanChangeAnswers: ['everybody', [Validators.required]],
|
|
isProtectedByPassword: [false, [Validators.required]],
|
|
isOwnerNotifiedByEmailOnNewVote: [false, [Validators.required]],
|
|
isOwnerNotifiedByEmailOnNewComment: [false, [Validators.required]],
|
|
isMaybeAnswerAvailable: [false, [Validators.required]],
|
|
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]],
|
|
password: [this.pollUtilitiesService.makeUuid(), [Validators.required]],
|
|
dateCreated: [creationDate, [Validators.required]],
|
|
hasSeveralHours: [true, [Validators.required]],
|
|
hasMaxCountOfAnswers: [true, [Validators.required, Validators.min(1)]],
|
|
startDateInterval: ['', [Validators.required]],
|
|
endDateInterval: ['', [Validators.required]],
|
|
});
|
|
console.log('this.form ', this.form);
|
|
|
|
if (showDemoValues) {
|
|
this.setDemoValues();
|
|
this.toastService.display('default values filled for demo');
|
|
}
|
|
|
|
if (environment.autoSendNewPoll) {
|
|
this.createPoll();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* add example values to the form, overrides defaults of PollConfiguration
|
|
*/
|
|
setDemoValues(): void {
|
|
const title = 'le titre de démo oh oh';
|
|
this.form.patchValue({
|
|
title: title,
|
|
custom_url: this.pollUtilitiesService.makeSlugFromString(title),
|
|
description: 'répondez SVP <3 ! *-*',
|
|
creatorPseudo: 'Chuck Norris',
|
|
creatorEmail: 'chucknorris@example.com',
|
|
isAboutDate: true,
|
|
// hasSeveralHours: true,
|
|
kind: 'date',
|
|
whoCanChangeAnswers: 'everybody',
|
|
isProtectedByPassword: false,
|
|
isOwnerNotifiedByEmailOnNewVote: false,
|
|
isOwnerNotifiedByEmailOnNewComment: false,
|
|
isMaybeAnswerAvailable: false,
|
|
areResultsPublic: true,
|
|
expiresDaysDelay: 60,
|
|
comments: [],
|
|
choices: [
|
|
{
|
|
name: 'un choix',
|
|
url: 'https://placekitten.com/120/150',
|
|
},
|
|
],
|
|
dateChoices: [
|
|
{
|
|
name: new Date(),
|
|
},
|
|
],
|
|
timeChoices: [],
|
|
});
|
|
}
|
|
|
|
goPreviousStep() {
|
|
alert('todo');
|
|
}
|
|
goNextStep() {
|
|
alert('todo');
|
|
}
|
|
|
|
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');
|
|
this.apiService.createPoll(newpoll).then((resp) => {
|
|
console.log('resp', resp);
|
|
router.navigate(['success']);
|
|
});
|
|
} else {
|
|
if (this.form.valid) {
|
|
console.log('Le sondage est correctement rempli, prêt à enregistrer.');
|
|
const newpoll = this.pollService.newPollFromForm(this.form);
|
|
this.apiService.createPoll(newpoll).then((resp) => {
|
|
console.log('resp', resp);
|
|
router.navigate(['poll', this.form.value.custom_url, 'consultation']);
|
|
|
|
this.toastService.display('sauvegarde du nouveau sondage réussie');
|
|
});
|
|
} else {
|
|
this.toastService.display('invalid form');
|
|
}
|
|
}
|
|
}
|
|
}
|