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) {
widget.onItemSelected!();
}
debugPrint('${widget.title} ${widget.isSelected} tapped for selection');
},
child: Text(
_capitalizeFirstLetter(widget.title),

View File

@ -97,8 +97,7 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
void _onUpdateSpacePosition(
UpdateSpacePositionEvent event,
Emitter<SpaceManagementState> emit,
) {
}
) {}
void _onCreateCommunity(
CreateCommunityEvent event,
@ -152,10 +151,26 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
List<SpaceModel> spaces, String communityUuid) async {
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) {
try {
if (space.uuid != null && space.uuid!.isNotEmpty) {
// Call update if the space already has a UUID
final response = await _api.updateSpace(
communityId: communityUuid,
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/selected_product_model.dart';
enum SpaceStatus { newSpace, modified, unchanged }
enum SpaceStatus { newSpace, modified, unchanged, deleted }
class SpaceModel {
String? uuid;
@ -106,17 +106,4 @@ class SpaceModel {
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
Widget build(BuildContext context) {
final visibleSpaces = flattenSpaces(widget.spaces);
final visibleConnections = createConnections(widget.spaces);
Size screenSize = MediaQuery.of(context).size;
return Expanded(
child: Container(
@ -81,7 +84,7 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
Flexible(
child: Stack(
children: [
if (spaces.isNotEmpty)
if (visibleSpaces.isNotEmpty)
InteractiveViewer(
boundaryMargin: EdgeInsets.all(500),
minScale: 0.5,
@ -94,7 +97,7 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
children: [
for (var connection in connections)
CustomPaint(painter: CurvedLinePainter([connection])),
for (var entry in spaces.asMap().entries)
for (var entry in visibleSpaces.asMap().entries)
Positioned(
left: entry.value.position.dx,
top: entry.value.position.dy,
@ -103,7 +106,7 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
onButtonTap: (int index, Offset newPosition, String direction) {
_showCreateSpaceDialog(
screenSize,
position: spaces[index].position + newPosition,
position: visibleSpaces[index].position + newPosition,
parentIndex: index,
direction: direction,
);
@ -119,10 +122,10 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
return SpaceContainerWidget(
index: index,
onDoubleTap: () {
_showEditSpaceDialog(spaces[index]);
_showEditSpaceDialog(visibleSpaces[index]);
},
icon: spaces[index].icon ?? '',
name: spaces[index].name,
icon: visibleSpaces[index].icon ?? '',
name: visibleSpaces[index].name,
);
},
),
@ -131,7 +134,7 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
),
),
),
if (spaces.isEmpty)
if (visibleSpaces.isEmpty)
Center(
child: AddSpaceButton(
onTap: () {
@ -180,7 +183,7 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
],
),
// Show "Save" button only if there are spaces
if (spaces.isNotEmpty && widget.selectedCommunity != null)
if (widget.spaces.isNotEmpty && widget.selectedCommunity != null)
Row(children: [
ElevatedButton.icon(
onPressed: () {
@ -358,16 +361,15 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
List<SpaceModel> result = [];
void flatten(SpaceModel space) {
// Add the current space to the result
if (space.status == SpaceStatus.deleted) return;
result.add(space);
// Recursively flatten child spaces
for (var child in space.children) {
flatten(child);
}
}
// Process each top-level space
for (var space in spaces) {
flatten(space);
}
@ -379,7 +381,11 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
List<Connection> connections = [];
void addConnections(SpaceModel parent, String direction) {
if (parent.status == SpaceStatus.deleted) return;
for (var child in parent.children) {
if (child.status == SpaceStatus.deleted) continue;
// Create a connection object
connections.add(
Connection(
@ -395,7 +401,6 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
}
}
// Process each top-level space
for (var space in spaces) {
addConnections(space, "down");
}
@ -405,16 +410,18 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
void _saveSpaces() {
if (widget.selectedCommunity == null) {
print("No community selected for saving spaces.");
debugPrint("No community selected for saving spaces.");
return;
}
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();
if (spacesToSave.isEmpty) {
print("No new or modified spaces to save.");
debugPrint("No new or modified spaces to save.");
return;
}
@ -427,11 +434,37 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
}
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(
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 CommunityModel? selectedCommunity;
final SpaceModel? selectedSpace;
final ValueChanged<CommunityModel> onCommunitySelected;
final ValueChanged<SpaceModel> onSpaceSelected;
final ValueChanged<CommunityModel>? onCommunitySelected;
final ValueChanged<SpaceModel?>? onSpaceSelected;
final List<ProductModel>? products;
const LoadedSpaceView({

View File

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