created user form for edit user
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
<div (click)="showModal()">
|
<div (click)="showModal()">
|
||||||
<nz-icon [nzType]="nameIcon" nzTheme="outline"></nz-icon>
|
<nz-icon [nzType]="nameIcon()" nzTheme="outline"></nz-icon>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<ng-template #modalContent>
|
<ng-template #modalContent>
|
||||||
@@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
<nz-modal
|
<nz-modal
|
||||||
[(nzVisible)]="isVisible"
|
[(nzVisible)]="isVisible"
|
||||||
[nzTitle]="name"
|
[nzTitle]="name()"
|
||||||
(nzOnCancel)="handleCancel()"
|
(nzOnCancel)="handleCancel()"
|
||||||
(nzOnOk)="handleOk()"
|
(nzOnOk)="handleOk()"
|
||||||
[nzOkLoading]="isOkLoading"
|
[nzOkLoading]="isOkLoading"
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { Component, Input } from '@angular/core';
|
import {Component, input, output} from '@angular/core';
|
||||||
import {NzModalComponent} from "ng-zorro-antd/modal";
|
import {NzModalComponent} from "ng-zorro-antd/modal";
|
||||||
import {NzIconDirective} from "ng-zorro-antd/icon";
|
import {NzIconDirective} from "ng-zorro-antd/icon";
|
||||||
|
|
||||||
@@ -12,12 +12,15 @@ import {NzIconDirective} from "ng-zorro-antd/icon";
|
|||||||
styleUrl: './modal-icon.css',
|
styleUrl: './modal-icon.css',
|
||||||
})
|
})
|
||||||
export class ModalIcon {
|
export class ModalIcon {
|
||||||
@Input() nameIcon: string = '';
|
nameIcon = input.required<string>()
|
||||||
@Input() name: string = '';
|
name = input.required<string>();
|
||||||
|
|
||||||
isVisible = false;
|
isVisible = false;
|
||||||
isOkLoading = false;
|
isOkLoading = false;
|
||||||
|
|
||||||
|
ok = output<void>();
|
||||||
|
cancel = output<void>();
|
||||||
|
|
||||||
showModal(): void {
|
showModal(): void {
|
||||||
this.isVisible = true;
|
this.isVisible = true;
|
||||||
}
|
}
|
||||||
@@ -25,12 +28,13 @@ export class ModalIcon {
|
|||||||
handleOk(): void {
|
handleOk(): void {
|
||||||
this.isOkLoading = true;
|
this.isOkLoading = true;
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
this.isVisible = false;
|
this.ok.emit();
|
||||||
this.isOkLoading = false;
|
this.isOkLoading = false;
|
||||||
}, 1000);
|
}, 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
handleCancel(): void {
|
handleCancel(): void {
|
||||||
|
this.cancel.emit();
|
||||||
this.isVisible = false;
|
this.isVisible = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
<form nz-form nzLayout="horizontal" [formGroup]="updateUserForm" (ngSubmit)="submitForm()">
|
<form nz-form nzLayout="horizontal" [formGroup]="updateUserForm">
|
||||||
<nz-form-item>
|
<nz-form-item>
|
||||||
<nz-form-label nzSpan="8" nzRequired>
|
<nz-form-label nzSpan="8" nzRequired>
|
||||||
Nom
|
Nom
|
||||||
@@ -30,12 +30,12 @@
|
|||||||
</nz-form-item>
|
</nz-form-item>
|
||||||
|
|
||||||
<nz-form-item>
|
<nz-form-item>
|
||||||
<nz-form-label nzSpan="8" nzRequired>
|
<nz-form-label nzSpan="8">
|
||||||
Anniversaire
|
Anniversaire
|
||||||
</nz-form-label>
|
</nz-form-label>
|
||||||
|
|
||||||
<nz-form-control nzSpan="40" nzErrorTip="Ce champ est requis">
|
<nz-form-control nzSpan="12">
|
||||||
<input nz-input placeholder="JJ/MM/AAAA" formControlName="birthDate">
|
<nz-date-picker formControlName="birthDate"></nz-date-picker>
|
||||||
</nz-form-control>
|
</nz-form-control>
|
||||||
</nz-form-item>
|
</nz-form-item>
|
||||||
</form>
|
</form>
|
||||||
@@ -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 {FormControl, FormGroup, FormsModule, ReactiveFormsModule, Validators} from "@angular/forms";
|
||||||
import {NzColDirective, NzRowDirective} from "ng-zorro-antd/grid";
|
import {NzColDirective, NzRowDirective} from "ng-zorro-antd/grid";
|
||||||
import {NzFormControlComponent, NzFormDirective, NzFormItemComponent, NzFormLabelComponent} from "ng-zorro-antd/form";
|
import {NzFormControlComponent, NzFormDirective, NzFormItemComponent, NzFormLabelComponent} from "ng-zorro-antd/form";
|
||||||
import {NzInputDirective} from "ng-zorro-antd/input";
|
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({
|
@Component({
|
||||||
selector: 'app-update-user',
|
selector: 'app-update-user',
|
||||||
@@ -15,7 +19,8 @@ import {NzInputDirective} from "ng-zorro-antd/input";
|
|||||||
NzFormLabelComponent,
|
NzFormLabelComponent,
|
||||||
NzInputDirective,
|
NzInputDirective,
|
||||||
NzRowDirective,
|
NzRowDirective,
|
||||||
ReactiveFormsModule
|
ReactiveFormsModule,
|
||||||
|
NzDatePickerComponent
|
||||||
],
|
],
|
||||||
templateUrl: './update-user.html',
|
templateUrl: './update-user.html',
|
||||||
styleUrl: './update-user.css',
|
styleUrl: './update-user.css',
|
||||||
@@ -28,14 +33,15 @@ export class UpdateUser {
|
|||||||
birthDate: new FormControl(null, [Validators.required]),
|
birthDate: new FormControl(null, [Validators.required]),
|
||||||
})
|
})
|
||||||
|
|
||||||
submitForm() {
|
user = input.required<GetUserDto>()
|
||||||
// Pour annuler si le formulaire est invalide
|
ngOnChanges() {
|
||||||
if (this.updateUserForm.invalid) return;
|
if (this.user) {
|
||||||
|
this.updateUserForm.patchValue({
|
||||||
// Pour obtenir la valeur du formulaire
|
name: this.user().name,
|
||||||
console.log(this.updateUserForm.getRawValue())
|
firstName: this.user().firstName,
|
||||||
|
email: this.user().email,
|
||||||
// Pour vider le formulaire
|
birthDate: new Date(this.user().birthDate)
|
||||||
this.updateUserForm.reset()
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,8 +45,13 @@
|
|||||||
<td>
|
<td>
|
||||||
<div style="display: flex; align-items: center;">
|
<div style="display: flex; align-items: center;">
|
||||||
<div class="cursor-pointer">
|
<div class="cursor-pointer">
|
||||||
<app-modal-icon nameIcon="edit" [name]="'Modifier'">
|
<app-modal-icon #modalIcon
|
||||||
<app-update-user></app-update-user>
|
nameIcon="edit"
|
||||||
|
[name]="'Modifier'"
|
||||||
|
(ok)="onModalOk(selectedUser?.id)"
|
||||||
|
(cancel)="onModalCancel()"
|
||||||
|
(click)="openEditModal(user)">
|
||||||
|
<app-update-user #updateUser [user]="selectedUser"></app-update-user>
|
||||||
</app-modal-icon>
|
</app-modal-icon>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -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 {NzTableComponent} from "ng-zorro-antd/table";
|
||||||
import {Modal} from "../modal/modal";
|
import {Modal} from "../modal/modal";
|
||||||
import {DatePipe} from "@angular/common";
|
import {DatePipe} from "@angular/common";
|
||||||
@@ -9,6 +9,8 @@ import {firstValueFrom} from "rxjs";
|
|||||||
import {NzDividerComponent} from "ng-zorro-antd/divider";
|
import {NzDividerComponent} from "ng-zorro-antd/divider";
|
||||||
import {ModalIcon} from "../modal-icon/modal-icon";
|
import {ModalIcon} from "../modal-icon/modal-icon";
|
||||||
import {NzIconDirective} from "ng-zorro-antd/icon";
|
import {NzIconDirective} from "ng-zorro-antd/icon";
|
||||||
|
import {format} from "date-fns";
|
||||||
|
import {CreateUser} from "../create-user/create-user";
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-user-table',
|
selector: 'app-user-table',
|
||||||
@@ -29,6 +31,8 @@ export class UserTable implements OnInit {
|
|||||||
private notificationService = inject(NzNotificationService)
|
private notificationService = inject(NzNotificationService)
|
||||||
users = signal<GetUserDto[]>([]);
|
users = signal<GetUserDto[]>([]);
|
||||||
usersLoading = signal<boolean>(false);
|
usersLoading = signal<boolean>(false);
|
||||||
|
updateUser = viewChild.required<UpdateUser>('updateUser');
|
||||||
|
modal = viewChild.required<ModalIcon>('modalIcon');
|
||||||
|
|
||||||
async ngOnInit() {
|
async ngOnInit() {
|
||||||
await this.fetchUsers();
|
await this.fetchUsers();
|
||||||
@@ -63,7 +67,52 @@ export class UserTable implements OnInit {
|
|||||||
'Impossible de supprimer la ligne'
|
'Impossible de supprimer la ligne'
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.fetchUsers();
|
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'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user