From 6de87949917cbbb2c1629f098482f93ef48bf488 Mon Sep 17 00:00:00 2001 From: Ashwin Date: Wed, 18 Feb 2026 21:11:33 +0530 Subject: [PATCH] feat: Enable task detail navigation from dashboard, remove recent activity section, and optimize task update/patch logic. --- mobile/lib/data/models/dashboard_data.dart | 94 ++++-- mobile/lib/providers/dashboard_provider.dart | 17 +- mobile/lib/providers/tasks_provider.dart | 32 +- .../screens/dashboard/dashboard_screen.dart | 279 +++++------------- mobile/pubspec.yaml | 2 +- 5 files changed, 181 insertions(+), 243 deletions(-) diff --git a/mobile/lib/data/models/dashboard_data.dart b/mobile/lib/data/models/dashboard_data.dart index ea35c48d..12d86838 100644 --- a/mobile/lib/data/models/dashboard_data.dart +++ b/mobile/lib/data/models/dashboard_data.dart @@ -1,4 +1,5 @@ // Dashboard data models for BottleCRM +import 'package:flutter/foundation.dart'; /// Urgent counts from dashboard API class UrgentCounts { @@ -102,7 +103,7 @@ class HotLead { factory HotLead.fromJson(Map json) { return HotLead( - id: json['id'] as String, + id: json['id']?.toString() ?? '', firstName: json['first_name'] as String? ?? '', lastName: json['last_name'] as String? ?? '', company: json['company'] as String?, @@ -145,7 +146,7 @@ class DashboardTask { factory DashboardTask.fromJson(Map json) { return DashboardTask( - id: json['id'] as String, + id: json['id']?.toString() ?? '', title: json['title'] as String? ?? 'Untitled', description: json['description'] as String?, status: json['status'] as String? ?? 'New', @@ -234,7 +235,11 @@ class DashboardData { }); factory DashboardData.fromJson(Map json) { + debugPrint('DashboardData.fromJson: Starting to parse...'); + debugPrint('DashboardData.fromJson: Keys: ${json.keys.toList()}'); + // Parse pipeline by stage + debugPrint('DashboardData.fromJson: Parsing pipeline_by_stage...'); final pipelineMap = json['pipeline_by_stage'] as Map? ?? {}; final pipelineStages = pipelineMap.entries .map((e) => PipelineStage.fromJson(e.key, e.value as Map)) @@ -247,31 +252,80 @@ class DashboardData { final bIndex = stageOrder.indexOf(b.code); return aIndex.compareTo(bIndex); }); + debugPrint('DashboardData.fromJson: Pipeline stages parsed: ${pipelineStages.length}'); + + // Parse urgent counts + debugPrint('DashboardData.fromJson: Parsing urgent_counts...'); + final urgentCounts = json['urgent_counts'] != null + ? UrgentCounts.fromJson(json['urgent_counts'] as Map) + : const UrgentCounts(); + debugPrint('DashboardData.fromJson: Urgent counts parsed'); + + // Parse revenue metrics + debugPrint('DashboardData.fromJson: Parsing revenue_metrics...'); + final revenueMetrics = json['revenue_metrics'] != null + ? RevenueMetrics.fromJson(json['revenue_metrics'] as Map) + : const RevenueMetrics(); + debugPrint('DashboardData.fromJson: Revenue metrics parsed'); + + // Parse hot leads + debugPrint('DashboardData.fromJson: Parsing hot_leads...'); + final hotLeadsList = []; + if (json['hot_leads'] != null) { + for (final item in json['hot_leads'] as List) { + try { + hotLeadsList.add(HotLead.fromJson(item as Map)); + } catch (e) { + debugPrint('DashboardData.fromJson: Error parsing hot lead: $e'); + debugPrint('DashboardData.fromJson: Hot lead data: $item'); + } + } + } + debugPrint('DashboardData.fromJson: Hot leads parsed: ${hotLeadsList.length}'); + + // Parse tasks + debugPrint('DashboardData.fromJson: Parsing tasks...'); + final tasksList = []; + if (json['tasks'] != null) { + for (final item in json['tasks'] as List) { + try { + tasksList.add(DashboardTask.fromJson(item as Map)); + } catch (e) { + debugPrint('DashboardData.fromJson: Error parsing task: $e'); + debugPrint('DashboardData.fromJson: Task data: $item'); + } + } + } + debugPrint('DashboardData.fromJson: Tasks parsed: ${tasksList.length}'); + + // Parse activities + debugPrint('DashboardData.fromJson: Parsing activities...'); + final activitiesList = []; + if (json['activities'] != null) { + for (final item in json['activities'] as List) { + try { + activitiesList.add(DashboardActivity.fromJson(item as Map)); + } catch (e) { + debugPrint('DashboardData.fromJson: Error parsing activity: $e'); + debugPrint('DashboardData.fromJson: Activity data: $item'); + } + } + } + debugPrint('DashboardData.fromJson: Activities parsed: ${activitiesList.length}'); + + debugPrint('DashboardData.fromJson: All parsing complete!'); return DashboardData( accountsCount: json['accounts_count'] as int? ?? 0, contactsCount: json['contacts_count'] as int? ?? 0, leadsCount: json['leads_count'] as int? ?? 0, opportunitiesCount: json['opportunities_count'] as int? ?? 0, - urgentCounts: json['urgent_counts'] != null - ? UrgentCounts.fromJson(json['urgent_counts'] as Map) - : const UrgentCounts(), + urgentCounts: urgentCounts, pipelineByStage: pipelineStages, - revenueMetrics: json['revenue_metrics'] != null - ? RevenueMetrics.fromJson(json['revenue_metrics'] as Map) - : const RevenueMetrics(), - hotLeads: (json['hot_leads'] as List?) - ?.map((e) => HotLead.fromJson(e as Map)) - .toList() ?? - [], - tasks: (json['tasks'] as List?) - ?.map((e) => DashboardTask.fromJson(e as Map)) - .toList() ?? - [], - activities: (json['activities'] as List?) - ?.map((e) => DashboardActivity.fromJson(e as Map)) - .toList() ?? - [], + revenueMetrics: revenueMetrics, + hotLeads: hotLeadsList, + tasks: tasksList, + activities: activitiesList, ); } } diff --git a/mobile/lib/providers/dashboard_provider.dart b/mobile/lib/providers/dashboard_provider.dart index 7f22d510..9be699b1 100644 --- a/mobile/lib/providers/dashboard_provider.dart +++ b/mobile/lib/providers/dashboard_provider.dart @@ -66,7 +66,20 @@ class DashboardNotifier extends StateNotifier { try { final response = await _apiService.get(ApiConfig.dashboard); + debugPrint('DashboardNotifier: Response status: ${response.statusCode}'); + debugPrint('DashboardNotifier: Response success: ${response.success}'); + if (response.success && response.data != null) { + debugPrint('DashboardNotifier: Response keys: ${response.data!.keys.toList()}'); + debugPrint('DashboardNotifier: tasks data type: ${response.data!['tasks']?.runtimeType}'); + if (response.data!['tasks'] != null) { + final tasksList = response.data!['tasks'] as List; + debugPrint('DashboardNotifier: tasks count: ${tasksList.length}'); + if (tasksList.isNotEmpty) { + debugPrint('DashboardNotifier: first task: ${tasksList.first}'); + } + } + final dashboardData = DashboardData.fromJson(response.data!); state = state.copyWith( data: dashboardData, @@ -84,9 +97,11 @@ class DashboardNotifier extends StateNotifier { error: response.message ?? 'Failed to load dashboard data', ); debugPrint('DashboardNotifier: API error - ${response.message}'); + debugPrint('DashboardNotifier: Response data: ${response.data}'); } - } catch (e) { + } catch (e, stackTrace) { debugPrint('DashboardNotifier: Exception - $e'); + debugPrint('DashboardNotifier: Stack trace - $stackTrace'); state = state.copyWith( isLoading: false, error: 'Failed to load dashboard: ${e.toString()}', diff --git a/mobile/lib/providers/tasks_provider.dart b/mobile/lib/providers/tasks_provider.dart index fdaeca12..1222b175 100644 --- a/mobile/lib/providers/tasks_provider.dart +++ b/mobile/lib/providers/tasks_provider.dart @@ -1,6 +1,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../config/api_config.dart'; +import '../data/models/lead.dart' show Priority; import '../data/models/task.dart'; import '../services/api_service.dart'; @@ -236,11 +237,9 @@ class TasksNotifier extends StateNotifier { final response = await _apiService.put(url, data); if (response.success) { - // Update the task in local state - final updatedTask = Task.fromJson(response.data!); - state = state.copyWith( - tasks: state.tasks.map((t) => t.id == taskId ? updatedTask : t).toList(), - ); + debugPrint('TasksNotifier: Update successful, refreshing task list'); + // Refresh the list to get updated data from server + await fetchTasks(refresh: true); } return response; @@ -296,11 +295,26 @@ class TasksNotifier extends StateNotifier { final response = await _apiService.patch(url, data); - if (response.success && response.data != null) { - // Update the task in local state - final updatedTask = Task.fromJson(response.data!); + if (response.success) { + debugPrint('TasksNotifier: Patch successful, updating local state optimistically'); + // Update local state optimistically based on the patch data state = state.copyWith( - tasks: state.tasks.map((t) => t.id == taskId ? updatedTask : t).toList(), + tasks: state.tasks.map((t) { + if (t.id == taskId) { + // Apply patch data to existing task + return t.copyWith( + status: data.containsKey('status') + ? TaskStatus.fromString(data['status'] as String?) + : null, + priority: data.containsKey('priority') + ? Priority.fromString(data['priority'] as String?) + : null, + title: data['title'] as String?, + description: data['description'] as String?, + ); + } + return t; + }).toList(), ); } diff --git a/mobile/lib/screens/dashboard/dashboard_screen.dart b/mobile/lib/screens/dashboard/dashboard_screen.dart index b63c9497..0295c2c3 100644 --- a/mobile/lib/screens/dashboard/dashboard_screen.dart +++ b/mobile/lib/screens/dashboard/dashboard_screen.dart @@ -147,13 +147,6 @@ class _DashboardScreenState extends ConsumerState { // Today's Tasks _buildTasksSection(data.tasks), - const SizedBox(height: 16), - - // Recent Activity - if (data.activities.isNotEmpty) ...[ - _buildActivitySection(data.activities), - ], - const SizedBox(height: 80), ], ); @@ -537,6 +530,7 @@ class _DashboardScreenState extends ConsumerState { return _TaskItem( task: task, showDivider: index < upcomingTasks.length - 1, + onTap: () => context.push('/tasks/${task.id}'), ); }).toList(), ), @@ -546,39 +540,6 @@ class _DashboardScreenState extends ConsumerState { ); } - Widget _buildActivitySection(List activities) { - return Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Padding( - padding: const EdgeInsets.symmetric(horizontal: 12), - child: Text('Recent Activity', style: AppTypography.labelSmall), - ), - const SizedBox(height: 8), - Padding( - padding: const EdgeInsets.symmetric(horizontal: 12), - child: Container( - decoration: BoxDecoration( - color: AppColors.surface, - borderRadius: AppLayout.borderRadiusMd, - border: Border.all(color: AppColors.border), - ), - child: Column( - children: activities.take(5).toList().asMap().entries.map((entry) { - final index = entry.key; - final activity = entry.value; - return _ActivityItem( - activity: activity, - showDivider: index < activities.length - 1 && index < 4, - ); - }).toList(), - ), - ), - ), - ], - ); - } - Widget _buildExpandableFAB(BuildContext context) { return FloatingActionButton( onPressed: () { @@ -780,10 +741,12 @@ class _UrgentBadge extends StatelessWidget { class _TaskItem extends StatelessWidget { final DashboardTask task; final bool showDivider; + final VoidCallback? onTap; const _TaskItem({ required this.task, this.showDivider = true, + this.onTap, }); Color _getPriorityColor() { @@ -804,75 +767,78 @@ class _TaskItem extends StatelessWidget { Widget build(BuildContext context) { return Column( children: [ - Padding( - padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 8), - child: Row( - children: [ - Container( - width: 3, - height: 32, - decoration: BoxDecoration( - color: _getPriorityColor(), - borderRadius: BorderRadius.circular(2), + InkWell( + onTap: onTap, + child: Padding( + padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 8), + child: Row( + children: [ + Container( + width: 3, + height: 32, + decoration: BoxDecoration( + color: _getPriorityColor(), + borderRadius: BorderRadius.circular(2), + ), ), - ), - const SizedBox(width: 10), - Expanded( - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text( - task.title, - style: AppTypography.body.copyWith( - fontWeight: FontWeight.w500, + const SizedBox(width: 10), + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + task.title, + style: AppTypography.body.copyWith( + fontWeight: FontWeight.w500, + ), + maxLines: 1, + overflow: TextOverflow.ellipsis, ), - maxLines: 1, - overflow: TextOverflow.ellipsis, - ), - if (task.relatedTo != null || task.dueDate != null) - Row( - children: [ - if (task.relatedTo != null) - Flexible( - child: Text( - task.relatedTo!, + if (task.relatedTo != null || task.dueDate != null) + Row( + children: [ + if (task.relatedTo != null) + Flexible( + child: Text( + task.relatedTo!, + style: AppTypography.caption.copyWith( + color: AppColors.textTertiary, + ), + maxLines: 1, + overflow: TextOverflow.ellipsis, + ), + ), + if (task.relatedTo != null && task.dueDate != null) + Text(' • ', style: TextStyle(color: AppColors.gray300, fontSize: 12)), + if (task.dueDate != null) + Text( + DateFormat.MMMd().format(task.dueDate!), style: AppTypography.caption.copyWith( color: AppColors.textTertiary, ), - maxLines: 1, - overflow: TextOverflow.ellipsis, ), - ), - if (task.relatedTo != null && task.dueDate != null) - Text(' • ', style: TextStyle(color: AppColors.gray300, fontSize: 12)), - if (task.dueDate != null) - Text( - DateFormat.MMMd().format(task.dueDate!), - style: AppTypography.caption.copyWith( - color: AppColors.textTertiary, - ), - ), - ], - ), - ], - ), - ), - Container( - padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2), - decoration: BoxDecoration( - color: _getPriorityColor().withValues(alpha: 0.1), - borderRadius: BorderRadius.circular(4), - ), - child: Text( - task.priority, - style: TextStyle( - fontSize: 11, - fontWeight: FontWeight.w600, - color: _getPriorityColor(), + ], + ), + ], ), ), - ), - ], + Container( + padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2), + decoration: BoxDecoration( + color: _getPriorityColor().withValues(alpha: 0.1), + borderRadius: BorderRadius.circular(4), + ), + child: Text( + task.priority, + style: TextStyle( + fontSize: 11, + fontWeight: FontWeight.w600, + color: _getPriorityColor(), + ), + ), + ), + ], + ), ), ), if (showDivider) const Divider(height: 1, indent: 24), @@ -881,114 +847,3 @@ class _TaskItem extends StatelessWidget { } } -// Activity Item -class _ActivityItem extends StatelessWidget { - final DashboardActivity activity; - final bool showDivider; - - const _ActivityItem({ - required this.activity, - this.showDivider = true, - }); - - IconData _getActivityIcon() { - switch (activity.activityType.toLowerCase()) { - case 'create': - return LucideIcons.plus; - case 'update': - return LucideIcons.edit; - case 'delete': - return LucideIcons.trash; - case 'comment': - return LucideIcons.messageSquare; - case 'email': - return LucideIcons.mail; - case 'call': - return LucideIcons.phone; - default: - return LucideIcons.activity; - } - } - - Color _getActivityColor() { - switch (activity.activityType.toLowerCase()) { - case 'create': - return AppColors.success500; - case 'update': - return AppColors.primary500; - case 'delete': - return AppColors.danger500; - case 'comment': - return AppColors.warning500; - default: - return AppColors.gray500; - } - } - - @override - Widget build(BuildContext context) { - return Column( - children: [ - Padding( - padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 8), - child: Row( - children: [ - Container( - width: 28, - height: 28, - decoration: BoxDecoration( - color: _getActivityColor().withValues(alpha: 0.1), - shape: BoxShape.circle, - ), - child: Icon( - _getActivityIcon(), - size: 14, - color: _getActivityColor(), - ), - ), - const SizedBox(width: 10), - Expanded( - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text( - activity.description, - style: AppTypography.body, - maxLines: 2, - overflow: TextOverflow.ellipsis, - ), - Row( - children: [ - if (activity.userName != null) ...[ - Flexible( - child: Text( - activity.userName!.split('@').first, - style: AppTypography.caption.copyWith( - fontWeight: FontWeight.w500, - color: AppColors.textSecondary, - ), - maxLines: 1, - overflow: TextOverflow.ellipsis, - ), - ), - Text(' • ', style: TextStyle(color: AppColors.gray300, fontSize: 12)), - ], - Text( - activity.relativeTime, - style: AppTypography.caption.copyWith( - color: AppColors.textTertiary, - ), - ), - ], - ), - ], - ), - ), - ], - ), - ), - if (showDivider) const Divider(height: 1, indent: 48), - ], - ); - } -} diff --git a/mobile/pubspec.yaml b/mobile/pubspec.yaml index 90702542..6e575070 100644 --- a/mobile/pubspec.yaml +++ b/mobile/pubspec.yaml @@ -4,7 +4,7 @@ description: "CRM for everyone." # pub.dev using `flutter pub publish`. This is preferred for private packages. publish_to: "none" # Remove this line if you wish to publish to pub.dev -version: 1.0.0+9 +version: 1.1.0+10 environment: sdk: ^3.8.1