66 lines
1.9 KiB
TypeScript
66 lines
1.9 KiB
TypeScript
import {Component, inject, viewChild} from '@angular/core';
|
|
import {UserTable} from "../../components/user-table/user-table";
|
|
import {ModalButton} from "../../components/modal-button/modal-button";
|
|
import {ProfilForm} from "../../components/profil-form/profil-form";
|
|
import {Search} from "../../components/search/search";
|
|
import {UsersService} from "../../services/api";
|
|
import {NzNotificationService} from "ng-zorro-antd/notification";
|
|
import {firstValueFrom} from "rxjs";
|
|
|
|
@Component({
|
|
selector: 'app-user',
|
|
imports: [
|
|
UserTable,
|
|
ModalButton,
|
|
ProfilForm,
|
|
Search
|
|
],
|
|
templateUrl: './user.html',
|
|
styleUrl: './user.css',
|
|
})
|
|
export class User {
|
|
modal = viewChild.required<ModalButton>('modalButton');
|
|
createUser = viewChild.required<ProfilForm>('profilForm');
|
|
usersTable = viewChild.required<UserTable>('userTable');
|
|
private usersService = inject(UsersService);
|
|
private notificationService = inject(NzNotificationService)
|
|
|
|
async onModalOk() {
|
|
await this.addUser()
|
|
this.createUser().profilForm.reset();
|
|
this.modal().isVisible = false;
|
|
await this.usersTable().fetchUsers()
|
|
}
|
|
|
|
onModalCancel() {
|
|
this.modal().isVisible = false;
|
|
}
|
|
|
|
async addUser() {
|
|
if (this.createUser().profilForm.invalid)
|
|
{
|
|
this.notificationService.error(
|
|
'Erreur',
|
|
'Erreur d\'écriture dans le formulaire'
|
|
)
|
|
}
|
|
try {
|
|
const users = this.createUser().profilForm.getRawValue();
|
|
await firstValueFrom(this.usersService.createUserEndpoint(users))
|
|
|
|
console.log(users)
|
|
|
|
this.notificationService.success(
|
|
'Success',
|
|
'Utilisateur crée'
|
|
)
|
|
} catch (e) {
|
|
this.notificationService.error(
|
|
'Erreur',
|
|
'Erreur d\'enregistrement'
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|