updated create-form of loan and book

This commit is contained in:
2025-11-23 13:41:55 +01:00
parent 6b1f3ece94
commit af1dec318f
8 changed files with 147 additions and 15 deletions

View File

@@ -6,6 +6,7 @@ import {NzOptionComponent, NzSelectComponent} from "ng-zorro-antd/select";
import {BooksService, GetBookDto, GetUserDto, UsersService} from "../../services/api"; import {BooksService, GetBookDto, GetUserDto, UsersService} from "../../services/api";
import {NzNotificationService} from "ng-zorro-antd/notification"; import {NzNotificationService} from "ng-zorro-antd/notification";
import {firstValueFrom} from "rxjs"; import {firstValueFrom} from "rxjs";
import {format} from "date-fns";
@Component({ @Component({
selector: 'app-create-loan', selector: 'app-create-loan',
@@ -27,7 +28,8 @@ import {firstValueFrom} from "rxjs";
export class CreateLoan implements OnInit { export class CreateLoan implements OnInit {
createLoanForm = new FormGroup({ createLoanForm = new FormGroup({
userId: new FormControl<number>(null, Validators.required), userId: new FormControl<number>(null, Validators.required),
bookId: new FormControl<number>(null, Validators.required) bookId: new FormControl<number>(null, Validators.required),
date: new FormControl<string>(format(new Date(), 'yyyy-MM-dd'))
}) })
private userService = inject(UsersService); private userService = inject(UsersService);

View File

@@ -1,8 +1,8 @@
<app-modal type="primary" [name]="'Ajouter un auteur'"> <app-modal #modal type="primary" [name]="'Ajouter un auteur'" (ok)="onModalOk()" (cancel)="onModalCancel()">>
<app-create-author></app-create-author> <app-create-author #createAuthor></app-create-author>
</app-modal> </app-modal>
<section class="mt-5"> <section class="mt-5">
<app-author-table></app-author-table> <app-author-table #authorTable></app-author-table>
</section> </section>

View File

@@ -1,7 +1,10 @@
import {Component} from '@angular/core'; import {Component, inject, viewChild} from '@angular/core';
import {CreateAuthor} from "../../components/create-author/create-author"; import {CreateAuthor} from "../../components/create-author/create-author";
import {Modal} from "../../components/modal/modal"; import {Modal} from "../../components/modal/modal";
import {AuthorTable} from "../../components/author-table/author-table"; 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({ @Component({
selector: 'app-author', selector: 'app-author',
@@ -14,5 +17,45 @@ import {AuthorTable} from "../../components/author-table/author-table";
styleUrl: './author.css', styleUrl: './author.css',
}) })
export class Author{ 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'
)
}
}
} }

View File

@@ -1,8 +1,8 @@
<app-modal type="primary" [name]="'Ajouter un livre'"> <app-modal #modal type="primary" [name]="'Ajouter un livre'" (ok)="onModalOk()" (cancel)="onModalCancel()">
<app-create-book></app-create-book> <app-create-book #createBook></app-create-book>
</app-modal> </app-modal>
<section class="mt-5"> <section class="mt-5">
<app-book-table></app-book-table> <app-book-table #bookTable></app-book-table>
</section> </section>

View File

@@ -1,7 +1,10 @@
import {Component, inject, OnInit, signal} from '@angular/core'; import {Component, inject, viewChild} from '@angular/core';
import {BookTable} from "../../components/book-table/book-table"; import {BookTable} from "../../components/book-table/book-table";
import {Modal} from "../../components/modal/modal"; import {Modal} from "../../components/modal/modal";
import {CreateBook} from "../../components/create-book/create-book"; import {CreateBook} from "../../components/create-book/create-book";
import {BooksService} from "../../services/api";
import {NzNotificationService} from "ng-zorro-antd/notification";
import {firstValueFrom} from "rxjs";
@Component({ @Component({
selector: 'app-book', selector: 'app-book',
@@ -14,4 +17,45 @@ import {CreateBook} from "../../components/create-book/create-book";
styleUrls: ['./book.css'], styleUrls: ['./book.css'],
}) })
export class Book{ export class Book{
modal = viewChild.required<Modal>('modal');
createBook = viewChild.required<CreateBook>('createBook');
booksTable = viewChild.required<BookTable>('bookTable');
private booksService = inject(BooksService);
private notificationService = inject(NzNotificationService)
async onModalOk() {
await this.addBook()
this.createBook().createBookForm.reset();
this.modal().isVisible = false;
await this.booksTable().fetchBooks()
}
onModalCancel() {
this.modal().isVisible = false;
}
async addBook() {
if (this.createBook().createBookForm.invalid)
{
this.notificationService.error(
'Erreur',
'Erreur d\'écriture dans le formulaire'
)
}
try {
const books = this.createBook().createBookForm.getRawValue();
await firstValueFrom(this.booksService.createBookEndpoint(books))
this.notificationService.success(
'Success',
'Enregistrement réussi'
)
} catch (e) {
this.notificationService.error(
'Erreur',
'Erreur d\'enregistrement'
)
}
}
} }

View File

@@ -1,7 +1,7 @@
<app-modal type="primary" [name]="'Ajouter un emprunt'"> <app-modal #modal type="primary" [name]="'Ajouter un emprunt'" (ok)="onModalOk()" (cancel)="onModalCancel()">
<app-create-loan></app-create-loan> <app-create-loan #createLoan></app-create-loan>
</app-modal> </app-modal>
<div class="mt-5"> <div class="mt-5">
<app-loan-table></app-loan-table> <app-loan-table #loanTable></app-loan-table>
</div> </div>

View File

@@ -1,7 +1,11 @@
import { Component } from '@angular/core'; import {Component, inject, viewChild} from '@angular/core';
import {Modal} from "../../components/modal/modal"; import {Modal} from "../../components/modal/modal";
import {CreateLoan} from "../../components/create-loan/create-loan"; import {CreateLoan} from "../../components/create-loan/create-loan";
import {LoanTable} from "../../components/loan-table/loan-table"; import {LoanTable} from "../../components/loan-table/loan-table";
import {LoansService} from "../../services/api";
import {NzNotificationService} from "ng-zorro-antd/notification";
import {format} from "date-fns";
import {firstValueFrom} from "rxjs";
@Component({ @Component({
selector: 'app-loan', selector: 'app-loan',
@@ -14,5 +18,44 @@ import {LoanTable} from "../../components/loan-table/loan-table";
styleUrl: './loan.css', styleUrl: './loan.css',
}) })
export class Loan { export class Loan {
modal = viewChild.required<Modal>('modal');
createLoan = viewChild.required<CreateLoan>('createLoan');
loansTable = viewChild.required<LoanTable>('loanTable');
private loansService = inject(LoansService);
private notificationService = inject(NzNotificationService)
async onModalOk() {
await this.addLoan()
this.createLoan().createLoanForm.reset();
this.modal().isVisible = false;
await this.loansTable().fetchLoans()
}
onModalCancel() {
this.modal().isVisible = false;
}
async addLoan() {
if (this.createLoan().createLoanForm.invalid)
{
this.notificationService.error(
'Erreur',
'Erreur d\'écriture dans le formulaire'
)
}
try {
const loans = this.createLoan().createLoanForm.getRawValue();
await firstValueFrom(this.loansService.createLoanEndpoint(loans))
this.notificationService.success(
'Success',
'Enregistrement réussi'
)
} catch (e) {
this.notificationService.error(
'Erreur',
'Erreur d\'enregistrement'
)
}
}
} }

View File

@@ -1,8 +1,8 @@
import {Component, inject, OnInit, viewChild} from '@angular/core'; import {Component, inject, viewChild} from '@angular/core';
import {Modal} from "../../components/modal/modal"; import {Modal} from "../../components/modal/modal";
import {CreateUser} from "../../components/create-user/create-user"; import {CreateUser} from "../../components/create-user/create-user";
import {UserTable} from "../../components/user-table/user-table"; import {UserTable} from "../../components/user-table/user-table";
import {CreateUserDto, UsersService} from "../../services/api"; import {UsersService} from "../../services/api";
import {NzNotificationService} from "ng-zorro-antd/notification"; import {NzNotificationService} from "ng-zorro-antd/notification";
import {firstValueFrom} from "rxjs"; import {firstValueFrom} from "rxjs";
import { format } from 'date-fns'; import { format } from 'date-fns';