mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-08-25 20:59:40 +00:00
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:
@ -268,7 +268,7 @@ class _CommunityStructureCanvasState extends State<CommunityStructureCanvas>
|
|||||||
Positioned(
|
Positioned(
|
||||||
left: createButtonX,
|
left: createButtonX,
|
||||||
top: createButtonY,
|
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(
|
onTap: () => SpaceDetailsDialogHelper.showCreate(
|
||||||
context,
|
context,
|
||||||
communityUuid: widget.community.uuid,
|
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),
|
||||||
|
);
|
||||||
|
},
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -1,14 +1,17 @@
|
|||||||
import 'package:flutter/material.dart';
|
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/pages/space_management_v2/modules/space_details/presentation/helpers/space_details_dialog_helper.dart';
|
||||||
import 'package:syncrow_web/utils/color_manager.dart';
|
import 'package:syncrow_web/utils/color_manager.dart';
|
||||||
|
|
||||||
class CreateSpaceButton extends StatefulWidget {
|
class CreateSpaceButton extends StatefulWidget {
|
||||||
const CreateSpaceButton({
|
const CreateSpaceButton({
|
||||||
required this.communityUuid,
|
required this.community,
|
||||||
super.key,
|
super.key,
|
||||||
});
|
});
|
||||||
|
|
||||||
final String communityUuid;
|
final CommunityModel community;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<CreateSpaceButton> createState() => _CreateSpaceButtonState();
|
State<CreateSpaceButton> createState() => _CreateSpaceButtonState();
|
||||||
@ -25,7 +28,15 @@ class _CreateSpaceButtonState extends State<CreateSpaceButton> {
|
|||||||
child: InkWell(
|
child: InkWell(
|
||||||
onTap: () => SpaceDetailsDialogHelper.showCreate(
|
onTap: () => SpaceDetailsDialogHelper.showCreate(
|
||||||
context,
|
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(
|
child: MouseRegion(
|
||||||
onEnter: (_) => setState(() => _isHovered = true),
|
onEnter: (_) => setState(() => _isHovered = true),
|
||||||
|
@ -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/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/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/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';
|
import 'package:syncrow_web/pages/space_management_v2/modules/communities/presentation/communities_tree_selection_bloc/communities_tree_selection_bloc.dart';
|
||||||
|
|
||||||
class SpaceManagementCommunityStructure extends StatelessWidget {
|
class SpaceManagementCommunityStructure extends StatelessWidget {
|
||||||
@ -12,20 +13,36 @@ class SpaceManagementCommunityStructure extends StatelessWidget {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final selectionBloc = context.watch<CommunitiesTreeSelectionBloc>().state;
|
return BlocBuilder<CommunitiesTreeSelectionBloc, CommunitiesTreeSelectionState>(
|
||||||
final selectedCommunity = selectionBloc.selectedCommunity;
|
builder: (context, state) {
|
||||||
final selectedSpace = selectionBloc.selectedSpace;
|
final selectedCommunity = state.selectedCommunity;
|
||||||
|
final selectedSpace = state.selectedSpace;
|
||||||
|
|
||||||
|
if (selectedCommunity == null) {
|
||||||
|
return const SizedBox.shrink();
|
||||||
|
}
|
||||||
|
|
||||||
return Column(
|
return Column(
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
children: [
|
children: [
|
||||||
const CommunityStructureHeader(),
|
const CommunityStructureHeader(),
|
||||||
Visibility(
|
BlocBuilder<CommunitiesBloc, CommunitiesState>(
|
||||||
visible: selectedCommunity!.spaces.isNotEmpty,
|
builder: (context, state) {
|
||||||
replacement: _buildEmptyWidget(selectedCommunity),
|
final community = state.communities.firstWhere(
|
||||||
child: _buildCanvas(selectedCommunity, selectedSpace),
|
(element) => element.uuid == selectedCommunity.uuid,
|
||||||
|
orElse: () => selectedCommunity,
|
||||||
|
);
|
||||||
|
return Visibility(
|
||||||
|
visible: community.spaces.isNotEmpty,
|
||||||
|
replacement: _buildEmptyWidget(community),
|
||||||
|
child: _buildCanvas(community, selectedSpace),
|
||||||
|
);
|
||||||
|
},
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _buildCanvas(
|
Widget _buildCanvas(
|
||||||
@ -47,11 +64,7 @@ class SpaceManagementCommunityStructure extends StatelessWidget {
|
|||||||
child: Row(
|
child: Row(
|
||||||
children: [
|
children: [
|
||||||
spacer,
|
spacer,
|
||||||
Expanded(
|
Expanded(child: CreateSpaceButton(community: selectedCommunity)),
|
||||||
child: CreateSpaceButton(
|
|
||||||
communityUuid: selectedCommunity.uuid,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
spacer,
|
spacer,
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.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/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/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/domain/models/space_details_model.dart';
|
||||||
import 'package:syncrow_web/pages/space_management_v2/modules/space_details/presentation/bloc/space_details_bloc.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(
|
static void showCreate(
|
||||||
BuildContext context, {
|
BuildContext context, {
|
||||||
required String communityUuid,
|
required String communityUuid,
|
||||||
|
required void Function(SpaceModel updatedSpaceModel)? onSuccess,
|
||||||
|
String? parentUuid,
|
||||||
}) {
|
}) {
|
||||||
showDialog<void>(
|
showDialog<void>(
|
||||||
context: context,
|
context: context,
|
||||||
@ -24,14 +29,41 @@ abstract final class SpaceDetailsDialogHelper {
|
|||||||
RemoteSpaceDetailsService(httpService: HTTPService()),
|
RemoteSpaceDetailsService(httpService: HTTPService()),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
BlocProvider(
|
||||||
|
create: (context) => CreateSpaceBloc(
|
||||||
|
RemoteCreateSpaceService(HTTPService()),
|
||||||
|
),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
child: Builder(
|
child: Builder(
|
||||||
builder: (context) => SpaceDetailsDialog(
|
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,
|
context: context,
|
||||||
title: const SelectableText('Create Space'),
|
title: const SelectableText('Create Space'),
|
||||||
spaceModel: SpaceModel.empty(),
|
spaceModel: SpaceModel.empty(),
|
||||||
onSave: (space) {},
|
onSave: (space) {
|
||||||
|
context.read<CreateSpaceBloc>().add(
|
||||||
|
CreateSpace(
|
||||||
|
CreateSpaceParam(
|
||||||
communityUuid: communityUuid,
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user