From 40b1c2620a0ff897545bb2c426d42e2fd6fc3370 Mon Sep 17 00:00:00 2001 From: sanchezvem Date: Thu, 27 Nov 2025 17:15:35 +0100 Subject: [PATCH] added edit fonction --- src/app/components/modal-nav/modal-nav.html | 4 +- src/app/components/modal-nav/modal-nav.ts | 13 +++--- src/app/components/profil-form/profil-form.ts | 17 +++++++- src/app/components/profil/profil.html | 2 +- src/app/components/user-table/user-table.html | 10 +++-- src/app/components/user-table/user-table.ts | 42 +++++++++++++++++++ src/app/pages/user/user.html | 2 +- src/app/pages/user/user.ts | 2 - 8 files changed, 77 insertions(+), 15 deletions(-) diff --git a/src/app/components/modal-nav/modal-nav.html b/src/app/components/modal-nav/modal-nav.html index 102ad5c..1401a39 100644 --- a/src/app/components/modal-nav/modal-nav.html +++ b/src/app/components/modal-nav/modal-nav.html @@ -1,5 +1,5 @@
- +
@@ -8,7 +8,7 @@ () + name = input.required() + ok = output(); + cancel = output(); isVisible = false; isOkLoading = false; @@ -23,12 +24,14 @@ export class ModalNav { handleOk(): void { this.isOkLoading = true; setTimeout(() => { - this.isVisible = false; + this.ok.emit(); this.isOkLoading = false; }, 1000); } handleCancel(): void { this.isVisible = false; + this.cancel.emit(); + } } diff --git a/src/app/components/profil-form/profil-form.ts b/src/app/components/profil-form/profil-form.ts index 7127e44..7d18b50 100644 --- a/src/app/components/profil-form/profil-form.ts +++ b/src/app/components/profil-form/profil-form.ts @@ -1,10 +1,11 @@ -import { Component } from '@angular/core'; +import {Component, effect, input} from '@angular/core'; import {FormControl, FormGroup, FormsModule, ReactiveFormsModule, Validators} from "@angular/forms"; import {NzColDirective} from "ng-zorro-antd/grid"; import {NzFlexDirective} from "ng-zorro-antd/flex"; import {NzFormControlComponent, NzFormDirective, NzFormItemComponent, NzFormLabelComponent} from "ng-zorro-antd/form"; import {NzInputDirective} from "ng-zorro-antd/input"; import {NzOptionComponent, NzSelectComponent} from "ng-zorro-antd/select"; +import {GetUserDto} from "../../services/api"; @Component({ selector: 'app-profil-form', @@ -31,4 +32,18 @@ export class ProfilForm { fonction: new FormControl(null, [Validators.required]), password: new FormControl(null, [Validators.required]) }) + + user = input(null); + constructor() { + effect(() => { + if (this.user()) { + this.profilForm.patchValue({ + name: this.user().name, + email: this.user().email, + fonction: this.user().fonction, + password: this.user().password + }); + } + }); + } } diff --git a/src/app/components/profil/profil.html b/src/app/components/profil/profil.html index 55ed75d..bf6e93f 100644 --- a/src/app/components/profil/profil.html +++ b/src/app/components/profil/profil.html @@ -1,6 +1,6 @@
- +
diff --git a/src/app/components/user-table/user-table.html b/src/app/components/user-table/user-table.html index 0b5d2c8..f973125 100644 --- a/src/app/components/user-table/user-table.html +++ b/src/app/components/user-table/user-table.html @@ -18,9 +18,7 @@ {{user.fonction}}
- - - +
@@ -31,3 +29,9 @@ } + + diff --git a/src/app/components/user-table/user-table.ts b/src/app/components/user-table/user-table.ts index 92212d3..8a5c86f 100644 --- a/src/app/components/user-table/user-table.ts +++ b/src/app/components/user-table/user-table.ts @@ -65,4 +65,46 @@ export class UserTable implements OnInit { await this.fetchUsers(); } + async edit(id: number, updateUserComponent: ProfilForm) { + if (updateUserComponent.profilForm.invalid) { + this.notificationService.error( + 'Erreur', + 'Erreur d\'écriture dans le formulaire' + ) + return; + } + + try { + const users = updateUserComponent.profilForm.getRawValue(); + await firstValueFrom(this.usersService.updateUserEndpoint(id, users)) + + this.notificationService.success( + 'Success', + 'Utilisateur modifié' + ) + } catch (e) { + this.notificationService.error( + 'Erreur', + 'Erreur lors de la modification' + ) + } + } + + selectedUser: GetUserDto | null = null; + openEditModal(user: GetUserDto) { + this.selectedUser = { ...user }; + this.modal().showModal(); + } + + async onModalOk(userId: number, updateUserComponent: ProfilForm, modal: ModalNav) { + if (!this.selectedUser) return; + + await this.edit(userId, updateUserComponent); + updateUserComponent.profilForm.reset(); + modal.isVisible = false; + await this.fetchUsers(); + } + onModalCancel(modal: ModalNav) { + modal.isVisible = false; + } } diff --git a/src/app/pages/user/user.html b/src/app/pages/user/user.html index b10e9ec..c87acd1 100644 --- a/src/app/pages/user/user.html +++ b/src/app/pages/user/user.html @@ -5,7 +5,7 @@ (ok)="onModalOk()" (cancel)="onModalCancel()"> - +
diff --git a/src/app/pages/user/user.ts b/src/app/pages/user/user.ts index b5a0722..9e09149 100644 --- a/src/app/pages/user/user.ts +++ b/src/app/pages/user/user.ts @@ -48,8 +48,6 @@ export class User { const users = this.createUser().profilForm.getRawValue(); await firstValueFrom(this.usersService.createUserEndpoint(users)) - console.log(users) - this.notificationService.success( 'Success', 'Utilisateur crée'