fixed delete

This commit is contained in:
hannathkadher
2024-11-21 16:16:14 +04:00
parent 288360f1af
commit 9be03850a5
6 changed files with 74 additions and 40 deletions

View File

@ -96,7 +96,6 @@ class CustomExpansionTileState extends State<CustomExpansionTile> {
if (widget.onItemSelected != null) { if (widget.onItemSelected != null) {
widget.onItemSelected!(); widget.onItemSelected!();
} }
debugPrint('${widget.title} ${widget.isSelected} tapped for selection');
}, },
child: Text( child: Text(
_capitalizeFirstLetter(widget.title), _capitalizeFirstLetter(widget.title),

View File

@ -97,8 +97,7 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
void _onUpdateSpacePosition( void _onUpdateSpacePosition(
UpdateSpacePositionEvent event, UpdateSpacePositionEvent event,
Emitter<SpaceManagementState> emit, Emitter<SpaceManagementState> emit,
) { ) {}
}
void _onCreateCommunity( void _onCreateCommunity(
CreateCommunityEvent event, CreateCommunityEvent event,
@ -152,10 +151,26 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
List<SpaceModel> spaces, String communityUuid) async { List<SpaceModel> spaces, String communityUuid) async {
final orderedSpaces = flattenHierarchy(spaces); final orderedSpaces = flattenHierarchy(spaces);
final parentsToDelete = orderedSpaces.where((space) =>
space.status == SpaceStatus.deleted &&
(space.parent == null || space.parent?.status != SpaceStatus.deleted));
for (var parent in parentsToDelete) {
try {
// Ensure parent.uuid is not null before calling the API
if (parent.uuid != null) {
await _api.deleteSpace(communityUuid, parent.uuid!);
}
} catch (e) {
print(
'Error deleting space ${parent.name} (UUID: ${parent.uuid}, Community UUID: $communityUuid): $e');
rethrow; // Decide whether to stop execution or continue
}
}
for (var space in orderedSpaces) { for (var space in orderedSpaces) {
try { try {
if (space.uuid != null && space.uuid!.isNotEmpty) { if (space.uuid != null && space.uuid!.isNotEmpty) {
// Call update if the space already has a UUID
final response = await _api.updateSpace( final response = await _api.updateSpace(
communityId: communityUuid, communityId: communityUuid,
spaceId: space.uuid!, spaceId: space.uuid!,

View File

@ -3,7 +3,7 @@ import 'package:syncrow_web/pages/spaces_management/model/community_model.dart';
import 'package:syncrow_web/pages/spaces_management/model/connection_model.dart'; import 'package:syncrow_web/pages/spaces_management/model/connection_model.dart';
import 'package:syncrow_web/pages/spaces_management/model/selected_product_model.dart'; import 'package:syncrow_web/pages/spaces_management/model/selected_product_model.dart';
enum SpaceStatus { newSpace, modified, unchanged } enum SpaceStatus { newSpace, modified, unchanged, deleted }
class SpaceModel { class SpaceModel {
String? uuid; String? uuid;
@ -106,17 +106,4 @@ class SpaceModel {
outgoingConnections.add(connection); outgoingConnections.add(connection);
} }
@override
String toString() {
return '''
SpaceModel(
uuid: $uuid,
name: $name,
icon: $icon,
isPrivate: $isPrivate,
position: $position,
selectedProducts: $selectedProducts
)
''';
}
} }

View File

@ -66,6 +66,9 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final visibleSpaces = flattenSpaces(widget.spaces);
final visibleConnections = createConnections(widget.spaces);
Size screenSize = MediaQuery.of(context).size; Size screenSize = MediaQuery.of(context).size;
return Expanded( return Expanded(
child: Container( child: Container(
@ -81,7 +84,7 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
Flexible( Flexible(
child: Stack( child: Stack(
children: [ children: [
if (spaces.isNotEmpty) if (visibleSpaces.isNotEmpty)
InteractiveViewer( InteractiveViewer(
boundaryMargin: EdgeInsets.all(500), boundaryMargin: EdgeInsets.all(500),
minScale: 0.5, minScale: 0.5,
@ -94,7 +97,7 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
children: [ children: [
for (var connection in connections) for (var connection in connections)
CustomPaint(painter: CurvedLinePainter([connection])), CustomPaint(painter: CurvedLinePainter([connection])),
for (var entry in spaces.asMap().entries) for (var entry in visibleSpaces.asMap().entries)
Positioned( Positioned(
left: entry.value.position.dx, left: entry.value.position.dx,
top: entry.value.position.dy, top: entry.value.position.dy,
@ -103,7 +106,7 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
onButtonTap: (int index, Offset newPosition, String direction) { onButtonTap: (int index, Offset newPosition, String direction) {
_showCreateSpaceDialog( _showCreateSpaceDialog(
screenSize, screenSize,
position: spaces[index].position + newPosition, position: visibleSpaces[index].position + newPosition,
parentIndex: index, parentIndex: index,
direction: direction, direction: direction,
); );
@ -119,10 +122,10 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
return SpaceContainerWidget( return SpaceContainerWidget(
index: index, index: index,
onDoubleTap: () { onDoubleTap: () {
_showEditSpaceDialog(spaces[index]); _showEditSpaceDialog(visibleSpaces[index]);
}, },
icon: spaces[index].icon ?? '', icon: visibleSpaces[index].icon ?? '',
name: spaces[index].name, name: visibleSpaces[index].name,
); );
}, },
), ),
@ -131,7 +134,7 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
), ),
), ),
), ),
if (spaces.isEmpty) if (visibleSpaces.isEmpty)
Center( Center(
child: AddSpaceButton( child: AddSpaceButton(
onTap: () { onTap: () {
@ -180,7 +183,7 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
], ],
), ),
// Show "Save" button only if there are spaces // Show "Save" button only if there are spaces
if (spaces.isNotEmpty && widget.selectedCommunity != null) if (widget.spaces.isNotEmpty && widget.selectedCommunity != null)
Row(children: [ Row(children: [
ElevatedButton.icon( ElevatedButton.icon(
onPressed: () { onPressed: () {
@ -358,16 +361,15 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
List<SpaceModel> result = []; List<SpaceModel> result = [];
void flatten(SpaceModel space) { void flatten(SpaceModel space) {
// Add the current space to the result if (space.status == SpaceStatus.deleted) return;
result.add(space); result.add(space);
// Recursively flatten child spaces
for (var child in space.children) { for (var child in space.children) {
flatten(child); flatten(child);
} }
} }
// Process each top-level space
for (var space in spaces) { for (var space in spaces) {
flatten(space); flatten(space);
} }
@ -379,7 +381,11 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
List<Connection> connections = []; List<Connection> connections = [];
void addConnections(SpaceModel parent, String direction) { void addConnections(SpaceModel parent, String direction) {
if (parent.status == SpaceStatus.deleted) return;
for (var child in parent.children) { for (var child in parent.children) {
if (child.status == SpaceStatus.deleted) continue;
// Create a connection object // Create a connection object
connections.add( connections.add(
Connection( Connection(
@ -395,7 +401,6 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
} }
} }
// Process each top-level space
for (var space in spaces) { for (var space in spaces) {
addConnections(space, "down"); addConnections(space, "down");
} }
@ -405,16 +410,18 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
void _saveSpaces() { void _saveSpaces() {
if (widget.selectedCommunity == null) { if (widget.selectedCommunity == null) {
print("No community selected for saving spaces."); debugPrint("No community selected for saving spaces.");
return; return;
} }
List<SpaceModel> spacesToSave = spaces.where((space) { List<SpaceModel> spacesToSave = spaces.where((space) {
return space.status == SpaceStatus.newSpace || space.status == SpaceStatus.modified; return space.status == SpaceStatus.newSpace ||
space.status == SpaceStatus.modified ||
space.status == SpaceStatus.deleted;
}).toList(); }).toList();
if (spacesToSave.isEmpty) { if (spacesToSave.isEmpty) {
print("No new or modified spaces to save."); debugPrint("No new or modified spaces to save.");
return; return;
} }
@ -427,11 +434,37 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
} }
void _onDelete() { void _onDelete() {
if (widget.selectedCommunity != null && widget.selectedCommunity?.uuid != null) { if (widget.selectedCommunity != null &&
widget.selectedCommunity?.uuid != null &&
widget.selectedSpace == null) {
context.read<SpaceManagementBloc>().add(DeleteCommunityEvent( context.read<SpaceManagementBloc>().add(DeleteCommunityEvent(
communityUuid: widget.selectedCommunity!.uuid, communityUuid: widget.selectedCommunity!.uuid,
)); ));
} }
if (widget.selectedSpace != null) {
setState(() {
for (var space in spaces) {
if (space.uuid == widget.selectedSpace?.uuid) {
space.status = SpaceStatus.deleted;
_markChildrenAsDeleted(space);
}
}
_removeConnectionsForDeletedSpaces();
});
}
}
void _markChildrenAsDeleted(SpaceModel parent) {
for (var child in parent.children) {
child.status = SpaceStatus.deleted;
_markChildrenAsDeleted(child);
}
}
void _removeConnectionsForDeletedSpaces() {
connections.removeWhere((connection) {
return connection.startSpace.status == SpaceStatus.deleted ||
connection.endSpace.status == SpaceStatus.deleted;
});
} }
} }

View File

@ -10,8 +10,8 @@ class LoadedSpaceView extends StatefulWidget {
final List<CommunityModel> communities; final List<CommunityModel> communities;
final CommunityModel? selectedCommunity; final CommunityModel? selectedCommunity;
final SpaceModel? selectedSpace; final SpaceModel? selectedSpace;
final ValueChanged<CommunityModel> onCommunitySelected; final ValueChanged<CommunityModel>? onCommunitySelected;
final ValueChanged<SpaceModel> onSpaceSelected; final ValueChanged<SpaceModel?>? onSpaceSelected;
final List<ProductModel>? products; final List<ProductModel>? products;
const LoadedSpaceView({ const LoadedSpaceView({

View File

@ -15,7 +15,7 @@ import 'package:syncrow_web/utils/style.dart';
class SidebarWidget extends StatefulWidget { class SidebarWidget extends StatefulWidget {
final Function(CommunityModel)? onCommunitySelected; final Function(CommunityModel)? onCommunitySelected;
final Function(SpaceModel)? onSpaceSelected; final Function(SpaceModel?)? onSpaceSelected;
final List<CommunityModel> communities; final List<CommunityModel> communities;
const SidebarWidget( const SidebarWidget(
@ -190,7 +190,8 @@ class _SidebarWidgetState extends State<SidebarWidget> {
}); });
if (widget.onCommunitySelected != null) { if (widget.onCommunitySelected != null) {
widget.onCommunitySelected!(community); // Pass the entire community widget.onCommunitySelected!(community);
widget.onSpaceSelected!(null); // Pass the entire community
} }
}, },
onExpansionChanged: (String title, bool expanded) { onExpansionChanged: (String title, bool expanded) {
@ -204,7 +205,6 @@ class _SidebarWidgetState extends State<SidebarWidget> {
Widget _buildSpaceTile(SpaceModel space, CommunityModel community) { Widget _buildSpaceTile(SpaceModel space, CommunityModel community) {
bool isExpandedSpace = _isSpaceOrChildSelected(space); bool isExpandedSpace = _isSpaceOrChildSelected(space);
bool isSelectedSpace = _selectedSpaceUuid == space.uuid;
// Check if space should be expanded // Check if space should be expanded
return SpaceTile( return SpaceTile(
title: space.name, title: space.name,