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!); await fetchRoomsByUnitId(selectedSpace!);
emitSafe(GetSpacesSuccess(spaces!)); emitSafe(GetSpacesSuccess(spaces!));
} else { } else {
print("here in else");
emitSafe(GetSpacesError("No spaces found")); emitSafe(GetSpacesError("No spaces found"));
} }
} }
fetchRoomsByUnitId(SpaceModel space) async { fetchRoomsByUnitId(SpaceModel space) async {
print("Community ID: ${space.community.uuid}");
print("Space ID: ${space.id}");
emitSafe(GetSpaceRoomsLoading()); emitSafe(GetSpaceRoomsLoading());
try { try {
space.subspaces = space.subspaces =
await SpacesAPI.getSubSpaceBySpaceId(space.community.uuid, space.id); await SpacesAPI.getSubSpaceBySpaceId(space.community.uuid, space.id);
} catch (failure) { } catch (failure) {
print("Error fetching subspaces: $failure");
emitSafe(GetSpaceRoomsError(failure.toString())); emitSafe(GetSpaceRoomsError(failure.toString()));
return; return;
} }

View File

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

View File

@ -26,13 +26,17 @@ class SpaceModel {
/// Factory constructor to create an instance from JSON. /// Factory constructor to create an instance from JSON.
factory SpaceModel.fromJson(Map<String, dynamic> 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( return SpaceModel(
id: json['uuid'] ?? '', id: id,
name: spaceData['spaceName'] ?? 'Unnamed Space', name: name,
community: Community.fromJson(spaceData['community'] ?? {}), community: Community.fromJson(
subspaces: (spaceData['subspaces'] as List<dynamic>?) communityJson), // Ensure Community is created correctly
subspaces: (json['subspaces'] as List<dynamic>?)
?.map((item) => SubSpaceModel.fromJson(item)) ?.map((item) => SubSpaceModel.fromJson(item))
.toList() ?? .toList() ??
[], [],
@ -41,6 +45,9 @@ class SpaceModel {
/// Helper method to parse a list of SpaceModel from JSON. /// Helper method to parse a list of SpaceModel from JSON.
static List<SpaceModel> fromJsonList(List<dynamic> jsonList) { 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( final response = await _httpService.get(
path: path, path: path,
showServerMessage: false, showServerMessage: false,
expectedResponseModel: (json) => SpaceModel.fromJsonList(json['data']), expectedResponseModel: (json) {
return SpaceModel.fromJsonList(json['data']);
},
); );
return response; return response;
} catch (error) { } catch (error) {
print("Error in getSpacesByUserId: $error");
rethrow; // Rethrow the error to be caught by `fetchUnitsByUserId` rethrow; // Rethrow the error to be caught by `fetchUnitsByUserId`
} }
} }
@ -33,6 +34,7 @@ class SpacesAPI {
final path = ApiEndpoints.listSubspace final path = ApiEndpoints.listSubspace
.replaceFirst('{communityUuid}', communityId) .replaceFirst('{communityUuid}', communityId)
.replaceFirst('{spaceUuid}', spaceId); .replaceFirst('{spaceUuid}', spaceId);
print("Constructed path: $path");
final response = await _httpService.get( final response = await _httpService.get(
path: path, path: path,