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

119 lines
3.4 KiB
TypeScript
Raw Normal View History

2020-10-29 18:43:19 +01:00
import { Component, Input, OnInit } from '@angular/core';
import { Poll } from '../../../core/models/poll.model';
2020-10-29 21:30:33 +01:00
import { FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms';
2020-10-29 18:43:19 +01:00
import { UuidService } from '../../../core/services/uuid.service';
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-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
2020-10-29 18:43:19 +01:00
public urlPrefix: string = window.location.origin + '/participation/';
2020-11-05 19:13:43 +01:00
public advancedDisplayEnabled = false;
2020-10-29 18:43:19 +01:00
2020-11-03 16:13:47 +01:00
constructor(
private fb: FormBuilder,
private uuidService: UuidService,
private toastService: ToastService,
2020-11-05 19:13:43 +01:00
private pollService: PollService,
2020-11-03 16:13:47 +01:00
private apiService: ApiService
) {}
2020-10-29 18:43:19 +01:00
ngOnInit(): void {
2020-11-03 15:44:08 +01:00
this.initFormDefault();
2020-10-29 18:43:19 +01:00
}
public createPoll(): void {
2020-11-03 15:44:08 +01:00
if (this.form.valid && this.form.valid) {
2020-10-29 18:43:19 +01:00
console.log('Le sondage est correctement rempli, prêt à enregistrer.');
2020-11-05 19:13:43 +01:00
let newpoll = this.pollService.newPollFromForm(this.form);
2020-10-29 18:43:19 +01:00
// TODO : save the poll
2020-11-05 19:13:43 +01:00
this.apiService.createPoll(newpoll);
2020-10-29 18:43:19 +01:00
}
}
2020-11-03 15:44:08 +01:00
2020-10-29 18:43:19 +01:00
public updateSlug() {
let newValueFormatted = 'TODO';
2020-11-03 15:44:08 +01:00
this.form.patchValue({ slug: newValueFormatted });
2020-10-29 18:43:19 +01:00
}
2020-10-31 17:36:54 +01:00
get choices() {
2020-11-03 15:44:08 +01:00
return this.form.get('choices') as FormArray;
2020-10-31 17:36:54 +01:00
}
2020-11-03 15:44:08 +01:00
2020-11-05 19:13:43 +01:00
addChoice(optionalLabel: string = '') {
let newControlGroup = this.fb.group({
label: this.fb.control('', [Validators.required]),
imageUrl: ['', [Validators.required]],
});
if (optionalLabel) {
newControlGroup.patchValue({
label: optionalLabel,
});
}
this.choices.push(newControlGroup);
2020-11-03 15:44:08 +01:00
}
deleteChoiceField(index: number) {
if (this.choices.length !== 1) {
this.choices.removeAt(index);
}
}
reinitChoices() {
this.choices.setValue([]);
}
2020-11-03 16:13:47 +01:00
initFormDefault() {
2020-11-03 15:44:08 +01:00
this.form = this.fb.group({
title: ['', [Validators.required, Validators.minLength(12)]],
creatorPseudo: ['', [Validators.required]],
creatorEmail: ['', [Validators.required]],
slug: [this.uuidService.getUUID(), [Validators.required]],
description: ['', [Validators.required]],
choices: new FormArray([]),
isAboutDate: [true, [Validators.required]],
isProtectedByPassword: [false, [Validators.required]],
isOwnerNotifiedByEmailOnNewVote: [false, [Validators.required]],
isOwnerNotifiedByEmailOnNewComment: [false, [Validators.required]],
isMaybeAnswerAvailable: [false, [Validators.required]],
areResultsPublic: [true, [Validators.required]],
expiracyNumberOfDays: [60, [Validators.required, Validators.min(0)]],
});
console.log('this.form ', this.form);
2020-11-05 19:13:43 +01:00
this.addChoice('orange');
this.addChoice('raisin');
this.addChoice('abricot');
2020-11-03 16:13:47 +01:00
2020-11-03 15:44:08 +01:00
this.form.patchValue({
title: 'mon titre',
description: 'répondez SVP <3 ! *-* ',
2020-11-03 16:13:47 +01:00
slug: this.uuidService.getUUID(),
creatorPseudo: 'Chuck Norris',
creatorEmail: '',
2020-11-03 15:44:08 +01:00
choices: ['matin', 'midi'],
2020-11-03 16:13:47 +01:00
isAboutDate: true,
isProtectedByPassword: false,
isOwnerNotifiedByEmailOnNewVote: false,
isOwnerNotifiedByEmailOnNewComment: false,
isMaybeAnswerAvailable: false,
areResultsPublic: true,
expiracyNumberOfDays: 60,
2020-11-03 15:44:08 +01:00
});
2020-11-03 16:13:47 +01:00
}
askInitFormDefault() {
this.initFormDefault();
this.toastService.display('formulaire réinitialisé', 'info');
2020-10-31 17:36:54 +01:00
}
2020-10-29 18:43:19 +01:00
}