62 lines
1.9 KiB
TypeScript
62 lines
1.9 KiB
TypeScript
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>('modal');
|
|
createAuthor = viewChild.required<CreateAuthor>('createAuthor');
|
|
authorTable = viewChild.required<AuthorTable>('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'
|
|
)
|
|
}
|
|
}
|
|
}
|