From 72241cba6cf2bce8040e9cc55e99a80706ac3af4 Mon Sep 17 00:00:00 2001 From: hannathkadher Date: Mon, 3 Feb 2025 14:47:05 +0400 Subject: [PATCH] added error validation --- .../assign_tag/bloc/assign_tag_bloc.dart | 14 ++++++++++---- .../assign_tag/bloc/assign_tag_state.dart | 4 ++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/pages/spaces_management/assign_tag/bloc/assign_tag_bloc.dart b/lib/pages/spaces_management/assign_tag/bloc/assign_tag_bloc.dart index 2d9222a6..3de906d3 100644 --- a/lib/pages/spaces_management/assign_tag/bloc/assign_tag_bloc.dart +++ b/lib/pages/spaces_management/assign_tag/bloc/assign_tag_bloc.dart @@ -57,6 +57,7 @@ class AssignTagBloc extends Bloc { if (currentState is AssignTagLoaded && currentState.tags.isNotEmpty) { final tags = List.from(currentState.tags); tags[event.index].tag = event.tag; + emit(AssignTagLoaded( tags: tags, isSaveEnabled: _validateTags(tags), @@ -78,6 +79,7 @@ class AssignTagBloc extends Bloc { emit(AssignTagLoaded( tags: tags, isSaveEnabled: _validateTags(tags), + errorMessage: _getValidationError(tags), )); } }); @@ -106,12 +108,13 @@ class AssignTagBloc extends Bloc { emit(AssignTagLoaded( tags: updatedTags, isSaveEnabled: _validateTags(updatedTags), + errorMessage: _getValidationError(updatedTags), )); } else { emit(const AssignTagLoaded( - tags: [], - isSaveEnabled: false, - )); + tags: [], + isSaveEnabled: false, + errorMessage: 'Failed to delete tag')); } }); } @@ -125,7 +128,10 @@ class AssignTagBloc extends Bloc { String? _getValidationError(List tags) { final hasEmptyTag = tags.any((tag) => (tag.tag?.trim() ?? '').isEmpty); - if (hasEmptyTag) return 'Tags cannot be empty.'; + if (hasEmptyTag) { + return 'Tags cannot be empty.'; + } + final duplicateTags = tags .map((tag) => tag.tag?.trim() ?? '') .fold>({}, (map, tag) { diff --git a/lib/pages/spaces_management/assign_tag/bloc/assign_tag_state.dart b/lib/pages/spaces_management/assign_tag/bloc/assign_tag_state.dart index 19cf4435..1ae0ea90 100644 --- a/lib/pages/spaces_management/assign_tag/bloc/assign_tag_state.dart +++ b/lib/pages/spaces_management/assign_tag/bloc/assign_tag_state.dart @@ -21,11 +21,11 @@ class AssignTagLoaded extends AssignTagState { const AssignTagLoaded({ required this.tags, required this.isSaveEnabled, - this.errorMessage, + required this.errorMessage, }); @override - List get props => [tags, isSaveEnabled]; + List get props => [tags, isSaveEnabled, errorMessage ?? '']; } class AssignTagError extends AssignTagState {