mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-09 22:57:21 +00:00
fixed issue of not recursively calling communtiy list
This commit is contained in:
@ -8,22 +8,31 @@ import 'package:syncrow_web/utils/constants/api_const.dart';
|
||||
|
||||
class CommunitySpaceManagementApi {
|
||||
// Community Management APIs
|
||||
Future<List<CommunityModel>> fetchCommunities() async {
|
||||
Future<List<CommunityModel>> fetchCommunities({int page = 1}) async {
|
||||
try {
|
||||
final response = await HTTPService().get(
|
||||
path: ApiEndpoints.getCommunityList,
|
||||
expectedResponseModel: (json) {
|
||||
// Access the 'data' key from the response
|
||||
List<dynamic> jsonData = json['data'];
|
||||
List<CommunityModel> allCommunities = [];
|
||||
bool hasNext = true;
|
||||
|
||||
// Check if jsonData is actually a List
|
||||
List<CommunityModel> 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<dynamic> jsonData = json['data'];
|
||||
hasNext = json['hasNext'] ?? false;
|
||||
int currentPage = json['page'] ?? 1;
|
||||
List<CommunityModel> 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<CommunityModel?> 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<CommunityModel?> createCommunity(String name, String description) async {
|
||||
Future<CommunityModel?> createCommunity(
|
||||
String name, String description) async {
|
||||
try {
|
||||
final response = await HTTPService().post(
|
||||
path: ApiEndpoints.createCommunity,
|
||||
@ -67,7 +78,8 @@ class CommunitySpaceManagementApi {
|
||||
Future<bool> 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<bool> 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<List<SpaceModel>> 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;
|
||||
},
|
||||
|
Reference in New Issue
Block a user