From ea20afd34e155ae7b446c5a065f371b75e31cc63 Mon Sep 17 00:00:00 2001 From: hannathkadher Date: Thu, 28 Nov 2024 15:16:07 +0400 Subject: [PATCH] fixed issue of not recursively calling communtiy list --- lib/services/space_mana_api.dart | 57 ++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/lib/services/space_mana_api.dart b/lib/services/space_mana_api.dart index 01248262..5d2464e6 100644 --- a/lib/services/space_mana_api.dart +++ b/lib/services/space_mana_api.dart @@ -8,22 +8,31 @@ import 'package:syncrow_web/utils/constants/api_const.dart'; class CommunitySpaceManagementApi { // Community Management APIs - Future> fetchCommunities() async { + Future> fetchCommunities({int page = 1}) async { try { - final response = await HTTPService().get( - path: ApiEndpoints.getCommunityList, - expectedResponseModel: (json) { - // Access the 'data' key from the response - List jsonData = json['data']; + List allCommunities = []; + bool hasNext = true; - // Check if jsonData is actually a List - List communityList = jsonData.map((jsonItem) { - return CommunityModel.fromJson(jsonItem); - }).toList(); - return communityList; - }, - ); - return response; + while (hasNext) { + await HTTPService().get( + path: ApiEndpoints.getCommunityList, + queryParameters: {'page': page}, + expectedResponseModel: (json) { + List jsonData = json['data']; + hasNext = json['hasNext'] ?? false; + int currentPage = json['page'] ?? 1; + List communityList = jsonData.map((jsonItem) { + return CommunityModel.fromJson(jsonItem); + }).toList(); + + allCommunities.addAll(communityList); + page = currentPage + 1; + return communityList; + }, + ); + } + + return allCommunities; } catch (e) { debugPrint('Error fetching communities: $e'); return []; @@ -33,7 +42,8 @@ class CommunitySpaceManagementApi { Future getCommunityById(String communityId) async { try { final response = await HTTPService().get( - path: ApiEndpoints.getCommunityById.replaceAll('{communityId}', communityId), + path: ApiEndpoints.getCommunityById + .replaceAll('{communityId}', communityId), expectedResponseModel: (json) { return CommunityModel.fromJson(json['data']); }, @@ -45,7 +55,8 @@ class CommunitySpaceManagementApi { } } - Future createCommunity(String name, String description) async { + Future createCommunity( + String name, String description) async { try { final response = await HTTPService().post( path: ApiEndpoints.createCommunity, @@ -67,7 +78,8 @@ class CommunitySpaceManagementApi { Future updateCommunity(String communityId, String name) async { try { final response = await HTTPService().put( - path: ApiEndpoints.updateCommunity.replaceAll('{communityId}', communityId), + path: ApiEndpoints.updateCommunity + .replaceAll('{communityId}', communityId), body: { 'name': name, }, @@ -85,7 +97,8 @@ class CommunitySpaceManagementApi { Future deleteCommunity(String communityId) async { try { final response = await HTTPService().delete( - path: ApiEndpoints.deleteCommunity.replaceAll('{communityId}', communityId), + path: ApiEndpoints.deleteCommunity + .replaceAll('{communityId}', communityId), expectedResponseModel: (json) { return json['success'] ?? false; }, @@ -236,10 +249,12 @@ class CommunitySpaceManagementApi { Future> getSpaceHierarchy(String communityId) async { try { final response = await HTTPService().get( - path: ApiEndpoints.getSpaceHierarchy.replaceAll('{communityId}', communityId), + path: ApiEndpoints.getSpaceHierarchy + .replaceAll('{communityId}', communityId), expectedResponseModel: (json) { - final spaceModels = - (json['data'] as List).map((spaceJson) => SpaceModel.fromJson(spaceJson)).toList(); + final spaceModels = (json['data'] as List) + .map((spaceJson) => SpaceModel.fromJson(spaceJson)) + .toList(); return spaceModels; },