Add recursive insertion method in SpacesRecursiveHelper for managing space hierarchy. Update CommunityStructureCanvas to utilize this method for inserting new spaces under the correct parent, enhancing community state management during space creation.

This commit is contained in:
Faris Armoush
2025-07-16 11:47:27 +03:00
parent 6ec972a520
commit f03c28f7fd
2 changed files with 29 additions and 8 deletions

View File

@ -40,4 +40,24 @@ abstract final class SpacesRecursiveHelper {
final nonNullSpaces = updatedSpaces.whereType<SpaceModel>().toList();
return nonNullSpaces;
}
static List<SpaceModel> recusrivelyInsert(
List<SpaceModel> spaces,
SpaceModel newSpace,
String parentUuid,
) {
return spaces.map((space) {
if (space.uuid == parentUuid) {
return space.copyWith(
children: [...space.children, newSpace],
);
}
if (space.children.isNotEmpty) {
return space.copyWith(
children: recusrivelyInsert(space.children, newSpace, parentUuid),
);
}
return space;
}).toList();
}
}

View File

@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/space_management_v2/main_module/helpers/spaces_recursive_helper.dart';
import 'package:syncrow_web/pages/space_management_v2/main_module/models/space_connection_model.dart';
import 'package:syncrow_web/pages/space_management_v2/main_module/models/space_reorder_data_model.dart';
import 'package:syncrow_web/pages/space_management_v2/main_module/painters/spaces_connections_arrow_painter.dart';
@ -329,15 +330,15 @@ class _CommunityStructureCanvasState extends State<CommunityStructureCanvas>
communityUuid: widget.community.uuid,
parentUuid: space.uuid,
onSuccess: (updatedSpaceModel) {
// TODO(FarisArmoush): insert the space under the parent, which is the space that was tapped
final newCommunity = widget.community.copyWith();
final parentIndex =
newCommunity.spaces.indexWhere((s) => s.uuid == space.uuid);
if (parentIndex != -1) {
newCommunity.spaces.insert(parentIndex + 1, updatedSpaceModel);
}
final updatedSpaces = SpacesRecursiveHelper.recusrivelyInsert(
spaces,
updatedSpaceModel,
space.uuid,
);
context.read<CommunitiesBloc>().add(
CommunitiesUpdateCommunity(newCommunity),
CommunitiesUpdateCommunity(
widget.community.copyWith(spaces: updatedSpaces),
),
);
},
),