updated create-form of loan and book

This commit is contained in:
2025-11-23 12:14:45 +01:00
parent 0f75d1768b
commit 6b1f3ece94
4 changed files with 77 additions and 33 deletions

View File

@@ -1,30 +1,40 @@
import { Component } from '@angular/core';
import {Component, inject, input, OnInit, signal} from '@angular/core';
import {NzFormModule} from "ng-zorro-antd/form";
import {FormControl, FormGroup, ReactiveFormsModule, Validators} from "@angular/forms";
import {NzInputDirective} from "ng-zorro-antd/input";
import {NzOptionComponent, NzSelectComponent} from "ng-zorro-antd/select";
import {AuthorsService, GetAuthorDto, GetBookDto} from "../../services/api";
import {NzNotificationService} from "ng-zorro-antd/notification";
import {firstValueFrom} from "rxjs";
@Component({
selector: 'app-create-book',
imports: [NzFormModule, ReactiveFormsModule, NzInputDirective],
imports: [NzFormModule, ReactiveFormsModule, NzInputDirective, NzOptionComponent, NzSelectComponent],
templateUrl: './create-book.html',
styleUrls: ['./create-book.css'],
})
export class CreateBook {
export class CreateBook implements OnInit {
createBookForm = new FormGroup({
title: new FormControl<string>(null, [Validators.required]),
isbn: new FormControl<string>(null, [Validators.required]),
releaseYear: new FormControl<string>(null, [Validators.required]),
author: new FormControl<string>(null, [Validators.required])
releaseYear: new FormControl<number>(null, [Validators.required]),
authorId: new FormControl<number>(null, Validators.required)
})
submitForm() {
// Pour annuler si le formulaire est invalide
if (this.createBookForm.invalid) return;
private authorsService = inject(AuthorsService);
private notificationService = inject(NzNotificationService);
authors = signal<GetAuthorDto[]>([]);
// Pour obtenir la valeur du formulaire
console.log(this.createBookForm.getRawValue())
async fetchAuthors() {
try {
const authors = await firstValueFrom(this.authorsService.getAllAuthorsEndpoint());
this.authors.set(authors);
} catch (e) {
this.notificationService.error('Erreur', 'Impossible de récupérer les auteurs');
}
}
// Pour vider le formulaire
this.createBookForm.reset()
async ngOnInit() {
await this.fetchAuthors();
}
}