()
+ 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'
+ )
+ }
+ }
}
|