From 6c5b01e7c210f1034c4eabfbbc05bbeced2beb82 Mon Sep 17 00:00:00 2001 From: hannathkadher Date: Thu, 21 Nov 2024 16:50:25 +0400 Subject: [PATCH] fixed edit community --- assets/icons/edit.svg | 4 ++ .../bloc/space_management_bloc.dart | 20 +++++- .../bloc/space_management_event.dart | 13 ++++ .../model/community_model.dart | 13 ++-- .../widgets/community_structure_widget.dart | 67 ++++++++++++++++++- lib/services/space_mana_api.dart | 6 +- lib/utils/constants/assets.dart | 1 + 7 files changed, 109 insertions(+), 15 deletions(-) create mode 100644 assets/icons/edit.svg diff --git a/assets/icons/edit.svg b/assets/icons/edit.svg new file mode 100644 index 00000000..ac510f4a --- /dev/null +++ b/assets/icons/edit.svg @@ -0,0 +1,4 @@ + + + + diff --git a/lib/pages/spaces_management/bloc/space_management_bloc.dart b/lib/pages/spaces_management/bloc/space_management_bloc.dart index 4b79ff1c..401a960a 100644 --- a/lib/pages/spaces_management/bloc/space_management_bloc.dart +++ b/lib/pages/spaces_management/bloc/space_management_bloc.dart @@ -20,6 +20,24 @@ class SpaceManagementBloc extends Bloc(_onSaveSpaces); on(_onFetchProducts); on(_onCommunityDelete); + on(_onUpdateCommunity); + } + + void _onUpdateCommunity( + UpdateCommunityEvent event, + Emitter emit, + ) async { + try { + emit(SpaceManagementLoading()); + final success = await _api.updateCommunity(event.communityUuid, event.name); + if (success) { + add(LoadCommunityAndSpacesEvent()); + } else { + emit(const SpaceManagementError('Failed to update the community.')); + } + } catch (e) { + emit(SpaceManagementError('Error updating community: $e')); + } } void _onFetchProducts( @@ -160,7 +178,7 @@ class SpaceManagementBloc extends Bloc get props => [communityId]; } + +class UpdateCommunityEvent extends SpaceManagementEvent { + final String communityUuid; + final String name; + + const UpdateCommunityEvent({ + required this.communityUuid, + required this.name, + }); + + @override + List get props => [communityUuid, name]; +} diff --git a/lib/pages/spaces_management/model/community_model.dart b/lib/pages/spaces_management/model/community_model.dart index 182aee73..b61b780b 100644 --- a/lib/pages/spaces_management/model/community_model.dart +++ b/lib/pages/spaces_management/model/community_model.dart @@ -5,7 +5,7 @@ class CommunityModel { final String uuid; final DateTime createdAt; final DateTime updatedAt; - final String name; + String name; final String description; final RegionModel? region; List spaces; @@ -27,12 +27,9 @@ class CommunityModel { updatedAt: DateTime.parse(json['updatedAt']), name: json['name'], description: json['description'], - region: - json['region'] != null ? RegionModel.fromJson(json['region']) : null, + region: json['region'] != null ? RegionModel.fromJson(json['region']) : null, spaces: json['spaces'] != null - ? (json['spaces'] as List) - .map((space) => SpaceModel.fromJson(space)) - .toList() + ? (json['spaces'] as List).map((space) => SpaceModel.fromJson(space)).toList() : [], ); } @@ -45,9 +42,7 @@ class CommunityModel { 'name': name, 'description': description, 'region': region?.toJson(), - 'spaces': spaces - .map((space) => space.toMap()) - .toList(), // Convert spaces to Map + 'spaces': spaces.map((space) => space.toMap()).toList(), // Convert spaces to Map }; } } diff --git a/lib/pages/spaces_management/widgets/community_structure_widget.dart b/lib/pages/spaces_management/widgets/community_structure_widget.dart index 51bdccc2..a6a679f4 100644 --- a/lib/pages/spaces_management/widgets/community_structure_widget.dart +++ b/lib/pages/spaces_management/widgets/community_structure_widget.dart @@ -42,6 +42,8 @@ class _CommunityStructureAreaState extends State { double canvasHeight = 1000; List spaces = []; List connections = []; + late TextEditingController _nameController; + bool isEditingName = false; @override void initState() { @@ -49,6 +51,15 @@ class _CommunityStructureAreaState extends State { spaces = widget.spaces.isNotEmpty ? flattenSpaces(widget.spaces) : []; connections = widget.spaces.isNotEmpty ? createConnections(widget.spaces) : []; _adjustCanvasSizeForSpaces(); + _nameController = TextEditingController( + text: widget.selectedCommunity?.name ?? '', + ); + } + + @override + void dispose() { + _nameController.dispose(); + super.dispose(); } @override @@ -176,9 +187,59 @@ class _CommunityStructureAreaState extends State { style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold), ), if (widget.selectedCommunity != null) - Text( - widget.selectedCommunity?.name ?? '', - style: const TextStyle(fontSize: 16, color: ColorsManager.blackColor), + Row( + children: [ + // Show Text widget when not editing + if (!isEditingName) + Text( + widget.selectedCommunity?.name ?? '', + style: const TextStyle(fontSize: 16, color: ColorsManager.blackColor), + ), + if (isEditingName) // Show TextField when editing + SizedBox( + width: 200, // Adjusted width to make TextField visible + child: TextField( + controller: _nameController, + decoration: const InputDecoration( + border: InputBorder.none, + isDense: true, // Reduce the height of the TextField + ), + style: const TextStyle( + fontSize: 16, + color: ColorsManager.blackColor, + ), + onSubmitted: (value) { + context.read().add( + UpdateCommunityEvent( + communityUuid: widget.selectedCommunity!.uuid, + name: value, + ), + ); + setState(() { + widget.selectedCommunity?.name = value; // Update the name + isEditingName = false; // Exit edit mode + }); + }, + ), + ), + const SizedBox(width: 8), + if (!isEditingName) + GestureDetector( + onTap: () { + setState(() { + isEditingName = !isEditingName; // Toggle edit mode + }); + if (isEditingName) { + _nameController.text = widget.selectedCommunity?.name ?? ''; // Pre-fill + } + }, + child: SvgPicture.asset( + Assets.iconEdit, // Path to the edit icon SVG asset + width: 16, + height: 16, + ), + ), + ], ), ], ), diff --git a/lib/services/space_mana_api.dart b/lib/services/space_mana_api.dart index e09fe8f6..07b98f10 100644 --- a/lib/services/space_mana_api.dart +++ b/lib/services/space_mana_api.dart @@ -65,11 +65,13 @@ class CommunitySpaceManagementApi { } } - Future updateCommunity(String communityId, CommunityModel community) async { + Future updateCommunity(String communityId, String name) async { try { final response = await HTTPService().put( path: ApiEndpoints.updateCommunity.replaceAll('{communityId}', communityId), - body: community.toMap(), + body: { + 'name': name, + }, expectedResponseModel: (json) { return json['success'] ?? false; }, diff --git a/lib/utils/constants/assets.dart b/lib/utils/constants/assets.dart index d96ce0dc..6abd689e 100644 --- a/lib/utils/constants/assets.dart +++ b/lib/utils/constants/assets.dart @@ -236,4 +236,5 @@ class Assets { static const String doorSensor = 'assets/icons/door_sensor.svg'; static const String delete = 'assets/icons/delete.svg'; + static const String edit = 'assets/icons/edit.svg'; }