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 3 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
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
7 changes: 6 additions & 1 deletion src/leaseCreateCharge/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ export const fetchAttributes = (): Generator<any, any, any> => {
method: 'OPTIONS'
}));
};
export const fetchReceivableTypes = (): Generator<any, any, any> => {
export const fetchReceivableTypes = (nextUrl: string): Generator<any, any, any> => {
const state = store.getState();
const lease = getCurrentLease(state);
const serviceUnit = lease.service_unit;
if (nextUrl) {
henrinie-nc marked this conversation as resolved.
Show resolved Hide resolved
return callApi(new Request(nextUrl, {
method: 'GET'
}));
}
return callApi(new Request(createUrl(`receivable_type/`, {
service_unit: serviceUnit.id
}), {
Expand Down
35 changes: 21 additions & 14 deletions src/leaseCreateCharge/saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,35 @@ function* fetchAttributesSaga(): Generator<any, any, any> {
}

function* fetchReceivableTypesSaga(): Generator<any, any, any> {
let nextUrl = ''
const allReceivableTypes = []
try {
const {
response: {
status: statusCode
},
bodyAsJson
} = yield call(fetchReceivableTypes);
while (typeof nextUrl === 'string') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this enough of a check to see whether to continue? Maybe something else could be checked to ensure it doesn't end up in infinite loop

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there are more pages, bodyAsJson.next will be a string that contains the URL. If there are no pages left, bodyAsJson.next will be null.

In all cases of the switch case below, the nextUrl will be either a string (should continue), or null (should stop). If there is an error in the call(fetchRecceivableTypes, nextUrl) line, the error is caught and nextUrl is again set to null, just in case even though we are out of the loop by then.

The condition could also be (nextUrl !== null), but it should achieve the same thing.

const {
response: {
status: statusCode
},
bodyAsJson
} = yield call(fetchReceivableTypes, nextUrl);

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

default:
yield put(receivableTypesNotFound());
break;
default:
yield put(receivableTypesNotFound());
nextUrl = null;
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