funky-framadate-front/src/app/features/administration/form/form.component.ts

208 lines
6.8 KiB
TypeScript
Raw Normal View History

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';
import { DOCUMENT } from '@angular/common';
2020-12-16 17:21:01 +01:00
import { Router } from '@angular/router';
import { environment } from '../../../../environments/environment';
import { PollUtilitiesService } from '../../../core/services/poll.utilities.service';
import { StorageService } from '../../../core/services/storage.service';
2021-05-04 09:42:56 +02:00
import { defaultDates, defaultTimeOfDay } from '../../../../../mocks/old-stuff/config/defaultConfigs';
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;
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,
private cd: ChangeDetectorRef,
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,
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,
@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();
// this.goNextStep();
2020-10-29 18:43:19 +01: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]],
custom_url: [this.pollUtilitiesService.makeUuid(), [Validators.required]],
2020-11-03 15:44:08 +01:00
description: ['', [Validators.required]],
2021-05-04 09:42:56 +02:00
// 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]],
// }),
// ]),
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]],
hasSeveralHours: [false, [Validators.required]],
2021-04-30 12:37:04 +02:00
hasMaxCountOfAnswers: [true, [Validators.required, Validators.min(1)]],
startDateInterval: ['', [Validators.required]],
endDateInterval: ['', [Validators.required]],
2020-11-03 15:44:08 +01:00
});
console.log('this.form ', this.form);
if (showDemoValues) {
this.setDemoValues();
2021-02-08 11:32:58 +01:00
this.toastService.display('default values filled for demo');
}
2021-04-30 11:27:54 +02:00
if (environment.autoSendNewPoll) {
this.createPoll();
}
}
/**
* add example values to the form, overrides defaults of PollConfiguration
*/
2020-11-06 12:04:38 +01:00
setDemoValues(): void {
const title = 'le titre de démo oh oh ' + new Date().getTime();
2020-11-03 15:44:08 +01:00
this.form.patchValue({
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,
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-05-04 09:42:56 +02:00
// choices: [
// {
// name: 'un choix',
// url: 'https://placekitten.com/120/150',
// },
// ],
// dateChoices: defaultDates,
// timeChoices: defaultTimeOfDay,
2020-11-03 15:44:08 +01:00
});
}
goPreviousStep() {
alert('todo');
}
2021-05-03 16:10:13 +02:00
goNextStep() {
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 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');
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) {
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
}