mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 15:17:31 +00:00
updated duplication ui
This commit is contained in:
@ -6,19 +6,23 @@ import 'package:syncrow_web/pages/spaces_management/space_model/models/subspace_
|
||||
import 'package:syncrow_web/utils/constants/action_enum.dart';
|
||||
|
||||
class SubSpaceModelBloc extends Bloc<SubSpaceModelEvent, SubSpaceModelState> {
|
||||
SubSpaceModelBloc() : super(SubSpaceModelState([], [], '')) {
|
||||
SubSpaceModelBloc() : super(SubSpaceModelState([], [], '', {})) {
|
||||
// Handle AddSubSpaceModel Event
|
||||
on<AddSubSpaceModel>((event, emit) {
|
||||
// Check for duplicate names (case-insensitive)
|
||||
final existingNames =
|
||||
state.subSpaces.map((e) => e.subspaceName.toLowerCase()).toSet();
|
||||
|
||||
if (existingNames.contains(event.subSpace.subspaceName.toLowerCase())) {
|
||||
// Emit state with an error message if duplicate name exists
|
||||
final updatedDuplicates = Set<String>.from(state.duplicates)
|
||||
..add(event.subSpace.subspaceName.toLowerCase());
|
||||
final updatedSubSpaces =
|
||||
List<SubspaceTemplateModel>.from(state.subSpaces)
|
||||
..add(event.subSpace);
|
||||
emit(SubSpaceModelState(
|
||||
state.subSpaces,
|
||||
updatedSubSpaces,
|
||||
state.updatedSubSpaceModels,
|
||||
'Subspace name already exists.',
|
||||
'*Duplicated sub-space name',
|
||||
updatedDuplicates,
|
||||
));
|
||||
} else {
|
||||
// Add subspace if no duplicate exists
|
||||
@ -29,7 +33,9 @@ class SubSpaceModelBloc extends Bloc<SubSpaceModelEvent, SubSpaceModelState> {
|
||||
emit(SubSpaceModelState(
|
||||
updatedSubSpaces,
|
||||
state.updatedSubSpaceModels,
|
||||
'', // Clear error message
|
||||
'',
|
||||
state.duplicates,
|
||||
// Clear error message
|
||||
));
|
||||
}
|
||||
});
|
||||
@ -42,6 +48,16 @@ class SubSpaceModelBloc extends Bloc<SubSpaceModelEvent, SubSpaceModelState> {
|
||||
final updatedSubspaceModels = List<UpdateSubspaceTemplateModel>.from(
|
||||
state.updatedSubSpaceModels,
|
||||
);
|
||||
final nameOccurrences = <String, int>{};
|
||||
for (final subSpace in updatedSubSpaces) {
|
||||
final lowerName = subSpace.subspaceName.toLowerCase();
|
||||
nameOccurrences[lowerName] = (nameOccurrences[lowerName] ?? 0) + 1;
|
||||
}
|
||||
|
||||
final updatedDuplicates = nameOccurrences.entries
|
||||
.where((entry) => entry.value > 1)
|
||||
.map((entry) => entry.key)
|
||||
.toSet();
|
||||
|
||||
if (event.subSpace.uuid?.isNotEmpty ?? false) {
|
||||
updatedSubspaceModels.add(UpdateSubspaceTemplateModel(
|
||||
@ -53,7 +69,9 @@ class SubSpaceModelBloc extends Bloc<SubSpaceModelEvent, SubSpaceModelState> {
|
||||
emit(SubSpaceModelState(
|
||||
updatedSubSpaces,
|
||||
updatedSubspaceModels,
|
||||
'', // Clear error message
|
||||
'',
|
||||
updatedDuplicates,
|
||||
// Clear error message
|
||||
));
|
||||
});
|
||||
|
||||
@ -78,7 +96,9 @@ class SubSpaceModelBloc extends Bloc<SubSpaceModelEvent, SubSpaceModelState> {
|
||||
emit(SubSpaceModelState(
|
||||
updatedSubSpaces,
|
||||
updatedSubspaceModels,
|
||||
'', // Clear error message
|
||||
'',
|
||||
state.duplicates,
|
||||
// Clear error message
|
||||
));
|
||||
});
|
||||
}
|
||||
|
@ -5,22 +5,26 @@ class SubSpaceModelState {
|
||||
final List<SubspaceTemplateModel> subSpaces;
|
||||
final List<UpdateSubspaceTemplateModel> updatedSubSpaceModels;
|
||||
final String errorMessage;
|
||||
final Set<String> duplicates;
|
||||
|
||||
SubSpaceModelState(
|
||||
this.subSpaces,
|
||||
this.updatedSubSpaceModels,
|
||||
this.errorMessage,
|
||||
this.duplicates,
|
||||
);
|
||||
|
||||
SubSpaceModelState copyWith({
|
||||
List<SubspaceTemplateModel>? subSpaces,
|
||||
List<UpdateSubspaceTemplateModel>? updatedSubSpaceModels,
|
||||
String? errorMessage,
|
||||
Set<String>? duplicates,
|
||||
}) {
|
||||
return SubSpaceModelState(
|
||||
subSpaces ?? this.subSpaces,
|
||||
updatedSubSpaceModels ?? this.updatedSubSpaceModels,
|
||||
errorMessage ?? this.errorMessage,
|
||||
duplicates ?? this.duplicates,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ class CreateSubSpaceModelDialog extends StatelessWidget {
|
||||
return Container(
|
||||
color: ColorsManager.whiteColors,
|
||||
child: SizedBox(
|
||||
width: screenWidth * 0.35,
|
||||
width: screenWidth * 0.3,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
@ -73,18 +73,40 @@ class CreateSubSpaceModelDialog extends StatelessWidget {
|
||||
spacing: 8.0,
|
||||
runSpacing: 8.0,
|
||||
children: [
|
||||
...state.subSpaces.map(
|
||||
(subSpace) => Chip(
|
||||
...state.subSpaces.asMap().entries.map(
|
||||
(entry) {
|
||||
final index = entry.key;
|
||||
final subSpace = entry.value;
|
||||
|
||||
final lowerName =
|
||||
subSpace.subspaceName.toLowerCase();
|
||||
|
||||
final duplicateIndices = state.subSpaces
|
||||
.asMap()
|
||||
.entries
|
||||
.where((e) =>
|
||||
e.value.subspaceName.toLowerCase() ==
|
||||
lowerName)
|
||||
.map((e) => e.key)
|
||||
.toList();
|
||||
final isDuplicate =
|
||||
duplicateIndices.length > 1 &&
|
||||
duplicateIndices.indexOf(index) != 0;
|
||||
|
||||
return Chip(
|
||||
label: Text(
|
||||
subSpace.subspaceName,
|
||||
style: const TextStyle(
|
||||
color: ColorsManager.spaceColor),
|
||||
color: ColorsManager.spaceColor,
|
||||
),
|
||||
),
|
||||
backgroundColor: ColorsManager.whiteColors,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
side: const BorderSide(
|
||||
color: ColorsManager.transparentColor,
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
side: BorderSide(
|
||||
color: isDuplicate
|
||||
? ColorsManager.red
|
||||
: ColorsManager.transparentColor,
|
||||
width: 0,
|
||||
),
|
||||
),
|
||||
@ -107,7 +129,8 @@ class CreateSubSpaceModelDialog extends StatelessWidget {
|
||||
onDeleted: () => context
|
||||
.read<SubSpaceModelBloc>()
|
||||
.add(RemoveSubSpaceModel(subSpace)),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
SizedBox(
|
||||
width: 200,
|
||||
@ -135,20 +158,20 @@ class CreateSubSpaceModelDialog extends StatelessWidget {
|
||||
color: ColorsManager.blackColor),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
if (state.errorMessage.isNotEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 8.0),
|
||||
padding: const EdgeInsets.only(bottom: 16.0),
|
||||
child: Text(
|
||||
state.errorMessage,
|
||||
style: const TextStyle(
|
||||
color: ColorsManager.warningRed,
|
||||
color: ColorsManager.red,
|
||||
fontSize: 12,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Row(
|
||||
children: [
|
||||
@ -163,17 +186,25 @@ class CreateSubSpaceModelDialog extends StatelessWidget {
|
||||
const SizedBox(width: 10),
|
||||
Expanded(
|
||||
child: DefaultButton(
|
||||
onPressed: () async {
|
||||
onPressed: (state.subSpaces.isEmpty ||
|
||||
state.errorMessage.isNotEmpty)
|
||||
? null
|
||||
: () async {
|
||||
final subSpaces = context
|
||||
.read<SubSpaceModelBloc>()
|
||||
.state
|
||||
.subSpaces;
|
||||
Navigator.of(context).pop();
|
||||
if (onUpdate != null) {
|
||||
onUpdate!(subSpaces);
|
||||
}
|
||||
},
|
||||
backgroundColor: ColorsManager.secondaryColor,
|
||||
borderRadius: 10,
|
||||
foregroundColor: ColorsManager.whiteColors,
|
||||
foregroundColor: state.subSpaces.isEmpty ||
|
||||
state.errorMessage.isNotEmpty
|
||||
? ColorsManager.whiteColorsWithOpacity
|
||||
: ColorsManager.whiteColors,
|
||||
child: const Text('OK'),
|
||||
),
|
||||
),
|
||||
|
Reference in New Issue
Block a user