Files
library-frontend/src/app/components/create-book/create-book.ts

45 lines
1.7 KiB
TypeScript

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, NzOptionComponent, NzSelectComponent],
templateUrl: './create-book.html',
styleUrls: ['./create-book.css'],
})
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<number>(null, [Validators.required]),
authorId: new FormControl<number>(null, Validators.required)
})
private authorsService = inject(AuthorsService);
private notificationService = inject(NzNotificationService);
authors = signal<GetAuthorDto[]>([]);
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');
}
}
async ngOnInit() {
await this.fetchAuthors();
}
filterAuthor(input: string, option: any) {
return option.nzLabel.toLowerCase().includes(input.toLowerCase());
}
}