Added user management: improved user interface with notifications, confirmation modal for deletion, and integration of a service to load and delete users.

master
Alix JEUDI--LEMOINE 2 days ago
parent 9a0e36fa7c
commit 7acce35ec0

@ -1,24 +1,67 @@
<div class="container mx-auto px-4 py-8"> <div class="container mx-auto px-4 py-8">
<h1 class="text-3xl font-bold mb-8">Gestion des utilisateurs</h1> <h1 class="text-3xl font-bold mb-8 dark:text-white">Gestion des utilisateurs</h1>
<!-- Message de notification -->
<div *ngIf="message"
[class]="messageType === 'success' ? 'bg-green-100 border-green-400 text-green-700' : 'bg-red-100 border-red-400 text-red-700'"
class="px-4 py-3 rounded relative mb-4 border"
role="alert">
<span class="block sm:inline">{{ message }}</span>
</div>
<div class="bg-white rounded-lg shadow overflow-hidden"> <div class="bg-white rounded-lg shadow overflow-hidden">
<table class="min-w-full divide-y divide-gray-200"> <table class="min-w-full divide-y divide-gray-200">
<thead class="bg-gray-50"> <thead class="bg-gray-50">
<tr> <tr>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">ID</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Nom d'utilisateur</th> <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Nom d'utilisateur</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Statut</th> <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Statut</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Actions</th>
</tr> </tr>
</thead> </thead>
<tbody class="bg-white divide-y divide-gray-200"> <tbody class="bg-white divide-y divide-gray-200">
<tr *ngFor="let user of users"> <tr *ngFor="let user of users">
<td class="px-6 py-4 whitespace-nowrap">{{ user.uid }}</td>
<td class="px-6 py-4 whitespace-nowrap">{{ user.username }}</td> <td class="px-6 py-4 whitespace-nowrap">{{ user.username }}</td>
<td class="px-6 py-4 whitespace-nowrap"> <td class="px-6 py-4 whitespace-nowrap">
<span [class]="user.is_admin ? 'text-green-600' : 'text-gray-600'"> <span [class]="user.is_admin ? 'text-green-600' : 'text-gray-600'">
{{ user.is_admin ? 'Administrateur' : 'Utilisateur' }} {{ user.is_admin ? 'Administrateur' : 'Utilisateur' }}
</span> </span>
</td> </td>
<td class="px-6 py-4 whitespace-nowrap">
<button (click)="openDeleteModal(user)"
class="bg-red-500 hover:bg-red-700 text-white font-bold py-2 px-4 rounded disabled:opacity-50" [disabled]="user.is_admin">
Supprimer
</button>
</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>
</div> </div>
<!-- Modal de confirmation -->
<div *ngIf="showConfirmModal"
class="fixed inset-0 bg-gray-600 bg-opacity-50 overflow-y-auto h-full w-full flex items-center justify-center">
<div class="relative p-5 border w-96 shadow-lg rounded-md bg-white">
<div class="mt-3 text-center">
<h3 class="text-lg leading-6 font-medium text-gray-900">Confirmer la suppression</h3>
<div class="mt-2 px-7 py-3">
<p class="text-sm text-gray-500">
Êtes-vous sûr de vouloir supprimer l'utilisateur <span class="font-semibold">{{ userToDelete?.username }}</span> ?
Cette action est irréversible.
</p>
</div>
<div class="flex justify-center space-x-4 mt-4">
<button (click)="closeModal()"
class="px-4 py-2 bg-gray-300 text-gray-700 rounded hover:bg-gray-400">
Annuler
</button>
<button (click)="confirmDelete()"
class="px-4 py-2 bg-red-500 text-white rounded hover:bg-red-600">
Confirmer
</button>
</div>
</div>
</div>
</div>
</div> </div>

@ -1,13 +1,7 @@
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
import { HttpClient } from '@angular/common/http'; import { UsersService } from '../../services/users.service';
import { environment } from '../../../environment'; import { UserDTO } from '../../model/UserDTO';
interface User {
_id: string;
username: string;
is_admin: boolean;
}
@Component({ @Component({
selector: 'app-users', selector: 'app-users',
@ -16,22 +10,51 @@ interface User {
imports: [CommonModule] imports: [CommonModule]
}) })
export class UsersComponent implements OnInit { export class UsersComponent implements OnInit {
users: User[] = []; users: UserDTO[] = [];
message: string = '';
messageType: 'success' | 'error' = 'success';
showConfirmModal: boolean = false;
userToDelete: UserDTO | null = null;
constructor(private http: HttpClient) {} constructor(private usersService: UsersService) {}
ngOnInit(): void { ngOnInit(): void {
this.loadUsers(); this.loadUsers();
} }
private loadUsers(): void { private loadUsers(): void {
this.http.get<User[]>(`${environment.apiURL}/admin/users`).subscribe( this.usersService.loadUsers().subscribe(
users => { (users: UserDTO[]) => {
this.users = users; this.users = users;
},
error => {
console.error('Erreur lors du chargement des utilisateurs:', error);
} }
); );
} }
openDeleteModal(user: UserDTO): void {
this.userToDelete = user;
this.showConfirmModal = true;
}
closeModal(): void {
this.showConfirmModal = false;
this.userToDelete = null;
}
confirmDelete(): void {
if (this.userToDelete) {
this.usersService.deleteUser(this.userToDelete.uid).subscribe({
next: (response) => {
this.message = response.message;
this.messageType = 'success';
this.loadUsers();
this.closeModal();
},
error: (error) => {
this.message = error.detail || "Une erreur est survenue";
this.messageType = 'error';
this.closeModal();
}
});
}
}
} }

@ -0,0 +1,5 @@
export interface UserDTO {
uid: string;
username: string;
is_admin: boolean;
}

@ -0,0 +1,25 @@
import { Injectable } from '@angular/core';
import { environment } from '../../environment';
import { HttpClient } from '@angular/common/http';
import { AuthService } from './auth.service';
import { Observable } from 'rxjs';
import { UserDTO } from '../model/UserDTO';
@Injectable({
providedIn: 'root',
})
export class UsersService {
constructor(private http: HttpClient, private authService: AuthService) {}
public loadUsers(): Observable<UserDTO[]> {
return this.http.get<UserDTO[]>(`${environment.apiURL}/admin/users`, {
headers: this.authService.getAuthHeaders(),
});
}
public deleteUser(uid: string): Observable<{message: string}> {
return this.http.delete<{message: string}>(`${environment.apiURL}/admin/user/${uid}`, {
headers: this.authService.getAuthHeaders(),
});
}
}
Loading…
Cancel
Save