feat(#35): unifier la modale gestionnaire en création et édition
Branche la modale sur l'action Modifier, supprime l'action dédiée de rattachement relais, ajoute la suppression avec confirmation et sécurise le dropdown relais en édition. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,10 +1,16 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:p_tits_pas/models/relais_model.dart';
|
||||
import 'package:p_tits_pas/models/user.dart';
|
||||
import 'package:p_tits_pas/services/relais_service.dart';
|
||||
import 'package:p_tits_pas/services/user_service.dart';
|
||||
|
||||
class GestionnaireCreateDialog extends StatefulWidget {
|
||||
const GestionnaireCreateDialog({super.key});
|
||||
final AppUser? initialUser;
|
||||
|
||||
const GestionnaireCreateDialog({
|
||||
super.key,
|
||||
this.initialUser,
|
||||
});
|
||||
|
||||
@override
|
||||
State<GestionnaireCreateDialog> createState() =>
|
||||
@@ -24,10 +30,25 @@ class _GestionnaireCreateDialogState extends State<GestionnaireCreateDialog> {
|
||||
bool _isLoadingRelais = true;
|
||||
List<RelaisModel> _relais = [];
|
||||
String? _selectedRelaisId;
|
||||
bool get _isEditMode => widget.initialUser != null;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
final user = widget.initialUser;
|
||||
if (user != null) {
|
||||
_nomController.text = user.nom ?? '';
|
||||
_prenomController.text = user.prenom ?? '';
|
||||
_emailController.text = user.email;
|
||||
_telephoneController.text = user.telephone ?? '';
|
||||
// En édition, on ne préremplit jamais le mot de passe.
|
||||
_passwordController.clear();
|
||||
final initialRelaisId = user.relaisId?.trim();
|
||||
_selectedRelaisId =
|
||||
(initialRelaisId == null || initialRelaisId.isEmpty)
|
||||
? null
|
||||
: initialRelaisId;
|
||||
}
|
||||
_loadRelais();
|
||||
}
|
||||
|
||||
@@ -45,13 +66,30 @@ class _GestionnaireCreateDialogState extends State<GestionnaireCreateDialog> {
|
||||
try {
|
||||
final list = await RelaisService.getRelais();
|
||||
if (!mounted) return;
|
||||
final uniqueById = <String, RelaisModel>{};
|
||||
for (final relais in list) {
|
||||
uniqueById[relais.id] = relais;
|
||||
}
|
||||
|
||||
final filtered = uniqueById.values.where((r) => r.actif).toList();
|
||||
if (_selectedRelaisId != null &&
|
||||
!filtered.any((r) => r.id == _selectedRelaisId)) {
|
||||
final selected = uniqueById[_selectedRelaisId!];
|
||||
if (selected != null) {
|
||||
filtered.add(selected);
|
||||
} else {
|
||||
_selectedRelaisId = null;
|
||||
}
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_relais = list.where((r) => r.actif).toList();
|
||||
_relais = filtered;
|
||||
_isLoadingRelais = false;
|
||||
});
|
||||
} catch (_) {
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_selectedRelaisId = null;
|
||||
_relais = [];
|
||||
_isLoadingRelais = false;
|
||||
});
|
||||
@@ -75,6 +113,9 @@ class _GestionnaireCreateDialogState extends State<GestionnaireCreateDialog> {
|
||||
}
|
||||
|
||||
String? _validatePassword(String? value) {
|
||||
if (_isEditMode && (value == null || value.trim().isEmpty)) {
|
||||
return null;
|
||||
}
|
||||
final base = _required(value, 'Mot de passe');
|
||||
if (base != null) return base;
|
||||
if (value!.trim().length < 6) return 'Minimum 6 caractères';
|
||||
@@ -90,17 +131,37 @@ class _GestionnaireCreateDialogState extends State<GestionnaireCreateDialog> {
|
||||
});
|
||||
|
||||
try {
|
||||
await UserService.createGestionnaire(
|
||||
nom: _nomController.text.trim(),
|
||||
prenom: _prenomController.text.trim(),
|
||||
email: _emailController.text.trim(),
|
||||
password: _passwordController.text,
|
||||
telephone: _telephoneController.text.trim(),
|
||||
relaisId: _selectedRelaisId,
|
||||
);
|
||||
if (_isEditMode) {
|
||||
await UserService.updateGestionnaire(
|
||||
gestionnaireId: widget.initialUser!.id,
|
||||
nom: _nomController.text.trim(),
|
||||
prenom: _prenomController.text.trim(),
|
||||
email: _emailController.text.trim(),
|
||||
telephone: _telephoneController.text.trim(),
|
||||
relaisId: _selectedRelaisId,
|
||||
password: _passwordController.text.trim().isEmpty
|
||||
? null
|
||||
: _passwordController.text,
|
||||
);
|
||||
} else {
|
||||
await UserService.createGestionnaire(
|
||||
nom: _nomController.text.trim(),
|
||||
prenom: _prenomController.text.trim(),
|
||||
email: _emailController.text.trim(),
|
||||
password: _passwordController.text,
|
||||
telephone: _telephoneController.text.trim(),
|
||||
relaisId: _selectedRelaisId,
|
||||
);
|
||||
}
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Gestionnaire créé avec succès.')),
|
||||
SnackBar(
|
||||
content: Text(
|
||||
_isEditMode
|
||||
? 'Gestionnaire modifié avec succès.'
|
||||
: 'Gestionnaire créé avec succès.',
|
||||
),
|
||||
),
|
||||
);
|
||||
Navigator.of(context).pop(true);
|
||||
} catch (e) {
|
||||
@@ -121,131 +182,269 @@ class _GestionnaireCreateDialogState extends State<GestionnaireCreateDialog> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _delete() async {
|
||||
if (!_isEditMode || _isSubmitting) return;
|
||||
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (ctx) {
|
||||
return AlertDialog(
|
||||
title: const Text('Confirmer la suppression'),
|
||||
content: Text(
|
||||
'Supprimer ${widget.initialUser!.fullName.isEmpty ? widget.initialUser!.email : widget.initialUser!.fullName} ?',
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(ctx).pop(false),
|
||||
child: const Text('Annuler'),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () => Navigator.of(ctx).pop(true),
|
||||
style: FilledButton.styleFrom(backgroundColor: Colors.red.shade700),
|
||||
child: const Text('Supprimer'),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
if (confirmed != true) return;
|
||||
|
||||
setState(() {
|
||||
_isSubmitting = true;
|
||||
});
|
||||
try {
|
||||
await UserService.deleteUser(widget.initialUser!.id);
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Gestionnaire supprimé.')),
|
||||
);
|
||||
Navigator.of(context).pop(true);
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(e.toString().replaceFirst('Exception: ', '')),
|
||||
backgroundColor: Colors.red.shade700,
|
||||
),
|
||||
);
|
||||
setState(() {
|
||||
_isSubmitting = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: const Text('Créer un gestionnaire'),
|
||||
content: ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxWidth: 640),
|
||||
title: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
_isEditMode
|
||||
? 'Modifier un gestionnaire'
|
||||
: 'Créer un gestionnaire',
|
||||
),
|
||||
),
|
||||
if (_isEditMode)
|
||||
IconButton(
|
||||
icon: const Icon(Icons.close),
|
||||
tooltip: 'Fermer',
|
||||
onPressed: _isSubmitting
|
||||
? null
|
||||
: () => Navigator.of(context).pop(false),
|
||||
),
|
||||
],
|
||||
),
|
||||
content: SizedBox(
|
||||
width: 620,
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
TextFormField(
|
||||
controller: _nomController,
|
||||
textCapitalization: TextCapitalization.words,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Nom',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
validator: (v) => _required(v, 'Nom'),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextFormField(
|
||||
controller: _prenomController,
|
||||
textCapitalization: TextCapitalization.words,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Prénom',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
validator: (v) => _required(v, 'Prénom'),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextFormField(
|
||||
controller: _emailController,
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Email',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
validator: _validateEmail,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextFormField(
|
||||
controller: _passwordController,
|
||||
obscureText: _obscurePassword,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'Mot de passe',
|
||||
border: const OutlineInputBorder(),
|
||||
suffixIcon: IconButton(
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
_obscurePassword = !_obscurePassword;
|
||||
});
|
||||
},
|
||||
icon: Icon(
|
||||
_obscurePassword
|
||||
? Icons.visibility_off
|
||||
: Icons.visibility,
|
||||
),
|
||||
),
|
||||
),
|
||||
validator: _validatePassword,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextFormField(
|
||||
controller: _telephoneController,
|
||||
keyboardType: TextInputType.phone,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Téléphone',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
validator: (v) => _required(v, 'Téléphone'),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
DropdownButtonFormField<String?>(
|
||||
value: _selectedRelaisId,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Relais principal',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
items: [
|
||||
const DropdownMenuItem<String?>(
|
||||
value: null,
|
||||
child: Text('Aucun relais'),
|
||||
),
|
||||
..._relais.map(
|
||||
(relais) => DropdownMenuItem<String?>(
|
||||
value: relais.id,
|
||||
child: Text(relais.nom),
|
||||
),
|
||||
),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(child: _buildNomField()),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(child: _buildPrenomField()),
|
||||
],
|
||||
onChanged: _isLoadingRelais
|
||||
? null
|
||||
: (value) {
|
||||
setState(() {
|
||||
_selectedRelaisId = value;
|
||||
});
|
||||
},
|
||||
),
|
||||
if (_isLoadingRelais) ...[
|
||||
const SizedBox(height: 8),
|
||||
const LinearProgressIndicator(minHeight: 2),
|
||||
],
|
||||
const SizedBox(height: 12),
|
||||
_buildEmailField(),
|
||||
const SizedBox(height: 12),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(child: _buildPasswordField()),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(child: _buildTelephoneField()),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
_buildRelaisField(),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
OutlinedButton(
|
||||
onPressed: _isSubmitting ? null : () => Navigator.of(context).pop(false),
|
||||
child: const Text('Annuler'),
|
||||
if (_isEditMode) ...[
|
||||
OutlinedButton(
|
||||
onPressed: _isSubmitting ? null : _delete,
|
||||
style: OutlinedButton.styleFrom(foregroundColor: Colors.red.shade700),
|
||||
child: const Text('Supprimer'),
|
||||
),
|
||||
FilledButton.icon(
|
||||
onPressed: _isSubmitting ? null : _submit,
|
||||
icon: _isSubmitting
|
||||
? const SizedBox(
|
||||
width: 16,
|
||||
height: 16,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Icon(Icons.edit),
|
||||
label: Text(_isSubmitting ? 'Modification...' : 'Modifier'),
|
||||
),
|
||||
] else ...[
|
||||
OutlinedButton(
|
||||
onPressed:
|
||||
_isSubmitting ? null : () => Navigator.of(context).pop(false),
|
||||
child: const Text('Annuler'),
|
||||
),
|
||||
FilledButton.icon(
|
||||
onPressed: _isSubmitting ? null : _submit,
|
||||
icon: _isSubmitting
|
||||
? const SizedBox(
|
||||
width: 16,
|
||||
height: 16,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Icon(Icons.person_add_alt_1),
|
||||
label: Text(_isSubmitting ? 'Création...' : 'Créer'),
|
||||
),
|
||||
],
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildNomField() {
|
||||
return TextFormField(
|
||||
controller: _nomController,
|
||||
textCapitalization: TextCapitalization.words,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Nom',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
validator: (v) => _required(v, 'Nom'),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildPrenomField() {
|
||||
return TextFormField(
|
||||
controller: _prenomController,
|
||||
textCapitalization: TextCapitalization.words,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Prénom',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
validator: (v) => _required(v, 'Prénom'),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildEmailField() {
|
||||
return TextFormField(
|
||||
controller: _emailController,
|
||||
keyboardType: TextInputType.emailAddress,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Email',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
validator: _validateEmail,
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildPasswordField() {
|
||||
return TextFormField(
|
||||
controller: _passwordController,
|
||||
obscureText: _obscurePassword,
|
||||
enableSuggestions: false,
|
||||
autocorrect: false,
|
||||
autofillHints: _isEditMode
|
||||
? const <String>[]
|
||||
: const [AutofillHints.newPassword],
|
||||
decoration: InputDecoration(
|
||||
labelText: _isEditMode
|
||||
? 'Nouveau mot de passe'
|
||||
: 'Mot de passe',
|
||||
border: const OutlineInputBorder(),
|
||||
suffixIcon: IconButton(
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
_obscurePassword = !_obscurePassword;
|
||||
});
|
||||
},
|
||||
icon: Icon(
|
||||
_obscurePassword ? Icons.visibility_off : Icons.visibility,
|
||||
),
|
||||
),
|
||||
FilledButton.icon(
|
||||
onPressed: _isSubmitting ? null : _submit,
|
||||
icon: _isSubmitting
|
||||
? const SizedBox(
|
||||
width: 16,
|
||||
height: 16,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Icon(Icons.person_add_alt_1),
|
||||
label: Text(_isSubmitting ? 'Création...' : 'Créer'),
|
||||
),
|
||||
validator: _validatePassword,
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTelephoneField() {
|
||||
return TextFormField(
|
||||
controller: _telephoneController,
|
||||
keyboardType: TextInputType.phone,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Téléphone',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
validator: (v) => _required(v, 'Téléphone'),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildRelaisField() {
|
||||
final selectedValue = _selectedRelaisId != null &&
|
||||
_relais.any((relais) => relais.id == _selectedRelaisId)
|
||||
? _selectedRelaisId
|
||||
: null;
|
||||
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
DropdownButtonFormField<String?>(
|
||||
isExpanded: true,
|
||||
value: selectedValue,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Relais principal',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
items: [
|
||||
const DropdownMenuItem<String?>(
|
||||
value: null,
|
||||
child: Text('Aucun relais'),
|
||||
),
|
||||
..._relais.map(
|
||||
(relais) => DropdownMenuItem<String?>(
|
||||
value: relais.id,
|
||||
child: Text(relais.nom),
|
||||
),
|
||||
),
|
||||
],
|
||||
onChanged: _isLoadingRelais
|
||||
? null
|
||||
: (value) {
|
||||
setState(() {
|
||||
_selectedRelaisId = value;
|
||||
});
|
||||
},
|
||||
),
|
||||
if (_isLoadingRelais) ...[
|
||||
const SizedBox(height: 8),
|
||||
const LinearProgressIndicator(minHeight: 2),
|
||||
],
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user