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-04-30 10:59:46 +02:00
|
|
|
import { PollUtilitiesService } from '../../../../core/services/poll.utilities.service';
|
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;
|
2021-02-04 19:04:20 +01:00
|
|
|
@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,
|
2021-04-30 10:59:46 +02:00
|
|
|
private utilitiesService: PollUtilitiesService,
|
2021-02-04 16:14:07 +01:00
|
|
|
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);
|
|
|
|
this.apiService.createPoll(newpoll).then((resp) => {
|
|
|
|
console.log('resp', resp);
|
|
|
|
router.navigate(['success']);
|
2021-04-30 10:59:46 +02:00
|
|
|
|
|
|
|
this.toastService.display('sauvegarde du nouveau sondage réussie');
|
2021-04-25 12:05:17 +02:00
|
|
|
});
|
|
|
|
} 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 {
|
2021-04-30 10:59:46 +02:00
|
|
|
const newValueFormatted = this.pollService.makecustom_url(this.poll);
|
2021-02-04 16:14:07 +01:00
|
|
|
this.form.patchValue({ slug: newValueFormatted });
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-04-30 10:59:46 +02:00
|
|
|
* set the poll custom_url from other data of the poll
|
2021-02-04 16:14:07 +01:00
|
|
|
*/
|
|
|
|
automaticSlug() {
|
2021-04-30 10:59:46 +02:00
|
|
|
this.form.patchValue({ slug: this.utilitiesService.makeUuid() });
|
2021-02-05 15:34:00 +01:00
|
|
|
}
|
2021-02-04 16:14:07 +01:00
|
|
|
}
|