add form array for choices

This commit is contained in:
Baptiste Lemoine 2020-10-29 21:30:33 +01:00
parent 8cac3d1862
commit d9301559ef
2 changed files with 23 additions and 3 deletions

View File

@ -52,6 +52,12 @@
> >
<i class="fa fa-close"></i> <i class="fa fa-close"></i>
</button> </button>
<h2>Choix de réponses</h2>
<pre class="debug padded warning">
choicesFormArray :
{{ choicesFormArray | json }}
</pre
>
<label for="slug" <label for="slug"
>Url pour les participants >Url pour les participants

View File

@ -1,9 +1,10 @@
import { Component, Input, OnInit } from '@angular/core'; import { Component, Input, OnInit } from '@angular/core';
import { Poll } from '../../../core/models/poll.model'; import { Poll } from '../../../core/models/poll.model';
import { FormBuilder, FormGroup, Validators } from '@angular/forms'; import { FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms';
import { UuidService } from '../../../core/services/uuid.service'; import { UuidService } from '../../../core/services/uuid.service';
import { DateService } from '../../../core/services/date.service'; import { DateService } from '../../../core/services/date.service';
import { ApiService } from '../../../core/services/api.service'; import { ApiService } from '../../../core/services/api.service';
import { Choice } from '../../../core/models/choice.model';
@Component({ @Component({
selector: 'app-admin-form', selector: 'app-admin-form',
@ -15,6 +16,9 @@ export class FormComponent implements OnInit {
public poll?: Poll; public poll?: Poll;
public pollFormGroup: FormGroup; public pollFormGroup: FormGroup;
public configurationFormGroup: FormGroup; public configurationFormGroup: FormGroup;
public choicesFormArray: FormArray; // possible choices to answer
public longFormVersionEnabled = true; public longFormVersionEnabled = true;
public urlPrefix: string = window.location.origin + '/participation/'; public urlPrefix: string = window.location.origin + '/participation/';
@ -27,6 +31,17 @@ export class FormComponent implements OnInit {
slug: [this.poll ? this.poll.slug : this.uuidService.getUUID(), [Validators.required]], slug: [this.poll ? this.poll.slug : this.uuidService.getUUID(), [Validators.required]],
description: [this.poll ? this.poll.description : ''], description: [this.poll ? this.poll.description : ''],
}); });
// 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],
};
}),
},
]);
this.configurationFormGroup = this.fb.group({ this.configurationFormGroup = this.fb.group({
isAboutDate: [this.poll ? this.poll.configuration.isAboutDate : false, [Validators.required]], isAboutDate: [this.poll ? this.poll.configuration.isAboutDate : false, [Validators.required]],
@ -63,7 +78,6 @@ export class FormComponent implements OnInit {
} }
public updateSlug() { public updateSlug() {
let newValueFormatted = 'TODO'; let newValueFormatted = 'TODO';
this.pollFormGroup.patchValue({ slug: newValueFormatted });
} }
// this.pollFormGroup.setValue({'slug' : newValueFormatted);
// }
} }