diff --git a/lib/pages/spaces_management/bloc/space_management_bloc.dart b/lib/pages/spaces_management/bloc/space_management_bloc.dart index fefbc849..3e3b23b5 100644 --- a/lib/pages/spaces_management/bloc/space_management_bloc.dart +++ b/lib/pages/spaces_management/bloc/space_management_bloc.dart @@ -25,6 +25,7 @@ class SpaceManagementBloc on(_onSaveSpaces); on(_onFetchProducts); on(_onSelectSpace); + on(_onNewCommunity); } void _onUpdateCommunity( @@ -83,6 +84,26 @@ class SpaceManagementBloc return await _api.getSpaceHierarchy(communityUuid); } + void _onNewCommunity( + NewCommunityEvent event, + Emitter emit, + ) { + try { + if (event.communities.isEmpty) { + emit(const SpaceManagementError('No communities provided.')); + return; + } + + emit(BlankState( + communities: event.communities, + products: _cachedProducts ?? [], + )); + print('SpaceManagementLoaded state emitted with communities.'); + } catch (error) { + emit(SpaceManagementError('Error loading communities: $error')); + } + } + void _onLoadCommunityAndSpaces( LoadCommunityAndSpacesEvent event, Emitter emit, @@ -151,14 +172,16 @@ class SpaceManagementBloc await _api.createCommunity(event.name, event.description); if (newCommunity != null) { - if (previousState is SpaceManagementLoaded) { - final updatedCommunities = - List.from(previousState.communities) - ..add(newCommunity); + if (previousState is SpaceManagementLoaded || + previousState is BlankState) { + final prevCommunities = List.from( + (previousState as dynamic).communities, + ); + final updatedCommunities = prevCommunities..add(newCommunity); emit(SpaceManagementLoaded( communities: updatedCommunities, products: _cachedProducts ?? [], - selectedCommunity: null, + selectedCommunity: newCommunity, selectedSpace: null)); } } else { @@ -200,7 +223,8 @@ class SpaceManagementBloc emit(SpaceManagementLoading()); try { - if (previousState is SpaceManagementLoaded) { + if (previousState is SpaceManagementLoaded || + previousState is BlankState) { final communities = List.from( (previousState as dynamic).communities, ); diff --git a/lib/pages/spaces_management/bloc/space_management_event.dart b/lib/pages/spaces_management/bloc/space_management_event.dart index a61096ff..7b95f262 100644 --- a/lib/pages/spaces_management/bloc/space_management_event.dart +++ b/lib/pages/spaces_management/bloc/space_management_event.dart @@ -108,6 +108,15 @@ class SelectCommunityEvent extends SpaceManagementEvent { List get props => []; } +class NewCommunityEvent extends SpaceManagementEvent { + final List communities; + + const NewCommunityEvent({required this.communities}); + + @override + List get props => [communities]; +} + class SelectSpaceEvent extends SpaceManagementEvent { final CommunityModel? selectedCommunity; final SpaceModel? selectedSpace; diff --git a/lib/pages/spaces_management/bloc/space_management_state.dart b/lib/pages/spaces_management/bloc/space_management_state.dart index 1885af41..0a1cfe4d 100644 --- a/lib/pages/spaces_management/bloc/space_management_state.dart +++ b/lib/pages/spaces_management/bloc/space_management_state.dart @@ -27,6 +27,16 @@ class SpaceManagementLoaded extends SpaceManagementState { this.selectedSpace}); } +class BlankState extends SpaceManagementState { + final List communities; + final List products; + + BlankState({ + required this.communities, + required this.products, + }); +} + class SpaceCreationSuccess extends SpaceManagementState { final List spaces; diff --git a/lib/pages/spaces_management/view/spaces_management_page.dart b/lib/pages/spaces_management/view/spaces_management_page.dart index a540cf3c..8b37b876 100644 --- a/lib/pages/spaces_management/view/spaces_management_page.dart +++ b/lib/pages/spaces_management/view/spaces_management_page.dart @@ -45,6 +45,13 @@ class SpaceManagementPageState extends State { builder: (context, state) { if (state is SpaceManagementLoading) { return const Center(child: CircularProgressIndicator()); + } else if (state is BlankState) { + return LoadedSpaceView( + communities: state.communities, + selectedCommunity: null, + selectedSpace: null, + products: state.products, + ); } else if (state is SpaceManagementLoaded) { return LoadedSpaceView( communities: state.communities, diff --git a/lib/pages/spaces_management/widgets/loaded_space_widget.dart b/lib/pages/spaces_management/widgets/loaded_space_widget.dart index 5b340398..ef3cf3be 100644 --- a/lib/pages/spaces_management/widgets/loaded_space_widget.dart +++ b/lib/pages/spaces_management/widgets/loaded_space_widget.dart @@ -34,7 +34,9 @@ class _LoadedStateViewState extends State { children: [ SidebarWidget( communities: widget.communities, - selectedSpaceUuid: widget.selectedSpace?.uuid, + selectedSpaceUuid: widget.selectedSpace?.uuid ?? + widget.selectedCommunity?.uuid ?? + '', ), CommunityStructureArea( selectedCommunity: widget.selectedCommunity, diff --git a/lib/pages/spaces_management/widgets/sidebar_widget.dart b/lib/pages/spaces_management/widgets/sidebar_widget.dart index affc3fd0..efa33b48 100644 --- a/lib/pages/spaces_management/widgets/sidebar_widget.dart +++ b/lib/pages/spaces_management/widgets/sidebar_widget.dart @@ -14,18 +14,11 @@ import 'package:syncrow_web/utils/constants/assets.dart'; import 'package:syncrow_web/utils/style.dart'; class SidebarWidget extends StatefulWidget { - final Function(CommunityModel)? onCommunitySelected; - final Function(SpaceModel?)? onSpaceSelected; final List communities; - final Function(String?)? onSelectedSpaceChanged; // New callback - final String? selectedSpaceUuid; const SidebarWidget({ super.key, - this.onCommunitySelected, - this.onSpaceSelected, - this.onSelectedSpaceChanged, required this.communities, this.selectedSpaceUuid, }); @@ -145,7 +138,7 @@ class _SidebarWidgetState extends State { color: Colors.black, )), GestureDetector( - onTap: () => _showCreateCommunityDialog(context), + onTap: () => _navigateToBlank(context), child: Container( width: 30, height: 30, @@ -187,6 +180,15 @@ class _SidebarWidgetState extends State { ); } + void _navigateToBlank(BuildContext context) { + setState(() { + _selectedId = ''; + }); + context.read().add( + NewCommunityEvent(communities: widget.communities), + ); + } + Widget _buildCommunityTile(BuildContext context, CommunityModel community) { bool hasChildren = community.spaces.isNotEmpty;