front end version 2 de framadate, un gestionnaire de sondage
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

118 lines
3.4 KiB

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.');
let newpoll = this.pollService.newPollFromForm(this.form);
// TODO : save the poll
this.apiService.createPoll(newpoll);
}
}
public updateSlug() {
let newValueFormatted = 'TODO';
this.form.patchValue({ slug: newValueFormatted });
}
get choices() {
return this.form.get('choices') as FormArray;
}
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);
}
deleteChoiceField(index: number) {
if (this.choices.length !== 1) {
this.choices.removeAt(index);
}
}
reinitChoices() {
this.choices.setValue([]);
}
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.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() {
this.initFormDefault();
this.toastService.display('formulaire réinitialisé', 'info');
}
}