fixed delete community

This commit is contained in:
hannathkadher
2024-11-21 13:02:48 +04:00
parent 8a7f9ab2dc
commit 288360f1af
5 changed files with 64 additions and 28 deletions

View File

@ -19,6 +19,7 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
on<CreateCommunityEvent>(_onCreateCommunity);
on<SaveSpacesEvent>(_onSaveSpaces);
on<FetchProductsEvent>(_onFetchProducts);
on<DeleteCommunityEvent>(_onCommunityDelete);
}
void _onFetchProducts(
@ -74,11 +75,29 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
}
}
void _onCommunityDelete(
DeleteCommunityEvent event,
Emitter<SpaceManagementState> emit,
) async {
try {
emit(SpaceManagementLoading());
final success = await _api.deleteCommunity(event.communityUuid);
if (success) {
add(LoadCommunityAndSpacesEvent());
} else {
emit(const SpaceManagementError('Failed to delete the community.'));
}
} catch (e) {
// Handle unexpected errors
emit(SpaceManagementError('Error saving spaces: $e'));
}
}
void _onUpdateSpacePosition(
UpdateSpacePositionEvent event,
Emitter<SpaceManagementState> emit,
) {
// Handle space position update logic
}
void _onCreateCommunity(
@ -110,7 +129,6 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
}
}
void _onSaveSpaces(
SaveSpacesEvent event,
Emitter<SpaceManagementState> emit,
@ -147,8 +165,7 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
position: space.position,
icon: space.icon,
direction: space.incomingConnection?.direction,
products: space.selectedProducts
);
products: space.selectedProducts);
} else {
// Call create if the space does not have a UUID
final response = await _api.createSpace(
@ -159,9 +176,7 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
position: space.position,
icon: space.icon,
direction: space.incomingConnection?.direction,
products: space.selectedProducts
);
products: space.selectedProducts);
space.uuid = response?.uuid;
}
} catch (e) {

View File

@ -11,6 +11,17 @@ abstract class SpaceManagementEvent extends Equatable {
class LoadCommunityAndSpacesEvent extends SpaceManagementEvent {}
class DeleteCommunityEvent extends SpaceManagementEvent {
final String communityUuid;
const DeleteCommunityEvent({
required this.communityUuid,
});
@override
List<Object> get props => [communityUuid];
}
class CreateSpaceEvent extends SpaceManagementEvent {
final String name;
final String icon;

View File

@ -56,13 +56,15 @@ class SpaceManagementPageState extends State<SpaceManagementPage> {
);
if (selectedIndex != -1) {
selectedCommunity = state.communities[selectedIndex];
} else {
selectedCommunity = null;
selectedSpace = null;
}
return LoadedSpaceView(
communities: state.communities,
selectedCommunity: selectedCommunity,
selectedSpace: selectedSpace,
products:state.products,
products: state.products,
onCommunitySelected: (community) {
setState(() {
selectedCommunity = community;

View File

@ -119,7 +119,6 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
return SpaceContainerWidget(
index: index,
onDoubleTap: () {
print(spaces[index].toString());
_showEditSpaceDialog(spaces[index]);
},
icon: spaces[index].icon ?? '',
@ -209,12 +208,12 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
onPressed: () {
showDeleteConfirmationDialog(context, () {
// Handle the delete action here
print("Delete confirmed");
Navigator.of(context).pop(); // Close the dialog after confirming
_onDelete();
}, widget.selectedSpace != null);
},
icon: SvgPicture.asset(
Assets.acFanAuto, // Path to your SVG asset
Assets.delete, // Path to your SVG asset
width: 18, // Adjust width as needed
height: 18, // Adjust height as needed
),
@ -426,4 +425,13 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
communityUuid: communityUuid,
));
}
void _onDelete() {
if (widget.selectedCommunity != null && widget.selectedCommunity?.uuid != null) {
context.read<SpaceManagementBloc>().add(DeleteCommunityEvent(
communityUuid: widget.selectedCommunity!.uuid,
));
}
}
}

View File

@ -235,5 +235,5 @@ class Assets {
static const String garageDoor = 'assets/icons/garage_opener.svg';
static const String doorSensor = 'assets/icons/door_sensor.svg';
static const String delete = 'assets/icons/delete_svg';
static const String delete = 'assets/icons/delete.svg';
}