fixed issues in space

This commit is contained in:
hannathkadher
2024-10-30 11:04:36 +04:00
parent d025da6daf
commit 489789da0a
4 changed files with 26 additions and 13 deletions

View File

@ -269,17 +269,21 @@ class HomeCubit extends Cubit<HomeState> {
await fetchRoomsByUnitId(selectedSpace!);
emitSafe(GetSpacesSuccess(spaces!));
} else {
print("here in else");
emitSafe(GetSpacesError("No spaces found"));
}
}
fetchRoomsByUnitId(SpaceModel space) async {
print("Community ID: ${space.community.uuid}");
print("Space ID: ${space.id}");
emitSafe(GetSpaceRoomsLoading());
try {
space.subspaces =
await SpacesAPI.getSubSpaceBySpaceId(space.community.uuid, space.id);
} catch (failure) {
print("Error fetching subspaces: $failure");
emitSafe(GetSpaceRoomsError(failure.toString()));
return;
}

View File

@ -6,14 +6,14 @@ class Community {
Community({
required this.uuid,
required this.name,
required this.description,
this.description = '',
});
factory Community.fromJson(Map<String, dynamic> json) {
return Community(
uuid: json['uuid'],
name: json['name'],
description: json['description'],
uuid: json['uuid'] ?? '',
name: json['name'] ?? 'Unnamed Community',
description: json['description'] ?? '',
);
}

View File

@ -26,13 +26,17 @@ class SpaceModel {
/// Factory constructor to create an instance from JSON.
factory SpaceModel.fromJson(Map<String, dynamic> json) {
final spaceData = json['space'] as Map<String, dynamic>? ?? {};
// Extract and log each part of space data
final id = json['uuid'] ?? '';
final name = json['spaceName'] ?? 'Unnamed Space';
final communityJson = json['community'] ?? {};
return SpaceModel(
id: json['uuid'] ?? '',
name: spaceData['spaceName'] ?? 'Unnamed Space',
community: Community.fromJson(spaceData['community'] ?? {}),
subspaces: (spaceData['subspaces'] as List<dynamic>?)
id: id,
name: name,
community: Community.fromJson(
communityJson), // Ensure Community is created correctly
subspaces: (json['subspaces'] as List<dynamic>?)
?.map((item) => SubSpaceModel.fromJson(item))
.toList() ??
[],
@ -41,6 +45,9 @@ class SpaceModel {
/// Helper method to parse a list of SpaceModel from JSON.
static List<SpaceModel> fromJsonList(List<dynamic> jsonList) {
return jsonList.map((item) => SpaceModel.fromJson(item)).toList();
return jsonList.map((item) {
final spaceData = item['space']; // Extract the `space` object
return SpaceModel.fromJson(spaceData); // Pass to SpaceModel.fromJson
}).toList();
}
}

View File

@ -18,12 +18,13 @@ class SpacesAPI {
final response = await _httpService.get(
path: path,
showServerMessage: false,
expectedResponseModel: (json) => SpaceModel.fromJsonList(json['data']),
expectedResponseModel: (json) {
return SpaceModel.fromJsonList(json['data']);
},
);
return response;
} catch (error) {
print("Error in getSpacesByUserId: $error");
rethrow; // Rethrow the error to be caught by `fetchUnitsByUserId`
}
}
@ -33,6 +34,7 @@ class SpacesAPI {
final path = ApiEndpoints.listSubspace
.replaceFirst('{communityUuid}', communityId)
.replaceFirst('{spaceUuid}', spaceId);
print("Constructed path: $path");
final response = await _httpService.get(
path: path,