feat(#14): redirection première connexion config
- Redirection vers /login après première config réussie - Gestion défensive des réponses API (200/201, bool/string) - Force l'onglet Paramètres si setup non terminé Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
31857ec891
commit
6752dc97b4
@ -6,7 +6,7 @@ class Env {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Construit une URL vers l'API v1 à partir d'un chemin (commençant par '/')
|
// Construit une URL vers l'API v1 à partir d'un chemin (commençant par '/')
|
||||||
static String apiV1(String path) => "${apiBaseUrl}/api/v1$path";
|
static String apiV1(String path) => '$apiBaseUrl/api/v1$path';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -15,13 +15,8 @@ class AdminDashboardScreen extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _AdminDashboardScreenState extends State<AdminDashboardScreen> {
|
class _AdminDashboardScreenState extends State<AdminDashboardScreen> {
|
||||||
/// null = chargement du statut setup, true/false = connu
|
|
||||||
bool? _setupCompleted;
|
bool? _setupCompleted;
|
||||||
|
|
||||||
/// 0 = Gestion des utilisateurs, 1 = Paramètres
|
|
||||||
int mainTabIndex = 0;
|
int mainTabIndex = 0;
|
||||||
|
|
||||||
/// Sous-onglet quand mainTabIndex == 0 : 0=Gestionnaires, 1=Parents, 2=AM, 3=Administrateurs
|
|
||||||
int subIndex = 0;
|
int subIndex = 0;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -38,8 +33,11 @@ class _AdminDashboardScreenState extends State<AdminDashboardScreen> {
|
|||||||
_setupCompleted = completed;
|
_setupCompleted = completed;
|
||||||
if (!completed) mainTabIndex = 1;
|
if (!completed) mainTabIndex = 1;
|
||||||
});
|
});
|
||||||
} catch (_) {
|
} catch (e) {
|
||||||
if (mounted) setState(() => _setupCompleted = true);
|
if (mounted) setState(() {
|
||||||
|
_setupCompleted = false;
|
||||||
|
mainTabIndex = 1;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,9 +94,7 @@ class _AdminDashboardScreenState extends State<AdminDashboardScreen> {
|
|||||||
|
|
||||||
Widget _getBody() {
|
Widget _getBody() {
|
||||||
if (mainTabIndex == 1) {
|
if (mainTabIndex == 1) {
|
||||||
return ParametresPanel(
|
return ParametresPanel(redirectToLoginAfterSave: !_setupCompleted!);
|
||||||
onSetupCompleted: () => setState(() => _setupCompleted = true),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
switch (subIndex) {
|
switch (subIndex) {
|
||||||
case 0:
|
case 0:
|
||||||
|
|||||||
@ -55,6 +55,12 @@ class ConfigurationService {
|
|||||||
: Map<String, String>.from(ApiConfig.headers);
|
: Map<String, String>.from(ApiConfig.headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static String? _toStr(dynamic v) {
|
||||||
|
if (v == null) return null;
|
||||||
|
if (v is String) return v;
|
||||||
|
return v.toString();
|
||||||
|
}
|
||||||
|
|
||||||
/// GET /api/v1/configuration/setup/status
|
/// GET /api/v1/configuration/setup/status
|
||||||
static Future<bool> getSetupStatus() async {
|
static Future<bool> getSetupStatus() async {
|
||||||
final response = await http.get(
|
final response = await http.get(
|
||||||
@ -63,7 +69,11 @@ class ConfigurationService {
|
|||||||
);
|
);
|
||||||
if (response.statusCode != 200) return true;
|
if (response.statusCode != 200) return true;
|
||||||
final data = jsonDecode(response.body);
|
final data = jsonDecode(response.body);
|
||||||
return data['data']?['setupCompleted'] as bool? ?? true;
|
final val = data['data']?['setupCompleted'];
|
||||||
|
if (val is bool) return val;
|
||||||
|
if (val is String) return val.toLowerCase() == 'true' || val == '1';
|
||||||
|
if (val is int) return val == 1;
|
||||||
|
return true; // Par défaut on considère configuré pour ne pas bloquer
|
||||||
}
|
}
|
||||||
|
|
||||||
/// GET /api/v1/configuration (toutes les configs)
|
/// GET /api/v1/configuration (toutes les configs)
|
||||||
@ -73,9 +83,8 @@ class ConfigurationService {
|
|||||||
headers: await _headers(),
|
headers: await _headers(),
|
||||||
);
|
);
|
||||||
if (response.statusCode != 200) {
|
if (response.statusCode != 200) {
|
||||||
throw Exception(
|
final err = jsonDecode(response.body) as Map<String, dynamic>?;
|
||||||
(jsonDecode(response.body) as Map)['message'] ?? 'Erreur chargement configuration',
|
throw Exception(_toStr(err?['message']) ?? 'Erreur chargement configuration');
|
||||||
);
|
|
||||||
}
|
}
|
||||||
final data = jsonDecode(response.body);
|
final data = jsonDecode(response.body);
|
||||||
final list = data['data'] as List<dynamic>? ?? [];
|
final list = data['data'] as List<dynamic>? ?? [];
|
||||||
@ -89,9 +98,8 @@ class ConfigurationService {
|
|||||||
headers: await _headers(),
|
headers: await _headers(),
|
||||||
);
|
);
|
||||||
if (response.statusCode != 200) {
|
if (response.statusCode != 200) {
|
||||||
throw Exception(
|
final err = jsonDecode(response.body) as Map<String, dynamic>?;
|
||||||
(jsonDecode(response.body) as Map)['message'] ?? 'Erreur chargement configuration',
|
throw Exception(_toStr(err?['message']) ?? 'Erreur chargement configuration');
|
||||||
);
|
|
||||||
}
|
}
|
||||||
final data = jsonDecode(response.body);
|
final data = jsonDecode(response.body);
|
||||||
final map = data['data'] as Map<String, dynamic>? ?? {};
|
final map = data['data'] as Map<String, dynamic>? ?? {};
|
||||||
@ -105,9 +113,9 @@ class ConfigurationService {
|
|||||||
headers: await _headers(),
|
headers: await _headers(),
|
||||||
body: jsonEncode(body),
|
body: jsonEncode(body),
|
||||||
);
|
);
|
||||||
if (response.statusCode != 200) {
|
if (response.statusCode != 200 && response.statusCode != 201) {
|
||||||
final err = jsonDecode(response.body) as Map<String, dynamic>?;
|
final err = jsonDecode(response.body) as Map<String, dynamic>?;
|
||||||
final msg = err != null ? (err['message'] as String? ?? err['error'] as String?) : null;
|
final msg = err != null ? (_toStr(err['error']) ?? _toStr(err['message'])) : null;
|
||||||
throw Exception(msg ?? 'Erreur lors de la sauvegarde');
|
throw Exception(msg ?? 'Erreur lors de la sauvegarde');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -120,10 +128,10 @@ class ConfigurationService {
|
|||||||
body: jsonEncode({'testEmail': testEmail}),
|
body: jsonEncode({'testEmail': testEmail}),
|
||||||
);
|
);
|
||||||
final data = jsonDecode(response.body) as Map<String, dynamic>?;
|
final data = jsonDecode(response.body) as Map<String, dynamic>?;
|
||||||
if (response.statusCode == 200 && (data?['success'] == true)) {
|
if ((response.statusCode == 200 || response.statusCode == 201) && (data?['success'] == true)) {
|
||||||
return data!['message'] as String? ?? 'Test SMTP réussi.';
|
return _toStr(data?['message']) ?? 'Test SMTP réussi.';
|
||||||
}
|
}
|
||||||
final msg = data != null ? (data['error'] as String? ?? data['message'] as String?) : null;
|
final msg = data != null ? (_toStr(data['error']) ?? _toStr(data['message'])) : null;
|
||||||
throw Exception(msg ?? 'Échec du test SMTP');
|
throw Exception(msg ?? 'Échec du test SMTP');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,9 +141,9 @@ class ConfigurationService {
|
|||||||
Uri.parse('${ApiConfig.baseUrl}${ApiConfig.configurationSetupComplete}'),
|
Uri.parse('${ApiConfig.baseUrl}${ApiConfig.configurationSetupComplete}'),
|
||||||
headers: await _headers(),
|
headers: await _headers(),
|
||||||
);
|
);
|
||||||
if (response.statusCode != 200) {
|
if (response.statusCode != 200 && response.statusCode != 201) {
|
||||||
final err = jsonDecode(response.body) as Map<String, dynamic>?;
|
final err = jsonDecode(response.body) as Map<String, dynamic>?;
|
||||||
final msg = err != null ? (err['message'] as String? ?? err['error'] as String?) : null;
|
final msg = err != null ? (_toStr(err['error']) ?? _toStr(err['message'])) : null;
|
||||||
throw Exception(msg ?? 'Erreur finalisation configuration');
|
throw Exception(msg ?? 'Erreur finalisation configuration');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,10 +1,11 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:go_router/go_router.dart';
|
||||||
|
import 'package:p_tits_pas/services/auth_service.dart';
|
||||||
|
|
||||||
/// Barre principale du dashboard admin : 2 onglets (Gestion des utilisateurs | Paramètres) + infos utilisateur.
|
/// Barre du dashboard admin : onglets Gestion des utilisateurs | Paramètres + déconnexion.
|
||||||
class DashboardAppBarAdmin extends StatelessWidget implements PreferredSizeWidget {
|
class DashboardAppBarAdmin extends StatelessWidget implements PreferredSizeWidget {
|
||||||
final int selectedIndex;
|
final int selectedIndex;
|
||||||
final ValueChanged<int> onTabChange;
|
final ValueChanged<int> onTabChange;
|
||||||
/// Si false, l'onglet "Gestion des utilisateurs" est grisé et inaccessible.
|
|
||||||
final bool setupCompleted;
|
final bool setupCompleted;
|
||||||
|
|
||||||
const DashboardAppBarAdmin({
|
const DashboardAppBarAdmin({
|
||||||
@ -115,9 +116,10 @@ class DashboardAppBarAdmin extends StatelessWidget implements PreferredSizeWidge
|
|||||||
child: const Text('Annuler'),
|
child: const Text('Annuler'),
|
||||||
),
|
),
|
||||||
ElevatedButton(
|
ElevatedButton(
|
||||||
onPressed: () {
|
onPressed: () async {
|
||||||
Navigator.pop(context);
|
Navigator.pop(context);
|
||||||
// TODO: Implémenter la logique de déconnexion
|
await AuthService.logout();
|
||||||
|
if (context.mounted) context.go('/login');
|
||||||
},
|
},
|
||||||
child: const Text('Déconnecter'),
|
child: const Text('Déconnecter'),
|
||||||
),
|
),
|
||||||
@ -127,7 +129,7 @@ class DashboardAppBarAdmin extends StatelessWidget implements PreferredSizeWidge
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sous-barre affichée quand "Gestion des utilisateurs" est actif : 4 onglets sans infos utilisateur.
|
/// Sous-barre : Gestionnaires | Parents | Assistantes maternelles | Administrateurs.
|
||||||
class DashboardUserManagementSubBar extends StatelessWidget {
|
class DashboardUserManagementSubBar extends StatelessWidget {
|
||||||
final int selectedSubIndex;
|
final int selectedSubIndex;
|
||||||
final ValueChanged<int> onSubTabChange;
|
final ValueChanged<int> onSubTabChange;
|
||||||
|
|||||||
@ -1,12 +1,13 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:go_router/go_router.dart';
|
||||||
import 'package:p_tits_pas/services/configuration_service.dart';
|
import 'package:p_tits_pas/services/configuration_service.dart';
|
||||||
|
|
||||||
/// Panneau Paramètres / Configuration (ticket #15) : 3 sections sur une page.
|
/// Panneau Paramètres admin : Email (SMTP), Personnalisation, Avancé.
|
||||||
class ParametresPanel extends StatefulWidget {
|
class ParametresPanel extends StatefulWidget {
|
||||||
/// Appelé après une sauvegarde réussie (pour débloquer le reste du dashboard si config initiale).
|
/// Si true, après sauvegarde on redirige vers le login (première config). Sinon on reste sur la page.
|
||||||
final VoidCallback? onSetupCompleted;
|
final bool redirectToLoginAfterSave;
|
||||||
|
|
||||||
const ParametresPanel({super.key, this.onSetupCompleted});
|
const ParametresPanel({super.key, this.redirectToLoginAfterSave = false});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<ParametresPanel> createState() => _ParametresPanelState();
|
State<ParametresPanel> createState() => _ParametresPanelState();
|
||||||
@ -107,13 +108,14 @@ class _ParametresPanelState extends State<ParametresPanel> {
|
|||||||
return payload;
|
return payload;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Enregistre en base sans marquer la config initiale comme terminée (utilisé avant test SMTP).
|
/// Sauvegarde en base sans completeSetup (utilisé avant test SMTP).
|
||||||
Future<void> _saveBulkOnly() async {
|
Future<void> _saveBulkOnly() async {
|
||||||
await ConfigurationService.updateBulk(_buildPayload());
|
await ConfigurationService.updateBulk(_buildPayload());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sauvegarde + marque la config initiale comme terminée + débloque les panneaux.
|
/// Sauvegarde la config, marque le setup comme terminé. Si première config, redirige vers le login.
|
||||||
Future<void> _save() async {
|
Future<void> _save() async {
|
||||||
|
final redirectAfter = widget.redirectToLoginAfterSave;
|
||||||
setState(() {
|
setState(() {
|
||||||
_message = null;
|
_message = null;
|
||||||
_isSaving = true;
|
_isSaving = true;
|
||||||
@ -123,11 +125,14 @@ class _ParametresPanelState extends State<ParametresPanel> {
|
|||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
await ConfigurationService.completeSetup();
|
await ConfigurationService.completeSetup();
|
||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
widget.onSetupCompleted?.call();
|
|
||||||
setState(() {
|
setState(() {
|
||||||
_isSaving = false;
|
_isSaving = false;
|
||||||
_message = 'Configuration enregistrée.';
|
_message = 'Configuration enregistrée.';
|
||||||
});
|
});
|
||||||
|
if (!mounted) return;
|
||||||
|
if (redirectAfter) {
|
||||||
|
GoRouter.of(context).go('/login');
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (mounted) {
|
if (mounted) {
|
||||||
setState(() {
|
setState(() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user