diff --git a/lib/pages/space_management_v2/main_module/helpers/spaces_recursive_helper.dart b/lib/pages/space_management_v2/main_module/helpers/spaces_recursive_helper.dart index b725bf31..4322c4e8 100644 --- a/lib/pages/space_management_v2/main_module/helpers/spaces_recursive_helper.dart +++ b/lib/pages/space_management_v2/main_module/helpers/spaces_recursive_helper.dart @@ -40,4 +40,24 @@ abstract final class SpacesRecursiveHelper { final nonNullSpaces = updatedSpaces.whereType().toList(); return nonNullSpaces; } + + static List recusrivelyInsert( + List 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(); + } } diff --git a/lib/pages/space_management_v2/main_module/widgets/community_structure_canvas.dart b/lib/pages/space_management_v2/main_module/widgets/community_structure_canvas.dart index 63f8e8ec..03efab7d 100644 --- a/lib/pages/space_management_v2/main_module/widgets/community_structure_canvas.dart +++ b/lib/pages/space_management_v2/main_module/widgets/community_structure_canvas.dart @@ -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 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().add( - CommunitiesUpdateCommunity(newCommunity), + CommunitiesUpdateCommunity( + widget.community.copyWith(spaces: updatedSpaces), + ), ); }, ),