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

UIREC-427 Chunk HTTP requests to get pieces 'requests' by piece IDs #625

Merged
merged 2 commits into from
Dec 30, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Add "Mark late" action to the piece form action menu. Refs UIREC-413.
* Hide "Add piece" action when related order has "Pending" status and "Synchronized" workflow. Refs UIREC-362.
* *Breaking* Update "Send claim" action to use `pieces.send-claims` interface. Refs UIREC-412.
* Chunk HTTP requests to get pieces "requests" by piece IDs. Refs UIREC-427.

## [6.0.5](https://github.com/folio-org/ui-receiving/tree/v6.0.5) (2024-12-13)
[Full Changelog](https://github.com/folio-org/ui-receiving/compare/v6.0.4...v6.0.5)
Expand Down
3 changes: 3 additions & 0 deletions src/common/constants/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ export const BATCH_IDENTIFIER_TYPE = {
instanceHrid: 'instanceHrid',
itemBarcode: 'itemBarcode',
};

export const CHUNK_SIZE = 25;
export const MAX_PARALLEL_REQUESTS = 5;
37 changes: 28 additions & 9 deletions src/common/hooks/usePaginatedPieces/util.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import chunk from 'lodash/chunk';

import {
batchRequest,
ITEMS_API,
} from '@folio/stripes-acq-components';

import {
CHUNK_SIZE,
MAX_PARALLEL_REQUESTS,
PIECE_REQUESTS_API,
TENANT_ITEMS_API,
} from '../../constants';
Expand Down Expand Up @@ -38,15 +42,30 @@ export const fetchConsortiumPieceItems = (ky, { pieces }) => {
.then(({ tenantItems }) => tenantItems.map(({ item, tenantId }) => ({ tenantId, ...item })));
};

export const fetchLocalPieceRequests = (ky, { pieces }) => {
if (!pieces.length) {
return Promise.resolve([]);
export const fetchLocalPieceRequests = async (ky, { pieces }) => {
const results = [];

if (!pieces?.length) {
return Promise.resolve(results);
}

return ky
.get(PIECE_REQUESTS_API, {
searchParams: buildPieceRequestsSearchParams(pieces),
})
.json()
.then(({ circulationRequests }) => circulationRequests);
const pieceChunks = chunk(pieces, CHUNK_SIZE);
const pieceChunksGroups = chunk(pieceChunks, MAX_PARALLEL_REQUESTS);

for (const group of pieceChunksGroups) {
const responses = await Promise.all(
group.map(async (_chunk) => {
return ky.get(
PIECE_REQUESTS_API,
{ searchParams: buildPieceRequestsSearchParams(_chunk) },
)
.json()
.then(({ circulationRequests }) => circulationRequests);
}),
);

results.push(...responses);
}

return results.flat();
};
31 changes: 27 additions & 4 deletions src/common/hooks/usePiecesRequests/usePiecesRequests.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import chunk from 'lodash/chunk';
import { useQuery } from 'react-query';

import {
useNamespace,
useOkapiKy,
} from '@folio/stripes/core';

import { PIECE_REQUESTS_API } from '../../constants';
import {
CHUNK_SIZE,
MAX_PARALLEL_REQUESTS,
PIECE_REQUESTS_API,
} from '../../constants';
import { buildPieceRequestsSearchParams } from '../../utils';

const DEFAULT_DATA = [];
Expand All @@ -26,10 +31,28 @@ export const usePiecesRequests = (pieces = [], options = {}) => {
isLoading,
} = useQuery({
queryKey: [namespace, tenantId, pieces],
queryFn: ({ signal }) => {
const searchParams = buildPieceRequestsSearchParams(pieces);
queryFn: async ({ signal }) => {
const chunks = chunk(pieces, CHUNK_SIZE);
const chunkGroups = chunk(chunks, MAX_PARALLEL_REQUESTS);

const initialAccumulator = { circulationRequests: [], totalRecords: 0 };

for (const group of chunkGroups) {
const responses = await Promise.all(
group.map(async (_chunk) => {
const searchParams = buildPieceRequestsSearchParams(_chunk);

return ky.get(PIECE_REQUESTS_API, { searchParams, signal }).json();
}),
);

responses.forEach((response) => {
initialAccumulator.circulationRequests.push(...response.circulationRequests);
initialAccumulator.totalRecords += response.totalRecords;
});
}

return ky.get(PIECE_REQUESTS_API, { searchParams, signal }).json();
return initialAccumulator;
},
enabled: enabled && Boolean(pieces.length),
...queryOptions,
Expand Down
Loading