Providers Pt1

This commit is contained in:
2026-06-02 18:12:49 +02:00
parent 67870e19ea
commit ea65e6a3dc
15 changed files with 217 additions and 144 deletions
@@ -1,34 +1,105 @@
import { Component } from '@angular/core';
import {FormControl, FormGroup, ReactiveFormsModule, Validators} from "@angular/forms";
import {NzFormModule} from "ng-zorro-antd/form";
import {NzInputDirective} from "ng-zorro-antd/input";
import {NzFlexDirective} from "ng-zorro-antd/flex";
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: [ReactiveFormsModule, NzFormModule, NzInputDirective,],
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 {
providerForm = new FormGroup({
lastName: new FormControl<string>(null, [Validators.required]),
firstName: new FormControl<string>(null, [Validators.required]),
phoneNumber: new FormControl<string>(null, [Validators.required]),
email: new FormControl<string>(null, [Validators.required]),
address: new FormControl<string>(null, [Validators.required]),
role: new FormControl<string>(null, [Validators.required]),
price: new FormControl<string>(null, [Validators.required]),
})
submitForm() {
// Pour annuler si le formulaire est invalide
if (this.providerForm.invalid) return;
private providersService = inject(ProvidersService);
private providertypesService = inject(ProvidertypesService);
private notificationService = inject(NzNotificationService)
// Pour obtenir la valeur du formulaire
console.log(this.providerForm.getRawValue())
// Pour vider le formulaire
this.providerForm.reset()
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);
}
}