mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-11-27 21:34:55 +00:00
fixed page load for space
This commit is contained in:
@ -1,5 +1,3 @@
|
||||
|
||||
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';
|
||||
@ -7,15 +5,14 @@ import 'package:syncrow_web/pages/spaces_management/bloc/space_management_event.
|
||||
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<SpaceManagementEvent, SpaceManagementState> {
|
||||
class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementState> {
|
||||
final CommunitySpaceManagementApi _api;
|
||||
|
||||
SpaceManagementBloc(this._api) : super(SpaceManagementInitial()) {
|
||||
on<LoadCommunityAndSpacesEvent>(_onLoadCommunityAndSpaces);
|
||||
on<CreateSpaceEvent>(_onCreateSpace);
|
||||
on<UpdateSpacePositionEvent>(_onUpdateSpacePosition);
|
||||
on<CreateCommunityEvent>(_onCreateCommunity);
|
||||
on<SaveSpacesEvent>(_onSaveSpaces);
|
||||
}
|
||||
|
||||
void _onLoadCommunityAndSpaces(
|
||||
@ -30,8 +27,7 @@ class SpaceManagementBloc
|
||||
// Use Future.wait to handle async calls within map
|
||||
List<CommunityModel> updatedCommunities = await Future.wait(
|
||||
communities.map((community) async {
|
||||
List<SpaceModel> spaces =
|
||||
await _api.getSpaceHierarchy(community.uuid);
|
||||
List<SpaceModel> spaces = await _api.getSpaceHierarchy(community.uuid);
|
||||
return CommunityModel(
|
||||
uuid: community.uuid,
|
||||
createdAt: community.createdAt,
|
||||
@ -50,15 +46,6 @@ class SpaceManagementBloc
|
||||
}
|
||||
}
|
||||
|
||||
void _onCreateSpace(
|
||||
CreateSpaceEvent event,
|
||||
Emitter<SpaceManagementState> emit,
|
||||
) {
|
||||
// Handle space creation logic
|
||||
// You can emit a new state here based on your needs
|
||||
emit(SpaceCreationSuccess());
|
||||
}
|
||||
|
||||
void _onUpdateSpacePosition(
|
||||
UpdateSpacePositionEvent event,
|
||||
Emitter<SpaceManagementState> emit,
|
||||
@ -82,9 +69,8 @@ class SpaceManagementBloc
|
||||
|
||||
if (newCommunity != null) {
|
||||
if (previousState is SpaceManagementLoaded) {
|
||||
final updatedCommunities =
|
||||
List<CommunityModel>.from(previousState.communities)
|
||||
..add(newCommunity);
|
||||
final updatedCommunities = List<CommunityModel>.from(previousState.communities)
|
||||
..add(newCommunity);
|
||||
emit(SpaceManagementLoaded(communities: updatedCommunities));
|
||||
}
|
||||
} else {
|
||||
@ -95,34 +81,91 @@ class SpaceManagementBloc
|
||||
}
|
||||
}
|
||||
|
||||
void _onSaveSpaces(
|
||||
SaveSpacesEvent event,
|
||||
Emitter<SpaceManagementState> emit,
|
||||
) async {
|
||||
final previousState = state;
|
||||
emit(SpaceManagementLoading());
|
||||
void _onSaveSpaces(
|
||||
SaveSpacesEvent event,
|
||||
Emitter<SpaceManagementState> 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,
|
||||
);
|
||||
}
|
||||
try {
|
||||
final updatedSpaces = await saveSpacesHierarchically(event.spaces, event.communityUuid);
|
||||
|
||||
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);
|
||||
emit(SpaceCreationSuccess(spaces: updatedSpaces));
|
||||
add(LoadCommunityAndSpacesEvent());
|
||||
} catch (e) {
|
||||
emit(SpaceManagementError('Error saving spaces: $e'));
|
||||
if (previousState is SpaceManagementLoaded) {
|
||||
emit(previousState);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<SpaceModel>> saveSpacesHierarchically(
|
||||
List<SpaceModel> spaces, String communityUuid) async {
|
||||
final orderedSpaces = flattenHierarchy(spaces);
|
||||
|
||||
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!,
|
||||
name: space.name,
|
||||
parentId: space.parent?.uuid,
|
||||
isPrivate: space.isPrivate,
|
||||
position: space.position,
|
||||
icon: space.icon,
|
||||
direction: space.incomingConnection?.direction,
|
||||
);
|
||||
} else {
|
||||
// Call create if the space does not have a UUID
|
||||
final response = await _api.createSpace(
|
||||
communityId: communityUuid,
|
||||
name: space.name,
|
||||
parentId: space.parent?.uuid,
|
||||
isPrivate: space.isPrivate,
|
||||
position: space.position,
|
||||
icon: space.icon,
|
||||
direction: space.incomingConnection?.direction,
|
||||
);
|
||||
space.uuid = response?.uuid;
|
||||
}
|
||||
} catch (e) {
|
||||
print('Error creating space ${space.name}: $e');
|
||||
rethrow; // Stop further execution on failure
|
||||
}
|
||||
}
|
||||
return spaces;
|
||||
}
|
||||
|
||||
List<SpaceModel> flattenHierarchy(List<SpaceModel> spaces) {
|
||||
final result = <SpaceModel>{}; // Use a Set to avoid duplicates
|
||||
|
||||
// Collect all top-level spaces (those without a parent)
|
||||
final topLevelSpaces = spaces.where((space) => space.parent == null);
|
||||
|
||||
void visit(SpaceModel space) {
|
||||
if (!result.contains(space)) {
|
||||
result.add(space); // Add the space
|
||||
for (var child in spaces.where((s) => s.parent == space)) {
|
||||
visit(child); // Recursively add children based on parent
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Start with top-level spaces
|
||||
for (var space in topLevelSpaces) {
|
||||
visit(space);
|
||||
}
|
||||
|
||||
// Add any spaces that were not part of the hierarchy
|
||||
for (var space in spaces) {
|
||||
if (!result.contains(space)) {
|
||||
result.add(space); // Add standalone or orphan spaces
|
||||
}
|
||||
}
|
||||
return result.toList(); // Convert back to a list
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user