import { Component, Input, OnInit } from '@angular/core'; import { Poll } from '../../../core/models/poll.model'; import { FormArray, 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'; @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 urlPrefix: string = window.location.origin + '/participation/'; public advancedDisplayEnabled = false; constructor( private fb: FormBuilder, private uuidService: UuidService, private toastService: ToastService, private pollService: PollService, private apiService: ApiService ) {} ngOnInit(): void { this.initFormDefault(); } public createPoll(): void { if (this.form.valid && this.form.valid) { console.log('Le sondage est correctement rempli, prêt à enregistrer.'); const newpoll = this.pollService.newPollFromForm(this.form); // TODO : save the poll this.apiService.createPoll(newpoll); } } public updateSlug(): void { const newValueFormatted = 'TODO'; this.form.patchValue({ slug: newValueFormatted }); } get choices(): FormArray { return this.form.get('choices') as FormArray; } addChoice(optionalLabel = ''): void { const newControlGroup = this.fb.group({ label: this.fb.control('', [Validators.required]), imageUrl: ['', [Validators.required]], }); if (optionalLabel) { newControlGroup.patchValue({ label: optionalLabel, }); } this.choices.push(newControlGroup); } deleteChoiceField(index: number): void { if (this.choices.length !== 1) { this.choices.removeAt(index); } } reinitChoices(): void { this.choices.setValue([]); } initFormDefault(): void { 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.setDemoValues(); } setDemoValues(): void { this.addChoice('orange'); this.addChoice('raisin'); this.addChoice('abricot'); this.form.patchValue({ title: 'mon titre', description: 'répondez SVP <3 ! *-* ', slug: this.uuidService.getUUID(), creatorPseudo: 'Chuck Norris', creatorEmail: '', choices: ['matin', 'midi'], isAboutDate: true, isProtectedByPassword: false, isOwnerNotifiedByEmailOnNewVote: false, isOwnerNotifiedByEmailOnNewComment: false, isMaybeAnswerAvailable: false, areResultsPublic: true, expiracyNumberOfDays: 60, }); } askInitFormDefault(): void { this.initFormDefault(); this.toastService.display('formulaire réinitialisé'); } }