Skip to content

Commit

Permalink
create api response pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
NC-jsAhonen committed Oct 21, 2024
1 parent 8a10018 commit c86e1b3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 32 deletions.
46 changes: 46 additions & 0 deletions src/api/callApiPaginated.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import callApi from "./callApi";

function* callApiPaginated(request: Request): Generator<any, any, any> {
let { response, bodyAsJson } = yield callApi(request);
const allResults = [...bodyAsJson.results];

if (bodyAsJson.next) {
let nextUrl = bodyAsJson.next;
while (nextUrl !== null) {
request = new Request(nextUrl, {
method: 'GET'
});
({ response, bodyAsJson } = yield callApi(request));
const status = response.status;

switch (status) {
case 200:
allResults.push(...bodyAsJson.results);
break;
case 204:
return {
response
};
break;

default:
return {
response,
bodyAsJson: {
exception: response.status,
message: response.statusText
}
};
break;
}

nextUrl = bodyAsJson.next || null;
}
}

bodyAsJson.results = allResults;

return { response, bodyAsJson };
}

export default callApiPaginated;
10 changes: 3 additions & 7 deletions src/leaseCreateCharge/requests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import callApi from "@/api/callApi";
import callApiPaginated from "@/api/callApiPaginated";
import createUrl from "@/api/createUrl";
import { store } from "@/index";
import { getCurrentLease } from '@/leases/selectors';
Expand All @@ -7,16 +8,11 @@ export const fetchAttributes = (): Generator<any, any, any> => {
method: 'OPTIONS'
}));
};
export const fetchReceivableTypes = (nextUrl: string): Generator<any, any, any> => {
export const fetchReceivableTypes = (): Generator<any, any, any> => {
const state = store.getState();
const lease = getCurrentLease(state);
const serviceUnit = lease.service_unit;
if (nextUrl) {
return callApi(new Request(nextUrl, {
method: 'GET'
}));
}
return callApi(new Request(createUrl(`receivable_type/`, {
return callApiPaginated(new Request(createUrl(`receivable_type/`, {
service_unit: serviceUnit.id
}), {
method: 'GET'
Expand Down
39 changes: 14 additions & 25 deletions src/leaseCreateCharge/saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { receiveAttributes, attributesNotFound, receiveReceivableTypes, receivab
import { receiveError } from "@/api/actions";
import { fetchAttributes, fetchReceivableTypes } from "./requests";

const SAFETY_CAP_FOR_WHILE_LOOP_ITERATIONS = 20;

function* fetchAttributesSaga(): Generator<any, any, any> {
try {
const {
Expand Down Expand Up @@ -32,37 +30,28 @@ function* fetchAttributesSaga(): Generator<any, any, any> {
}

function* fetchReceivableTypesSaga(): Generator<any, any, any> {
let nextUrl = ''
let iterationCounter = 0;
const allReceivableTypes = []
try {
while (nextUrl !== null && iterationCounter < SAFETY_CAP_FOR_WHILE_LOOP_ITERATIONS) {
const {
response: {
status: statusCode
},
bodyAsJson
} = yield call(fetchReceivableTypes, nextUrl);
const {
response: {
status: statusCode
},
bodyAsJson
} = yield call(fetchReceivableTypes);

switch (statusCode) {
case 200:
allReceivableTypes.push(...bodyAsJson.results);
nextUrl = bodyAsJson.next || null;
break;
switch (statusCode) {
case 200:
const receivableTypes = bodyAsJson.results;
yield put(receiveReceivableTypes(receivableTypes));
break;

default:
yield put(receivableTypesNotFound());
nextUrl = null;
break;
}
iterationCounter++;
default:
yield put(receivableTypesNotFound());
break;
}
yield put(receiveReceivableTypes(allReceivableTypes));
} catch (error) {
console.error('Failed to fetch receivable types with error "%s"', error);
yield put(receivableTypesNotFound());
yield put(receiveError(error));
nextUrl = null;
}
}

Expand Down

0 comments on commit c86e1b3

Please sign in to comment.