Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

receivable types: fetch beyond page limit #534

Merged
merged 7 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
4 changes: 3 additions & 1 deletion src/leaseCreateCharge/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ export const getPayloadLeaseCreateCharge = (invoice: Record<string, any>): Recor
* @returns {Object}
*/
export const receivableTypesFromAttributes = (fieldAttributes: Record<string, any>, receivableTypes: Record<string, any>): Record<string, any> => {
const newChoices = fieldAttributes.choices.filter(choice => receivableTypes.find(type => type.is_active && type.id === choice.value));
const newChoices = fieldAttributes.choices
.filter(choice => receivableTypes.find(type => type.is_active && type.id === choice.value))
.sort((a, b) => a.display_name.localeCompare(b.display_name));
const newFieldAttributes = { ...fieldAttributes,
choices: newChoices
};
Expand Down
3 changes: 2 additions & 1 deletion 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 @@ -11,7 +12,7 @@ export const fetchReceivableTypes = (): Generator<any, any, any> => {
const state = store.getState();
const lease = getCurrentLease(state);
const serviceUnit = lease.service_unit;
return callApi(new Request(createUrl(`receivable_type/`, {
return callApiPaginated(new Request(createUrl(`receivable_type/`, {
service_unit: serviceUnit.id
}), {
method: 'GET'
Expand Down