manager-app/lib/Screens/Users/users_screen.dart

317 lines
12 KiB
Dart

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:manager_app/Models/managerContext.dart';
import 'package:manager_app/app_context.dart';
import 'package:manager_app/constants.dart';
import 'package:provider/provider.dart';
class UsersScreen extends StatefulWidget {
const UsersScreen({Key? key}) : super(key: key);
@override
_UsersScreenState createState() => _UsersScreenState();
}
class _UsersScreenState extends State<UsersScreen> {
List<Map<String, dynamic>> _users = [];
bool _loading = true;
static const _roleNames = ['SuperAdmin', 'InstanceAdmin', 'ContentEditor', 'Viewer'];
static String _roleName(dynamic v) {
if (v is String) return v;
final i = v as int?;
if (i != null && i >= 0 && i < _roleNames.length) return _roleNames[i];
return '';
}
// Convertit une valeur de rôle (String ou int) en int pour les comparaisons
static int _roleToInt(dynamic v) {
if (v is int) return v;
if (v is String) {
final i = _roleNames.indexOf(v);
return i >= 0 ? i : 3;
}
return 3;
}
// Roles the caller is allowed to assign (can't assign higher than own role)
List<int> _allowedRoles(int callerRoleValue) =>
[0, 1, 2, 3].where((r) => r >= callerRoleValue).toList();
Future<void> _loadUsers(ManagerAppContext ctx) async {
try {
final response = await ctx.clientAPI!.userApi!.userGetWithHttpInfo();
if (response.statusCode == 200) {
final List<dynamic> json = jsonDecode(utf8.decode(response.bodyBytes));
setState(() {
_users = json.cast<Map<String, dynamic>>();
_loading = false;
});
}
} catch (e) {
setState(() => _loading = false);
}
}
Future<void> _createUser(ManagerAppContext ctx, String email,
String firstName, String lastName, String password, int roleValue) async {
final body = {
'email': email,
'firstName': firstName,
'lastName': lastName,
'password': password,
'role': roleValue,
};
await ctx.clientAPI!.apiApi!.invokeAPI(
'/api/User', 'POST', [], body, {}, {}, 'application/json');
await _loadUsers(ctx);
}
Future<void> _updateUser(ManagerAppContext ctx, String id, String firstName,
String lastName, int roleValue) async {
final body = {
'id': id,
'firstName': firstName,
'lastName': lastName,
'role': roleValue,
};
await ctx.clientAPI!.apiApi!.invokeAPI(
'/api/User', 'PUT', [], body, {}, {}, 'application/json');
await _loadUsers(ctx);
}
Future<void> _deleteUser(ManagerAppContext ctx, String id) async {
await ctx.clientAPI!.userApi!.userDeleteUser(id);
await _loadUsers(ctx);
}
void _showCreateDialog(BuildContext context, ManagerAppContext ctx) {
final callerRole = _roleToInt(ctx.role?.value);
final emailCtrl = TextEditingController();
final firstCtrl = TextEditingController();
final lastCtrl = TextEditingController();
final passCtrl = TextEditingController();
int selectedRole = callerRole; // default: same as caller
showDialog(
context: context,
builder: (_) => StatefulBuilder(builder: (ctx2, setLocal) {
return AlertDialog(
title: const Text('Créer un utilisateur'),
content: SingleChildScrollView(
child: Column(mainAxisSize: MainAxisSize.min, children: [
TextField(controller: emailCtrl, decoration: const InputDecoration(labelText: 'Email')),
TextField(controller: firstCtrl, decoration: const InputDecoration(labelText: 'Prénom')),
TextField(controller: lastCtrl, decoration: const InputDecoration(labelText: 'Nom')),
TextField(controller: passCtrl, obscureText: true, decoration: const InputDecoration(labelText: 'Mot de passe')),
const SizedBox(height: 8),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('Rôle', style: TextStyle(fontSize: 12, color: Colors.grey)),
DropdownButton<int>(
value: selectedRole,
isExpanded: true,
items: _allowedRoles(callerRole)
.map((r) => DropdownMenuItem(value: r, child: Text(_roleName(r))))
.toList(),
onChanged: (v) => setLocal(() => selectedRole = v!),
),
],
),
]),
),
actions: [
TextButton(onPressed: () => Navigator.pop(ctx2), child: const Text('Annuler')),
ElevatedButton(
onPressed: () async {
Navigator.pop(ctx2);
await _createUser(ctx, emailCtrl.text, firstCtrl.text,
lastCtrl.text, passCtrl.text, selectedRole);
},
child: const Text('Créer'),
),
],
);
}),
);
}
void _showEditDialog(BuildContext context, ManagerAppContext ctx, Map<String, dynamic> user) {
final callerRole = _roleToInt(ctx.role?.value);
final firstCtrl = TextEditingController(text: user['firstName'] as String? ?? '');
final lastCtrl = TextEditingController(text: user['lastName'] as String? ?? '');
int selectedRole = _roleToInt(user['role']);
showDialog(
context: context,
builder: (_) => StatefulBuilder(builder: (ctx2, setLocal) {
return AlertDialog(
title: const Text('Modifier l\'utilisateur'),
content: SingleChildScrollView(
child: Column(mainAxisSize: MainAxisSize.min, children: [
TextField(controller: firstCtrl, decoration: const InputDecoration(labelText: 'Prénom')),
TextField(controller: lastCtrl, decoration: const InputDecoration(labelText: 'Nom')),
const SizedBox(height: 8),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('Rôle', style: TextStyle(fontSize: 12, color: Colors.grey)),
DropdownButton<int>(
value: selectedRole,
isExpanded: true,
items: _allowedRoles(callerRole)
.map((r) => DropdownMenuItem(value: r, child: Text(_roleName(r))))
.toList(),
onChanged: (v) => setLocal(() => selectedRole = v!),
),
],
),
]),
),
actions: [
TextButton(onPressed: () => Navigator.pop(ctx2), child: const Text('Annuler')),
ElevatedButton(
onPressed: () async {
Navigator.pop(ctx2);
await _updateUser(ctx, user['id'] as String, firstCtrl.text,
lastCtrl.text, selectedRole);
},
child: const Text('Enregistrer'),
),
],
);
}),
);
}
void _confirmDelete(BuildContext context, ManagerAppContext ctx, Map<String, dynamic> user) {
showDialog(
context: context,
builder: (_) => AlertDialog(
title: const Text('Supprimer l\'utilisateur'),
content: Text('Supprimer ${user['email']} ?'),
actions: [
TextButton(onPressed: () => Navigator.pop(context), child: const Text('Annuler')),
ElevatedButton(
style: ElevatedButton.styleFrom(backgroundColor: Colors.red),
onPressed: () async {
Navigator.pop(context);
await _deleteUser(ctx, user['id'] as String);
},
child: const Text('Supprimer', style: TextStyle(color: Colors.white)),
),
],
),
);
}
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
final ctx = Provider.of<AppContext>(context, listen: false).getContext() as ManagerAppContext;
_loadUsers(ctx);
});
}
static Color _roleColor(dynamic v) {
switch (_roleToInt(v)) {
case 0: return Colors.purple;
case 1: return Colors.blue;
case 2: return Colors.teal;
default: return Colors.grey;
}
}
@override
Widget build(BuildContext context) {
final appContext = Provider.of<AppContext>(context);
final managerCtx = appContext.getContext() as ManagerAppContext;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text('Utilisateurs', style: TextStyle(fontSize: 24, fontWeight: FontWeight.w600, color: kPrimaryColor)),
ElevatedButton.icon(
onPressed: () => _showCreateDialog(context, managerCtx),
icon: const Icon(Icons.add),
label: const Text('Créer un utilisateur'),
),
],
),
const SizedBox(height: 16),
if (_loading)
const Center(child: CircularProgressIndicator())
else if (_users.isEmpty)
const Center(child: Text('Aucun utilisateur'))
else
Expanded(
child: Card(
elevation: 0,
color: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
side: BorderSide(color: Colors.grey.shade200),
),
clipBehavior: Clip.antiAlias,
child: SingleChildScrollView(
child: SizedBox(
width: double.infinity,
child: DataTable(
horizontalMargin: 16,
columnSpacing: 24,
headingRowColor: WidgetStateProperty.all(Colors.grey.shade50),
dividerThickness: 1,
columns: const [
DataColumn(label: Text('Email', style: TextStyle(fontWeight: FontWeight.w600))),
DataColumn(label: Text('Prénom', style: TextStyle(fontWeight: FontWeight.w600))),
DataColumn(label: Text('Nom', style: TextStyle(fontWeight: FontWeight.w600))),
DataColumn(label: Text('Rôle', style: TextStyle(fontWeight: FontWeight.w600))),
DataColumn(label: Text('Actions', style: TextStyle(fontWeight: FontWeight.w600))),
],
rows: _users.map((user) {
final roleColor = _roleColor(user['role']);
return DataRow(cells: [
DataCell(Text(user['email'] as String? ?? '')),
DataCell(Text(user['firstName'] as String? ?? '')),
DataCell(Text(user['lastName'] as String? ?? '')),
DataCell(Chip(
label: Text(
_roleName(user['role']),
style: TextStyle(color: roleColor, fontSize: 12),
),
backgroundColor: roleColor.withValues(alpha: 0.08),
side: BorderSide(color: roleColor.withValues(alpha: 0.3)),
padding: const EdgeInsets.symmetric(horizontal: 4),
visualDensity: VisualDensity.compact,
)),
DataCell(Row(children: [
IconButton(
icon: Icon(Icons.edit, color: kPrimaryColor, size: 20),
tooltip: 'Modifier',
onPressed: () => _showEditDialog(context, managerCtx, user),
),
IconButton(
icon: const Icon(Icons.delete_outline, color: Colors.red, size: 20),
tooltip: 'Supprimer',
onPressed: () => _confirmDelete(context, managerCtx, user),
),
])),
]);
}).toList(),
),
),
),
),
),
],
);
}
}