diff --git a/src/app/components/create-book/create-book.html b/src/app/components/create-book/create-book.html index c916429..7726c8a 100644 --- a/src/app/components/create-book/create-book.html +++ b/src/app/components/create-book/create-book.html @@ -1,4 +1,4 @@ -
+ Titre @@ -25,7 +25,11 @@ - + + @for (author of authors(); track author.id) { + + } + diff --git a/src/app/components/create-book/create-book.ts b/src/app/components/create-book/create-book.ts index 2ea0c61..1987146 100644 --- a/src/app/components/create-book/create-book.ts +++ b/src/app/components/create-book/create-book.ts @@ -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(null, [Validators.required]), isbn: new FormControl(null, [Validators.required]), - releaseYear: new FormControl(null, [Validators.required]), - author: new FormControl(null, [Validators.required]) + releaseYear: new FormControl(null, [Validators.required]), + authorId: new FormControl(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([]); - // 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(); } } diff --git a/src/app/components/create-loan/create-loan.html b/src/app/components/create-loan/create-loan.html index 3e47157..5f4b071 100644 --- a/src/app/components/create-loan/create-loan.html +++ b/src/app/components/create-loan/create-loan.html @@ -1,21 +1,29 @@ - + - - Nom / Prénom + + Utilisateur - + + @for (user of users(); track user.id) { + + } + - + Livre - + + @for (book of books(); track book.id) { + + } + \ No newline at end of file diff --git a/src/app/components/create-loan/create-loan.ts b/src/app/components/create-loan/create-loan.ts index f3205ec..6874244 100644 --- a/src/app/components/create-loan/create-loan.ts +++ b/src/app/components/create-loan/create-loan.ts @@ -1,8 +1,11 @@ -import { Component } from '@angular/core'; +import {Component, inject, OnInit, signal} from '@angular/core'; import {FormControl, FormGroup, FormsModule, ReactiveFormsModule, Validators} from "@angular/forms"; import {NzColDirective, NzRowDirective} from "ng-zorro-antd/grid"; 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 {BooksService, GetBookDto, GetUserDto, UsersService} from "../../services/api"; +import {NzNotificationService} from "ng-zorro-antd/notification"; +import {firstValueFrom} from "rxjs"; @Component({ selector: 'app-create-loan', @@ -15,25 +18,44 @@ import {NzInputDirective} from "ng-zorro-antd/input"; NzFormLabelComponent, NzRowDirective, ReactiveFormsModule, - NzInputDirective + NzOptionComponent, + NzSelectComponent ], templateUrl: './create-loan.html', styleUrl: './create-loan.css', }) -export class CreateLoan { +export class CreateLoan implements OnInit { createLoanForm = new FormGroup({ - name: new FormControl(null, [Validators.required]), - book: new FormControl(null, [Validators.required]) + userId: new FormControl(null, Validators.required), + bookId: new FormControl(null, Validators.required) }) - submitForm() { - // Pour annuler si le formulaire est invalide - if (this.createLoanForm.invalid) return; + private userService = inject(UsersService); + private bookService = inject(BooksService); + private notificationService = inject(NzNotificationService); + users = signal([]); + books = signal([]); - // Pour obtenir la valeur du formulaire - console.log(this.createLoanForm.getRawValue()) + async fetchUsers() { + try { + const users = await firstValueFrom(this.userService.getAllUsersEndpoint()); + this.users.set(users); + } catch (e) { + this.notificationService.error('Erreur', 'Impossible de récupérer les utilisateurs'); + } + } - // Pour vider le formulaire - this.createLoanForm.reset() + async fetchBooks() { + try { + const books = await firstValueFrom(this.bookService.getAllBooksEndpoint()); + this.books.set(books); + } catch (e) { + this.notificationService.error('Erreur', 'Impossible de récupérer les livres'); + } + } + + async ngOnInit() { + await this.fetchUsers(); + await this.fetchBooks(); } }