Refactor CreateSpaceButton and CommunityStructureCanvas to utilize CommunityModel directly, enhancing data handling during space creation. Implement success callback for space creation to update community state in CommunitiesBloc, improving user experience and maintainability.

This commit is contained in:
Faris Armoush
2025-07-16 11:16:41 +03:00
parent 8e303af0d7
commit 62f67f5a5f
4 changed files with 107 additions and 28 deletions

View File

@ -268,7 +268,7 @@ class _CommunityStructureCanvasState extends State<CommunityStructureCanvas>
Positioned(
left: createButtonX,
top: createButtonY,
child: CreateSpaceButton(communityUuid: widget.community.uuid),
child: CreateSpaceButton(community: widget.community),
),
);
@ -327,6 +327,19 @@ class _CommunityStructureCanvasState extends State<CommunityStructureCanvas>
onTap: () => SpaceDetailsDialogHelper.showCreate(
context,
communityUuid: widget.community.uuid,
parentUuid: space.uuid,
onSuccess: (updatedSpaceModel) {
// TODO(FarisArmoush): insert the space under the parent, which is the space that was tapped
final newCommunity = widget.community.copyWith();
final parentIndex =
newCommunity.spaces.indexWhere((s) => s.uuid == space.uuid);
if (parentIndex != -1) {
newCommunity.spaces.insert(parentIndex + 1, updatedSpaceModel);
}
context.read<CommunitiesBloc>().add(
CommunitiesUpdateCommunity(newCommunity),
);
},
),
);

View File

@ -1,14 +1,17 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/models/community_model.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/communities/presentation/bloc/communities_bloc.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/space_details/presentation/helpers/space_details_dialog_helper.dart';
import 'package:syncrow_web/utils/color_manager.dart';
class CreateSpaceButton extends StatefulWidget {
const CreateSpaceButton({
required this.communityUuid,
required this.community,
super.key,
});
final String communityUuid;
final CommunityModel community;
@override
State<CreateSpaceButton> createState() => _CreateSpaceButtonState();
@ -25,7 +28,15 @@ class _CreateSpaceButtonState extends State<CreateSpaceButton> {
child: InkWell(
onTap: () => SpaceDetailsDialogHelper.showCreate(
context,
communityUuid: widget.communityUuid,
communityUuid: widget.community.uuid,
onSuccess: (updatedSpaceModel) {
final newCommunity = widget.community.copyWith(
spaces: [updatedSpaceModel, ...widget.community.spaces],
);
context.read<CommunitiesBloc>().add(
CommunitiesUpdateCommunity(newCommunity),
);
},
),
child: MouseRegion(
onEnter: (_) => setState(() => _isHovered = true),

View File

@ -5,6 +5,7 @@ import 'package:syncrow_web/pages/space_management_v2/main_module/widgets/commun
import 'package:syncrow_web/pages/space_management_v2/main_module/widgets/create_space_button.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/models/community_model.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/models/space_model.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/communities/presentation/bloc/communities_bloc.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/communities/presentation/communities_tree_selection_bloc/communities_tree_selection_bloc.dart';
class SpaceManagementCommunityStructure extends StatelessWidget {
@ -12,19 +13,35 @@ class SpaceManagementCommunityStructure extends StatelessWidget {
@override
Widget build(BuildContext context) {
final selectionBloc = context.watch<CommunitiesTreeSelectionBloc>().state;
final selectedCommunity = selectionBloc.selectedCommunity;
final selectedSpace = selectionBloc.selectedSpace;
return Column(
mainAxisSize: MainAxisSize.min,
children: [
const CommunityStructureHeader(),
Visibility(
visible: selectedCommunity!.spaces.isNotEmpty,
replacement: _buildEmptyWidget(selectedCommunity),
child: _buildCanvas(selectedCommunity, selectedSpace),
),
],
return BlocBuilder<CommunitiesTreeSelectionBloc, CommunitiesTreeSelectionState>(
builder: (context, state) {
final selectedCommunity = state.selectedCommunity;
final selectedSpace = state.selectedSpace;
if (selectedCommunity == null) {
return const SizedBox.shrink();
}
return Column(
mainAxisSize: MainAxisSize.min,
children: [
const CommunityStructureHeader(),
BlocBuilder<CommunitiesBloc, CommunitiesState>(
builder: (context, state) {
final community = state.communities.firstWhere(
(element) => element.uuid == selectedCommunity.uuid,
orElse: () => selectedCommunity,
);
return Visibility(
visible: community.spaces.isNotEmpty,
replacement: _buildEmptyWidget(community),
child: _buildCanvas(community, selectedSpace),
);
},
),
],
);
},
);
}
@ -47,11 +64,7 @@ class SpaceManagementCommunityStructure extends StatelessWidget {
child: Row(
children: [
spacer,
Expanded(
child: CreateSpaceButton(
communityUuid: selectedCommunity.uuid,
),
),
Expanded(child: CreateSpaceButton(community: selectedCommunity)),
spacer,
],
),

View File

@ -1,6 +1,9 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/models/space_model.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/create_space/data/services/remote_create_space_service.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/create_space/domain/params/create_space_param.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/create_space/presentation/bloc/create_space_bloc.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/space_details/data/services/remote_space_details_service.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/space_details/domain/models/space_details_model.dart';
import 'package:syncrow_web/pages/space_management_v2/modules/space_details/presentation/bloc/space_details_bloc.dart';
@ -14,6 +17,8 @@ abstract final class SpaceDetailsDialogHelper {
static void showCreate(
BuildContext context, {
required String communityUuid,
required void Function(SpaceModel updatedSpaceModel)? onSuccess,
String? parentUuid,
}) {
showDialog<void>(
context: context,
@ -24,14 +29,41 @@ abstract final class SpaceDetailsDialogHelper {
RemoteSpaceDetailsService(httpService: HTTPService()),
),
),
BlocProvider(
create: (context) => CreateSpaceBloc(
RemoteCreateSpaceService(HTTPService()),
),
),
],
child: Builder(
builder: (context) => SpaceDetailsDialog(
context: context,
title: const SelectableText('Create Space'),
spaceModel: SpaceModel.empty(),
onSave: (space) {},
communityUuid: communityUuid,
builder: (context) => BlocListener<CreateSpaceBloc, CreateSpaceState>(
listener: (context, state) => switch (state) {
CreateSpaceInitial() => null,
CreateSpaceLoading() => _onLoading(context),
CreateSpaceSuccess() => _onCreateSuccess(
context,
state.space,
onSuccess,
),
CreateSpaceFailure() => _onError(context, state.errorMessage),
},
child: SpaceDetailsDialog(
context: context,
title: const SelectableText('Create Space'),
spaceModel: SpaceModel.empty(),
onSave: (space) {
context.read<CreateSpaceBloc>().add(
CreateSpace(
CreateSpaceParam(
communityUuid: communityUuid,
space: space,
parentUuid: parentUuid,
),
),
);
},
communityUuid: communityUuid,
),
),
),
),
@ -135,4 +167,14 @@ abstract final class SpaceDetailsDialogHelper {
),
);
}
static void _onCreateSuccess(
BuildContext context,
SpaceModel space,
void Function(SpaceModel updatedSpaceModel)? onSuccess,
) {
Navigator.of(context).pop();
Navigator.of(context).pop();
onSuccess?.call(space);
}
}