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

84 lines
2.9 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 { DateService } from '../../../core/services/date.service';
import { ApiService } from '../../../core/services/api.service';
2020-10-29 21:30:33 +01:00
import { Choice } from '../../../core/models/choice.model';
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;
public pollFormGroup: FormGroup;
public configurationFormGroup: FormGroup;
2020-10-29 21:30:33 +01:00
public choicesFormArray: FormArray; // possible choices to answer
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 {
this.pollFormGroup = this.fb.group({
title: [this.poll ? this.poll.title : '', [Validators.required]],
slug: [this.poll ? this.poll.slug : this.uuidService.getUUID(), [Validators.required]],
description: [this.poll ? this.poll.description : ''],
});
2020-10-29 21:30:33 +01:00
// add dynamically elements to add choices
this.choicesFormArray = this.fb.array([
{
choices: this.poll.choices.forEach((elem: Choice) => {
return {
label: [elem.label, [Validators.required]],
imageUrl: [elem.imageUrl, null],
};
}),
},
]);
2020-10-29 18:43:19 +01:00
this.configurationFormGroup = this.fb.group({
isAboutDate: [this.poll ? this.poll.configuration.isAboutDate : false, [Validators.required]],
isProtectedByPassword: [
this.poll ? this.poll.configuration.isProtectedByPassword : false,
[Validators.required],
],
isOwnerNotifiedByEmailOnNewVote: [
this.poll ? this.poll.configuration.isOwnerNotifiedByEmailOnNewVote : false,
[Validators.required],
],
isOwnerNotifiedByEmailOnNewComment: [
this.poll ? this.poll.configuration.isOwnerNotifiedByEmailOnNewComment : false,
[Validators.required],
],
isMaybeAnswerAvailable: [
this.poll ? this.poll.configuration.isMaybeAnswerAvailable : false,
[Validators.required],
],
areResultsPublic: [this.poll ? this.poll.configuration.areResultsPublic : true, [Validators.required]],
expiracyNumberOfDays: [
this.poll ? DateService.diffInDays(new Date(), this.poll.configuration.expires) : 60,
[Validators.required],
],
});
}
public createPoll(): void {
if (this.pollFormGroup.valid && this.configurationFormGroup.valid) {
console.log('Le sondage est correctement rempli, prêt à enregistrer.');
// TODO : save the poll
this.apiService.createPoll(this.poll);
}
}
public updateSlug() {
let newValueFormatted = 'TODO';
2020-10-29 21:30:33 +01:00
this.pollFormGroup.patchValue({ slug: newValueFormatted });
2020-10-29 18:43:19 +01:00
}
}