petitspas/frontend/lib/controllers/parent_dashboard_controller.dart
Julien Martin 9cb4162165 feat: Intégration du frontend Flutter depuis YNOV
- Framework: Flutter web
- Pages: Login, inscription, dashboards
- Services: API client, authentification, gestion d'état
- Intégration avec backend NestJS
- Dockerfile pour déploiement web
2025-11-24 15:44:15 +01:00

138 lines
4.3 KiB
Dart

import 'package:flutter/material.dart';
import 'package:p_tits_pas/models/m_dashbord/assistant_model.dart';
import 'package:p_tits_pas/models/m_dashbord/child_model.dart';
import 'package:p_tits_pas/models/m_dashbord/contract_model.dart';
import 'package:p_tits_pas/models/m_dashbord/conversation_model.dart';
import 'package:p_tits_pas/models/m_dashbord/event_model.dart';
import 'package:p_tits_pas/models/m_dashbord/notification_model.dart';
import 'package:p_tits_pas/services/dashboardService.dart';
class ParentDashboardController extends ChangeNotifier {
final DashboardService _dashboardService;
ParentDashboardController(this._dashboardService);
// État des données
List<ChildModel> _children = [];
String? _selectedChildId;
AssistantModel? _selectedAssistant;
List<EventModel> _upcomingEvents = [];
List<ContractModel> _contracts = [];
List<ConversationModel> _conversations = [];
List<NotificationModel> _notifications = [];
// État de chargement
bool _isLoading = false;
String? _error;
// Getters
List<ChildModel> get children => _children;
String? get selectedChildId => _selectedChildId;
ChildModel? get selectedChild => _children.where((c) => c.id == _selectedChildId).firstOrNull;
AssistantModel? get selectedAssistant => _selectedAssistant;
List<EventModel> get upcomingEvents => _upcomingEvents;
List<ContractModel> get contracts => _contracts;
List<ConversationModel> get conversations => _conversations;
List<NotificationModel> get notifications => _notifications;
bool get isLoading => _isLoading;
String? get error => _error;
// Initialisation du dashboard
Future<void> initDashboard() async {
_isLoading = true;
_error = null;
notifyListeners();
try {
await Future.wait([
_loadChildren(),
_loadUpcomingEvents(),
_loadContracts(),
_loadConversations(),
_loadNotifications(),
]);
// Sélectionner le premier enfant par défaut
if (_children.isNotEmpty && _selectedChildId == null) {
await selectChild(_children.first.id);
}
} catch (e) {
_error = 'Erreur lors du chargement du tableau de bord: $e';
} finally {
_isLoading = false;
notifyListeners();
}
}
// Sélection d'un enfant
Future<void> selectChild(String childId) async {
_selectedChildId = childId;
notifyListeners();
// Charger les données spécifiques à cet enfant
await _loadChildSpecificData(childId);
}
// Afficher le modal d'ajout d'enfant
void showAddChildModal() {
// Logique pour ouvrir le modal d'ajout d'enfant
// Sera implémentée dans le ticket FRONT-09
}
// Méthodes privées de chargement des données
Future<void> _loadChildren() async {
_children = await _dashboardService.getChildren();
notifyListeners();
}
Future<void> _loadChildSpecificData(String childId) async {
try {
// Charger l'assistante maternelle associée à cet enfant
_selectedAssistant = await _dashboardService.getAssistantForChild(childId);
// Filtrer les événements et contrats pour cet enfant
_upcomingEvents = await _dashboardService.getEventsForChild(childId);
_contracts = await _dashboardService.getContractsForChild(childId);
notifyListeners();
} catch (e) {
_error = 'Erreur lors du chargement des données pour l\'enfant: $e';
notifyListeners();
}
}
Future<void> _loadUpcomingEvents() async {
_upcomingEvents = await _dashboardService.getUpcomingEvents();
notifyListeners();
}
Future<void> _loadContracts() async {
_contracts = await _dashboardService.getContracts();
notifyListeners();
}
Future<void> _loadConversations() async {
_conversations = await _dashboardService.getConversations();
notifyListeners();
}
Future<void> _loadNotifications() async {
_notifications = await _dashboardService.getNotifications();
notifyListeners();
}
// Méthodes d'action
Future<void> markNotificationAsRead(String notificationId) async {
try {
await _dashboardService.markNotificationAsRead(notificationId);
await _loadNotifications(); // Recharger les notifications
} catch (e) {
_error = 'Erreur lors du marquage de la notification: $e';
notifyListeners();
}
}
Future<void> refreshDashboard() async {
await initDashboard();
}
}