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

79 lines
2.4 KiB
TypeScript
Raw Normal View History

2021-04-25 12:05:17 +02:00
import { ChangeDetectorRef, Component, Inject, Input } from '@angular/core';
2021-02-04 16:14:07 +01:00
import { ToastService } from '../../../../core/services/toast.service';
2021-02-05 17:02:52 +01:00
import { FormBuilder, FormGroup } from '@angular/forms';
2021-02-04 16:14:07 +01:00
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';
2021-04-25 12:05:17 +02:00
import { environment } from '../../../../../environments/environment';
2021-02-04 16:14:07 +01:00
@Component({
selector: 'app-base-config',
templateUrl: './base-config.component.html',
styleUrls: ['./base-config.component.scss'],
})
2021-02-05 17:02:52 +01:00
export class BaseConfigComponent {
2021-02-04 16:14:07 +01:00
@Input()
public poll?: Poll;
@Input()
2021-02-04 16:14:07 +01:00
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 {
2021-02-05 15:34:00 +01:00
// this.initFormDefault(false);
2021-02-04 16:14:07 +01:00
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;
2021-04-25 12:05:17 +02:00
if (!environment.production) {
this.toastService.display('mode dev : envoi du form sans validation');
2021-02-05 17:02:52 +01:00
this.apiService.createPoll(newpoll).then((resp) => {
console.log('resp', resp);
router.navigate(['success']);
});
} else {
2021-04-25 12:05:17 +02:00
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');
}
2021-02-05 17:02:52 +01:00
}
2021-02-04 16:14:07 +01:00
}
public updateSlug(): void {
const newValueFormatted = 'TODO';
this.form.patchValue({ slug: newValueFormatted });
}
/**
* set the poll slug from other data of the poll
*/
automaticSlug() {
2021-02-05 17:02:52 +01:00
this.form.patchValue({ slug: this.pollService.makeSlug(this.form.value) });
2021-02-05 15:34:00 +01:00
}
2021-02-04 16:14:07 +01:00
}