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

90 lines
2.5 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';
@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 longFormVersionEnabled = true;
public urlPrefix: string = window.location.origin + '/participation/';
constructor(private fb: FormBuilder, private uuidService: UuidService, private apiService: ApiService) {}
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.');
// TODO : save the poll
this.apiService.createPoll(this.poll);
}
}
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-10-31 17:36:54 +01:00
addChoice() {
2020-11-03 15:44:08 +01:00
this.choices.push(
this.fb.group({
label: this.fb.control('', [Validators.required]),
imageUrl: ['', [Validators.required]],
})
);
}
deleteChoiceField(index: number) {
if (this.choices.length !== 1) {
this.choices.removeAt(index);
}
}
reinitChoices() {
this.choices.setValue([]);
this.addChoice();
}
private initFormDefault() {
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);
this.form.patchValue({
title: 'mon titre',
description: 'répondez SVP <3 ! *-* ',
choices: ['matin', 'midi'],
});
// this.addChoice();
2020-10-31 17:36:54 +01:00
}
2020-10-29 18:43:19 +01:00
}