import {Component, inject, viewChild} from '@angular/core'; import {CreateAuthor} from "../../components/create-author/create-author"; import {Modal} from "../../components/modal/modal"; import {AuthorTable} from "../../components/author-table/author-table"; import {AuthorsService} from "../../services/api"; import {NzNotificationService} from "ng-zorro-antd/notification"; import {firstValueFrom} from "rxjs"; @Component({ selector: 'app-author', imports: [ CreateAuthor, Modal, AuthorTable ], templateUrl: './author.html', styleUrl: './author.css', }) export class Author{ modal = viewChild.required('modal'); createAuthor = viewChild.required('createAuthor'); authorTable = viewChild.required('authorTable'); private authorsService = inject(AuthorsService); private notificationService = inject(NzNotificationService) async onModalOk() { await this.addAuthor() this.createAuthor().createAuthorForm.reset(); this.modal().isVisible = false; await this.authorTable().fetchAuthors() } onModalCancel() { this.modal().isVisible = false; } async addAuthor() { if (this.createAuthor().createAuthorForm.invalid) { this.notificationService.error( 'Erreur', 'Erreur d\'écriture dans le formulaire' ) } try { const authors = this.createAuthor().createAuthorForm.getRawValue(); await firstValueFrom(this.authorsService.createAuthorEndpoint(authors)) this.notificationService.success( 'Success', 'Utilisateur crée' ) } catch (e) { this.notificationService.error( 'Erreur', 'Erreur d\'enregistrement' ) } } }