diff --git a/src/app/components/modal-icon/modal-icon.html b/src/app/components/modal-icon/modal-icon.html index 102ad5c..1401a39 100644 --- a/src/app/components/modal-icon/modal-icon.html +++ b/src/app/components/modal-icon/modal-icon.html @@ -1,5 +1,5 @@
- +
@@ -8,7 +8,7 @@ () + name = input.required(); isVisible = false; isOkLoading = false; + ok = output(); + cancel = output(); + showModal(): void { this.isVisible = true; } @@ -25,12 +28,13 @@ export class ModalIcon { handleOk(): void { this.isOkLoading = true; setTimeout(() => { - this.isVisible = false; + this.ok.emit(); this.isOkLoading = false; }, 1000); } handleCancel(): void { + this.cancel.emit(); this.isVisible = false; } } \ No newline at end of file diff --git a/src/app/components/update-user/update-user.html b/src/app/components/update-user/update-user.html index 1de870f..52de56a 100644 --- a/src/app/components/update-user/update-user.html +++ b/src/app/components/update-user/update-user.html @@ -1,4 +1,4 @@ -
+ Nom @@ -30,12 +30,12 @@ - + Anniversaire - - + +
\ No newline at end of file diff --git a/src/app/components/update-user/update-user.ts b/src/app/components/update-user/update-user.ts index b839445..c989115 100644 --- a/src/app/components/update-user/update-user.ts +++ b/src/app/components/update-user/update-user.ts @@ -1,8 +1,12 @@ -import { Component } from '@angular/core'; +import {Component, input} 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 {NzDatePickerComponent} from "ng-zorro-antd/date-picker"; +import {format} from "date-fns"; +import {firstValueFrom} from "rxjs"; +import {GetUserDto} from "../../services/api"; @Component({ selector: 'app-update-user', @@ -15,7 +19,8 @@ import {NzInputDirective} from "ng-zorro-antd/input"; NzFormLabelComponent, NzInputDirective, NzRowDirective, - ReactiveFormsModule + ReactiveFormsModule, + NzDatePickerComponent ], templateUrl: './update-user.html', styleUrl: './update-user.css', @@ -28,14 +33,15 @@ export class UpdateUser { birthDate: new FormControl(null, [Validators.required]), }) - submitForm() { - // Pour annuler si le formulaire est invalide - if (this.updateUserForm.invalid) return; - - // Pour obtenir la valeur du formulaire - console.log(this.updateUserForm.getRawValue()) - - // Pour vider le formulaire - this.updateUserForm.reset() + user = input.required() + ngOnChanges() { + if (this.user) { + this.updateUserForm.patchValue({ + name: this.user().name, + firstName: this.user().firstName, + email: this.user().email, + birthDate: new Date(this.user().birthDate) + }); + } } } diff --git a/src/app/components/user-table/user-table.html b/src/app/components/user-table/user-table.html index 50869cd..e15eec3 100644 --- a/src/app/components/user-table/user-table.html +++ b/src/app/components/user-table/user-table.html @@ -45,8 +45,13 @@
- - + +
diff --git a/src/app/components/user-table/user-table.ts b/src/app/components/user-table/user-table.ts index c572182..68e9dcd 100644 --- a/src/app/components/user-table/user-table.ts +++ b/src/app/components/user-table/user-table.ts @@ -1,4 +1,4 @@ -import {Component, inject, OnInit, signal} from '@angular/core'; +import {Component, inject, input, OnInit, signal, viewChild} from '@angular/core'; import {NzTableComponent} from "ng-zorro-antd/table"; import {Modal} from "../modal/modal"; import {DatePipe} from "@angular/common"; @@ -9,6 +9,8 @@ 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"; +import {CreateUser} from "../create-user/create-user"; @Component({ selector: 'app-user-table', @@ -29,6 +31,8 @@ export class UserTable implements OnInit { private notificationService = inject(NzNotificationService) users = signal([]); usersLoading = signal(false); + updateUser = viewChild.required('updateUser'); + modal = viewChild.required('modalIcon'); async ngOnInit() { await this.fetchUsers(); @@ -63,7 +67,52 @@ export class UserTable implements OnInit { 'Impossible de supprimer la ligne' ) } - await this.fetchUsers(); } + + selectedUser: GetUserDto | null = null; + openEditModal(user: GetUserDto) { + this.selectedUser = user; + this.modal().showModal(); + } + + async onModalOk(userId:number) { + await this.edit(userId) + this.updateUser().updateUserForm.reset(); + this.modal().isVisible = false; + await this.fetchUsers() + } + + onModalCancel() { + this.modal().isVisible = false; + } + + async edit(id :number) { + if (this.updateUser().updateUserForm.invalid) + { + this.notificationService.error( + 'Erreur', + 'Erreur d\'écriture dans le formulaire' + ) + } + try { + const rawDate = this.updateUser().updateUserForm.get('birthDate')?.value; + const birthDate = format(rawDate, 'yyyy-MM-dd'); + + const users = this.updateUser().updateUserForm.getRawValue(); + users.birthDate = birthDate; + + await firstValueFrom(this.usersService.updateUserEndpoint(id, users)) + + this.notificationService.success( + 'Success', + 'Utilisateur modifié' + ) + } catch (e) { + this.notificationService.error( + 'Erreur', + 'Erreur lors de la modification' + ) + } + } }