diff --git a/src/app/components/author-table/author-table.html b/src/app/components/author-table/author-table.html index 539f33d..b0a5001 100644 --- a/src/app/components/author-table/author-table.html +++ b/src/app/components/author-table/author-table.html @@ -12,9 +12,9 @@ @for (author of authors(); track author.id) { - {{ author.name}} - {{ author.firstName }} - + {{ author.firstName}} + {{ author.name }} + diff --git a/src/app/components/loan-table/loan-table.html b/src/app/components/loan-table/loan-table.html index 8869ee7..589798d 100644 --- a/src/app/components/loan-table/loan-table.html +++ b/src/app/components/loan-table/loan-table.html @@ -21,7 +21,9 @@
- + + +
@@ -33,7 +35,7 @@ \ No newline at end of file diff --git a/src/app/components/loan-table/loan-table.ts b/src/app/components/loan-table/loan-table.ts index 5a23847..da5d628 100644 --- a/src/app/components/loan-table/loan-table.ts +++ b/src/app/components/loan-table/loan-table.ts @@ -1,4 +1,4 @@ -import {Component, inject, OnInit, signal} from '@angular/core'; +import {Component, inject, OnInit, signal, viewChild} from '@angular/core'; import {DatePipe} from "@angular/common"; import {NzTableComponent} from "ng-zorro-antd/table"; import {UpdateLoan} from "../update-loan/update-loan"; @@ -8,6 +8,7 @@ import {firstValueFrom} from "rxjs"; import {NzDividerComponent} from "ng-zorro-antd/divider"; import {ModalIcon} from "../modal-icon/modal-icon"; import {NzIconDirective} from "ng-zorro-antd/icon"; +import {format} from "date-fns"; @Component({ selector: 'app-loan-table', @@ -25,16 +26,16 @@ import {NzIconDirective} from "ng-zorro-antd/icon"; export class LoanTable implements OnInit { private loansService = inject(LoansService); private notificationService = inject(NzNotificationService) - loans = signal([]); - loansLoading = signal(false); + updateLoan = viewChild.required('updateLoan'); + modal = viewChild.required('modalIcon'); async ngOnInit() { - await this.fetchloans(); + await this.fetchLoans(); } - async fetchloans() { + async fetchLoans() { this.loansLoading.set(true) try { @@ -64,6 +65,95 @@ export class LoanTable implements OnInit { 'Impossible de supprimer la ligne' ) } - await this.fetchloans(); + await this.fetchLoans(); + } + + async edit(id: number, updateLoanComponent: UpdateLoan) { + if (updateLoanComponent.updateLoanForm.invalid) { + this.notificationService.error( + 'Erreur', + 'Erreur d\'écriture dans le formulaire' + ) + return; + } + + try { + const plannedDate = updateLoanComponent.updateLoanForm.get('plannedReturningDate').value; + const plannedDateResult = format(plannedDate, 'yyyy-MM-dd'); + + const effectiveDate = updateLoanComponent.updateLoanForm.get('effectiveReturningDate').value; + const effectiveDateResult = format(effectiveDate, 'yyyy-MM-dd'); + + const loans = updateLoanComponent.updateLoanForm.getRawValue(); + loans.plannedReturningDate = plannedDateResult; + loans.effectiveReturningDate = effectiveDateResult; + + const updatedLoanDto = { + id: this.selectedLoan.id, + bookId: updateLoanComponent.updateLoanForm.get('bookId').value, + userId: updateLoanComponent.updateLoanForm.get('userId').value, + date: this.selectedLoan.date, + plannedReturningDate: loans.plannedReturningDate, + effectiveReturningDate: loans.effectiveReturningDate + }; + + await firstValueFrom(this.loansService.updateLoanEndpoint(id, updatedLoanDto)) + + this.notificationService.success( + 'Success', + 'Emprunt modifié' + ) + } catch (e) { + this.notificationService.error( + 'Erreur', + 'Erreur lors de la modification' + ) + } + } + + async validationDate(loanId: number) { + try { + const PatchLoanValue = { + effectiveReturningDate: format(new Date(), 'yyyy-MM-dd') + }; + + try { + await firstValueFrom(this.loansService.patchLoanEndpoint(loanId, PatchLoanValue)) + + this.notificationService.success( + 'Success', + 'Date de retour actualisée' + ) + } catch (e) { + this.notificationService.error( + 'Erreur', + 'La date à déjà été actualisée' + ) + } + } catch (e) { + this.notificationService.error( + 'Erreur', + 'Erreur d\'actualisation de la date de retour' + ) + } + + await this.fetchLoans() + } + + selectedLoan: GetLoanDto | null = null; + openEditModal(loan: GetLoanDto) { + this.selectedLoan = { ...loan }; + this.modal().showModal(); + } + + async onModalOk(loanId: number, updateLoanComponent: UpdateLoan, modal: ModalIcon) { + await this.edit(loanId, updateLoanComponent); + updateLoanComponent.updateLoanForm.reset(); + modal.isVisible = false; + await this.fetchLoans(); + } + + onModalCancel(modal: ModalIcon) { + modal.isVisible = false; } } diff --git a/src/app/components/update-author/update-author.html b/src/app/components/update-author/update-author.html index 9f61d5c..3022a95 100644 --- a/src/app/components/update-author/update-author.html +++ b/src/app/components/update-author/update-author.html @@ -1,14 +1,4 @@
- - - Nom - - - - - - - Prénom @@ -18,4 +8,14 @@ + + + + Nom + + + + + +
\ No newline at end of file diff --git a/src/app/components/update-book/update-book.ts b/src/app/components/update-book/update-book.ts index bf98f91..0a04e01 100644 --- a/src/app/components/update-book/update-book.ts +++ b/src/app/components/update-book/update-book.ts @@ -26,6 +26,17 @@ export class UpdateBook implements OnInit { authors = signal([]); book = input.required() + ngOnChanges() { + if (this.book) { + this.updateBookForm.patchValue({ + title: this.book().title, + isbn: this.book().isbn, + releaseYear: this.book().releaseYear, + authorId: this.book().bookAuthorId + }); + } + } + async fetchAuthors() { try { const authors = await firstValueFrom(this.authorsService.getAllAuthorsEndpoint()); @@ -38,15 +49,4 @@ export class UpdateBook implements OnInit { async ngOnInit() { await this.fetchAuthors(); } - - ngOnChanges() { - if (this.book) { - this.updateBookForm.patchValue({ - title: this.book().title, - isbn: this.book().isbn, - releaseYear: this.book().releaseYear, - authorId: this.book().authorId - }); - } - } } diff --git a/src/app/components/update-loan/update-loan.html b/src/app/components/update-loan/update-loan.html index 5093113..e785279 100644 --- a/src/app/components/update-loan/update-loan.html +++ b/src/app/components/update-loan/update-loan.html @@ -1,11 +1,15 @@
- Nom / Prénom + Utilisateur - - + + + @for (user of users(); track user.id) { + + } + @@ -14,8 +18,12 @@ Livre - - + + + @for (book of books(); track book.id) { + + } + @@ -25,7 +33,7 @@ - + @@ -35,7 +43,7 @@ - + \ No newline at end of file diff --git a/src/app/components/update-loan/update-loan.ts b/src/app/components/update-loan/update-loan.ts index 5096cff..d85f676 100644 --- a/src/app/components/update-loan/update-loan.ts +++ b/src/app/components/update-loan/update-loan.ts @@ -1,12 +1,15 @@ -import {Component, input} from '@angular/core'; +import {Component, inject, input, 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 {GetBookDto, GetLoanDto} from "../../services/api"; +import {BooksService, GetBookDto, GetLoanDto, GetUserDto, UsersService} from "../../services/api"; +import {NzDatePickerComponent} from "ng-zorro-antd/date-picker"; +import {NzOptionComponent, NzSelectComponent} from "ng-zorro-antd/select"; +import {NzNotificationService} from "ng-zorro-antd/notification"; +import {firstValueFrom} from "rxjs"; @Component({ - selector: 'app-update-loan', + selector: 'app-update-loan', imports: [ FormsModule, NzColDirective, @@ -14,30 +17,71 @@ import {GetBookDto, GetLoanDto} from "../../services/api"; NzFormDirective, NzFormItemComponent, NzFormLabelComponent, - NzInputDirective, NzRowDirective, - ReactiveFormsModule + ReactiveFormsModule, + NzDatePickerComponent, + NzOptionComponent, + NzSelectComponent ], - templateUrl: './update-loan.html', - styleUrl: './update-loan.css', + templateUrl: './update-loan.html', + styleUrl: './update-loan.css', }) -export class UpdateLoan { +export class UpdateLoan implements OnInit { updateLoanForm = new FormGroup({ - name: new FormControl(null, [Validators.required]), - book: new FormControl(null, [Validators.required]), - plannedDate: new FormControl(null, [Validators.required]), - effectiveDate: new FormControl(null, [Validators.required]), - }) + userId: new FormControl(null, Validators.required), + bookId: new FormControl(null, Validators.required), + plannedReturningDate: new FormControl(null, Validators.required), + effectiveReturningDate: new FormControl(null), + }); + private userService = inject(UsersService); + private bookService = inject(BooksService); + private notificationService = inject(NzNotificationService); + users = signal([]); + books = signal([]); loan = input.required() + ngOnChanges() { if (this.loan) { - this.updateLoanForm.patchValue({ - name: this.loan().userName, - book: this.loan().bookTitle, - plannedDate: new Date(this.loan().plannedReturningDate), - effectiveDate: new Date(this.loan().effectiveReturningDate) - }); + if (this.loan().effectiveReturningDate != null) { + this.updateLoanForm.patchValue({ + userId: this.loan().userId, + bookId: this.loan().bookId, + plannedReturningDate: new Date(this.loan().plannedReturningDate), + effectiveReturningDate: new Date(this.loan().effectiveReturningDate) + }); + + } else { + this.updateLoanForm.patchValue({ + userId: this.loan().userId, + bookId: this.loan().bookId, + plannedReturningDate: new Date(this.loan().plannedReturningDate), + effectiveReturningDate: null + }); + } } } + + 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'); + } + } + + 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(); + } } diff --git a/src/app/components/user-table/user-table.ts b/src/app/components/user-table/user-table.ts index 9d46a4f..4e5349c 100644 --- a/src/app/components/user-table/user-table.ts +++ b/src/app/components/user-table/user-table.ts @@ -78,7 +78,7 @@ export class UserTable implements OnInit { } try { - const rawDate = updateUserComponent.updateUserForm.get('birthDate')?.value; + const rawDate = updateUserComponent.updateUserForm.get('birthDate').value; const birthDate = format(rawDate, 'yyyy-MM-dd'); const users = updateUserComponent.updateUserForm.getRawValue();