Files
pyrofetes/src/app/pages/providers/providers-add-form/providers-add-form.ts
T
2026-06-02 18:12:49 +02:00

106 lines
3.3 KiB
TypeScript

import {Component, inject, signal} from '@angular/core';
import {FormControl, FormGroup, FormsModule, ReactiveFormsModule, Validators} from "@angular/forms";
import {NzButtonComponent} from "ng-zorro-antd/button";
import {NzColDirective, NzRowDirective} from "ng-zorro-antd/grid";
import {NzFlexDirective} from "ng-zorro-antd/flex";
import {NzFormControlComponent, NzFormDirective, NzFormItemComponent, NzFormLabelComponent} from "ng-zorro-antd/form";
import {NzInputDirective} from "ng-zorro-antd/input";
import {NzOptionComponent, NzSelectComponent} from "ng-zorro-antd/select";
import {
CreateCustomerDto,
CustomersService,
CustomertypesService,
GetCustomerTypeDto,
ProvidersService, ProvidertypesService
} from "../../../services/api";
import {NzNotificationService} from "ng-zorro-antd/notification";
import {firstValueFrom} from "rxjs";
@Component({
selector: 'app-providers-add-form',
imports: [
FormsModule,
NzButtonComponent,
NzColDirective,
NzFlexDirective,
NzFormControlComponent,
NzFormDirective,
NzFormItemComponent,
NzFormLabelComponent,
NzInputDirective,
NzOptionComponent,
NzRowDirective,
NzSelectComponent,
ReactiveFormsModule
],
templateUrl: './providers-add-form.html',
styleUrl: './providers-add-form.css',
})
export class ProvidersAddForm {
private providersService = inject(ProvidersService);
private providertypesService = inject(ProvidertypesService);
private notificationService = inject(NzNotificationService)
providerForm = new FormGroup({
Price: new FormControl<string>(null, [Validators.required]),
providerTypeId: new FormControl<number>(null, [Validators.required]),
})
providerPost = signal<CreateCustomerDto>(this.providerForm.value);
providersLoading = signal<boolean>(false);
async submitForm() {
// Pour annuler si le formulaire est invalide
if (this.providerForm.invalid) return;
// Pour obtenir la valeur du formulaire
console.log(this.providerForm.getRawValue())
this.providerPost.set(this.providerForm.getRawValue())
await this.createCustomers(this.providerPost().note, this.providerPost().customerTypeId)
// Pour vider le formulaire
this.providerForm.reset()
}
async createCustomers(note: string, customerTypeId: number) {
this.providersLoading.set(true);
const providerValue = {
note: note,
customerTypeId: customerTypeId
}
try {
const provider = await firstValueFrom(this.providersService.createProviderEndpoint(providerValue));
this.providerPost.set(provider);
console.log(provider);
} catch (e)
{
this.notificationService.error('Erreur de recherche', "L\'auteur n\'existe pas !");
this.notificationService.error('Erreur', ' (ou Erreur de communication avec l\'API)');
}
this.providersLoading.set(false);
}
customerTypes = signal<GetCustomerTypeDto[]>([])
async ngOnInit() {
await this.fetchCustomerTypes()
}
async fetchCustomerTypes() {
this.customersLoading.set(true);
try {
const customerType = await firstValueFrom(this.customertypesService.getAllCustomerTypeEndpoint())
this.customerTypes.set(customerType)
} catch (e) {
this.notificationService.error('Erreur', 'Erreur de communication avec l\'API');
}
this.customersLoading.set(false);
}
}