import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:syncrow_web/pages/spaces_management/model/community_model.dart'; import 'package:syncrow_web/pages/spaces_management/model/space_model.dart'; import 'package:syncrow_web/pages/spaces_management/bloc/space_management_event.dart'; import 'package:syncrow_web/pages/spaces_management/bloc/space_management_state.dart'; import 'package:syncrow_web/services/space_mana_api.dart'; class SpaceManagementBloc extends Bloc { final CommunitySpaceManagementApi _api; SpaceManagementBloc(this._api) : super(SpaceManagementInitial()) { on(_onLoadCommunityAndSpaces); on(_onCreateSpace); on(_onUpdateSpacePosition); on(_onCreateCommunity); } void _onLoadCommunityAndSpaces( LoadCommunityAndSpacesEvent event, Emitter emit, ) async { emit(SpaceManagementLoading()); try { // Fetch all communities List communities = await _api.fetchCommunities(); // Use Future.wait to handle async calls within map List updatedCommunities = await Future.wait( communities.map((community) async { List spaces = await _api.getSpaceHierarchy(community.uuid); return CommunityModel( uuid: community.uuid, createdAt: community.createdAt, updatedAt: community.updatedAt, name: community.name, description: community.description, spaces: spaces, // New spaces list region: community.region, ); }).toList(), ); emit(SpaceManagementLoaded(communities: updatedCommunities)); } catch (e) { emit(SpaceManagementError('Error loading communities and spaces: $e')); } } void _onCreateSpace( CreateSpaceEvent event, Emitter emit, ) { // Handle space creation logic // You can emit a new state here based on your needs emit(SpaceCreationSuccess()); } void _onUpdateSpacePosition( UpdateSpacePositionEvent event, Emitter emit, ) { // Handle space position update logic } void _onCreateCommunity( CreateCommunityEvent event, Emitter emit, ) async { final previousState = state; emit(SpaceManagementLoading()); try { CommunityModel? newCommunity = await _api.createCommunity( event.name, event.description, event.regionId, ); if (newCommunity != null) { if (previousState is SpaceManagementLoaded) { final updatedCommunities = List.from(previousState.communities) ..add(newCommunity); emit(SpaceManagementLoaded(communities: updatedCommunities)); } } else { emit(const SpaceManagementError('Error creating community')); } } catch (e) { emit(SpaceManagementError('Error creating community: $e')); } } void _onSaveSpaces( SaveSpacesEvent event, Emitter emit, ) async { final previousState = state; emit(SpaceManagementLoading()); try { // Save spaces one by one for (var space in event.spaces) { await _api.createSpace( communityId: event.communityUuid, name: space.name, parentId: space.parent?.uuid, isPrivate: space.isPrivate, position: space.position, ); } emit(SpaceCreationSuccess()); } catch (e) { emit(SpaceManagementError('Error saving spaces: $e')); // Revert back to the previous state if an error occurs if (previousState is SpaceManagementLoaded) { emit(previousState); } } } }