fixed edit community

This commit is contained in:
hannathkadher
2024-11-21 16:50:25 +04:00
parent 9be03850a5
commit 6c5b01e7c2
7 changed files with 109 additions and 15 deletions
@@ -20,6 +20,24 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
on<SaveSpacesEvent>(_onSaveSpaces);
on<FetchProductsEvent>(_onFetchProducts);
on<DeleteCommunityEvent>(_onCommunityDelete);
on<UpdateCommunityEvent>(_onUpdateCommunity);
}
void _onUpdateCommunity(
UpdateCommunityEvent event,
Emitter<SpaceManagementState> 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<SpaceManagementEvent, SpaceManagementStat
// Ensure parent.uuid is not null before calling the API
if (parent.uuid != null) {
await _api.deleteSpace(communityUuid, parent.uuid!);
}
}
} catch (e) {
print(
'Error deleting space ${parent.name} (UUID: ${parent.uuid}, Community UUID: $communityUuid): $e');
@@ -95,3 +95,16 @@ class LoadSpaceHierarchyEvent extends SpaceManagementEvent {
@override
List<Object> get props => [communityId];
}
class UpdateCommunityEvent extends SpaceManagementEvent {
final String communityUuid;
final String name;
const UpdateCommunityEvent({
required this.communityUuid,
required this.name,
});
@override
List<Object> get props => [communityUuid, name];
}
@@ -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<SpaceModel> 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
};
}
}
@@ -42,6 +42,8 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
double canvasHeight = 1000;
List<SpaceModel> spaces = [];
List<Connection> connections = [];
late TextEditingController _nameController;
bool isEditingName = false;
@override
void initState() {
@@ -49,6 +51,15 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
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<CommunityStructureArea> {
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<SpaceManagementBloc>().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,
),
),
],
),
],
),
+4 -2
View File
@@ -65,11 +65,13 @@ class CommunitySpaceManagementApi {
}
}
Future<bool> updateCommunity(String communityId, CommunityModel community) async {
Future<bool> 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;
},
+1
View File
@@ -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';
}