import { ChangeDetectorRef, Component, Inject, Input, OnInit } from '@angular/core'; import { ToastService } from '../../../../core/services/toast.service'; import { FormBuilder, FormGroup } from '@angular/forms'; import { UuidService } from '../../../../core/services/uuid.service'; import { PollService } from '../../../../core/services/poll.service'; import { ApiService } from '../../../../core/services/api.service'; import { Router } from '@angular/router'; import { DOCUMENT } from '@angular/common'; import { Poll } from '../../../../core/models/poll.model'; @Component({ selector: 'app-base-config', templateUrl: './base-config.component.html', styleUrls: ['./base-config.component.scss'], }) export class BaseConfigComponent { @Input() public poll?: Poll; @Input() public form: FormGroup; constructor( private fb: FormBuilder, private cd: ChangeDetectorRef, private uuidService: UuidService, private toastService: ToastService, private pollService: PollService, private apiService: ApiService, private router: Router, @Inject(DOCUMENT) private document: Document ) {} askInitFormDefault(): void { // this.initFormDefault(false); this.toastService.display('formulaire réinitialisé'); } public createPoll(): void { console.log('this.form', this.form); const newpoll = this.pollService.newPollFromForm(this.form); console.log('newpoll', newpoll); const router = this.router; if (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).then((resp) => { console.log('resp', resp); router.navigate(['success']); }); } else { this.toastService.display('invalid form'); } } public updateSlug(): void { const newValueFormatted = 'TODO'; this.form.patchValue({ slug: newValueFormatted }); } /** * set the poll slug from other data of the poll */ automaticSlug() { this.form.patchValue({ slug: this.pollService.makeSlug(this.form.value) }); } }