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

revert: UIREC-393 invalid reference issues due to BE resolving the invalid holding/locations data #611

Merged
merged 2 commits into from
Nov 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
3 changes: 0 additions & 3 deletions src/Piece/PieceForm/PieceFields/PieceFields.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export const PieceFields = ({
instanceId,
locationIds,
locations,
isLocationsLoading,
pieceFormatOptions,
poLine,
setLocationValue,
Expand Down Expand Up @@ -299,7 +298,6 @@ export const PieceFields = ({
disabled={isLocationSelectionDisabled}
isNonInteractive={isLocationSelectionDisabled}
required={isLocationRequired}
isLoading={isLocationsLoading}
/>
</Col>
</Row>
Expand All @@ -314,7 +312,6 @@ PieceFields.propTypes = {
label: PropTypes.string,
value: PropTypes.string,
})),
isLocationsLoading: PropTypes.bool,
locationIds: PropTypes.arrayOf(PropTypes.string),
locations: PropTypes.arrayOf(PropTypes.object),
poLine: PropTypes.object.isRequired,
Expand Down
36 changes: 4 additions & 32 deletions src/Piece/PieceForm/PieceForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import PropTypes from 'prop-types';
import {
useCallback,
useEffect,
useMemo,
useRef,
} from 'react';
import { FormattedMessage } from 'react-intl';
Expand Down Expand Up @@ -33,10 +32,6 @@ import {
useModalToggle,
} from '@folio/stripes-acq-components';

import {
useHoldingsAndLocations,
useReceivingTenantIdsAndLocations,
} from '../../common/hooks';
import {
getClaimingIntervalFromDate,
setLocationValueFormMutator,
Expand Down Expand Up @@ -68,6 +63,8 @@ const PieceForm = ({
onClose,
onDelete: onDeleteProp,
onUnreceive: onUnreceiveProp,
locationIds,
locations,
paneTitle,
pieceFormatOptions,
poLine,
Expand Down Expand Up @@ -95,36 +92,10 @@ const PieceForm = ({
bindItemId,
isBound,
isCreateItem,
locationId,
holdingId,
metadata,
receivingStatus,
receivingTenantId,
} = formValues;

const receivingTenants = useMemo(() => {
if (poLine?.locations?.length) {
return poLine.locations.map(({ tenantId }) => tenantId);
}

return [];
}, [poLine?.locations]);

const receivingTenantIdsAndLocations = useReceivingTenantIdsAndLocations({
receivingTenantIds: receivingTenants,
currentReceivingTenantId: receivingTenantId,
currentLocationId: locationId,
});

const {
locations,
locationIds,
isFetching,
} = useHoldingsAndLocations({
instanceId,
...receivingTenantIdsAndLocations,
});

useEffect(() => {
if (!id && format === PIECE_FORMAT.electronic) {
batch(() => {
Expand Down Expand Up @@ -351,7 +322,6 @@ const PieceForm = ({
poLine={poLine}
locationIds={locationIds}
locations={locations}
isLocationsLoading={isEditMode && (!locationId || !holdingId) && isFetching}
setLocationValue={mutators.setLocationValue}
onChangeDisplayOnHolding={onChangeDisplayOnHolding}
/>
Expand Down Expand Up @@ -427,6 +397,8 @@ PieceForm.propTypes = {
hasValidationErrors: PropTypes.bool.isRequired,
initialValues: PropTypes.object.isRequired,
instanceId: PropTypes.string,
locationIds: PropTypes.arrayOf(PropTypes.string).isRequired,
locations: PropTypes.arrayOf(PropTypes.object),
onClose: PropTypes.func.isRequired,
onDelete: PropTypes.func.isRequired,
onUnreceive: PropTypes.func.isRequired,
Expand Down
17 changes: 17 additions & 0 deletions src/Piece/PieceForm/PieceFormContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
PIECE_FORMAT,
PIECE_FORMAT_OPTIONS,
useAcqRestrictions,
useLocationsQuery,
useOrderLine,
useShowCallout,
} from '@folio/stripes-acq-components';
Expand Down Expand Up @@ -67,11 +68,17 @@ export const PieceFormContainer = ({

const {
targetTenantId: tenantId,
isCentralOrderingEnabled,
isCentralRouting,
} = useReceivingSearchContext();

/* Data fetching */

const {
isLoading: isLocationsLoading,
locations,
} = useLocationsQuery({ consortium: isCentralOrderingEnabled });

const {
isLoading: isTitleLoading,
title,
Expand Down Expand Up @@ -115,13 +122,20 @@ export const PieceFormContainer = ({

const canDeletePiece = !(!orderLine?.checkinItems && order?.workflowStatus === ORDER_STATUSES.pending);
const instanceId = title?.instanceId;
const pieceLocationId = initialValues?.locationId;
const orderFormat = orderLine?.orderFormat;
const pieceFormatOptions = orderFormat === ORDER_FORMATS.PEMix
? PIECE_FORMAT_OPTIONS.filter(({ value }) => [PIECE_FORMAT.electronic, PIECE_FORMAT.physical].includes(value))
: PIECE_FORMAT_OPTIONS.filter(({ value }) => value === initialValues?.format);

/* Memoized values */

const locationIds = useMemo(() => {
const poLineLocationIds = (orderLine?.locations?.map(({ locationId }) => locationId) ?? []).filter(Boolean);

return (pieceLocationId ? [...new Set([...poLineLocationIds, pieceLocationId])] : poLineLocationIds);
}, [orderLine, pieceLocationId]);

const createInventoryValues = useMemo(() => ({
[PIECE_FORMAT.physical]: orderLine?.physical?.createInventory,
[PIECE_FORMAT.electronic]: orderLine?.eresource?.createInventory,
Expand Down Expand Up @@ -284,6 +298,7 @@ export const PieceFormContainer = ({
const isLoading = (
!initialValues
|| isLoadingProp
|| isLocationsLoading
|| isTitleLoading
|| isOrderLineLoading
|| isOrderLoading
Expand All @@ -305,6 +320,8 @@ export const PieceFormContainer = ({
onQuickReceive={onQuickReceive}
onSubmit={onSubmit}
onUnreceive={onUnreceive}
locationIds={locationIds}
locations={locations}
paneTitle={paneTitle}
pieceFormatOptions={pieceFormatOptions}
poLine={orderLine}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ import {
TextField,
} from '@folio/stripes/components';

import {
useHoldingsAndLocations,
useReceivingTenantIdsAndLocations,
} from '../../common/hooks';
import { useHoldingsAndLocations } from '../../common/hooks';
import { useReceivingSearchContext } from '../../contexts';
import { PIECE_FORM_FIELD_NAMES } from '../constants';
import {
Expand All @@ -39,22 +36,9 @@ export const TitleBindPiecesCreateItemForm = ({
const { loanTypes } = useLoanTypes();
const intl = useIntl();

const { locationId, tenantId: receivingTenantId } = bindItemValues;

const {
additionalLocationIds,
additionalTenantLocationIdsMap,
tenantId,
} = useReceivingTenantIdsAndLocations({
currentLocationId: locationId,
currentReceivingTenantId: receivingTenantId,
});

const { locations, isFetching } = useHoldingsAndLocations({
const { locations } = useHoldingsAndLocations({
instanceId,
tenantId,
additionalLocationIds,
additionalTenantLocationIdsMap,
tenantId: bindItemValues.tenantId,
});

const { crossTenant } = useReceivingSearchContext();
Expand Down Expand Up @@ -155,7 +139,6 @@ export const TitleBindPiecesCreateItemForm = ({
onChange={selectLocation}
locationLabelId="ui-receiving.piece.permanentLocationId"
holdingLabelId="ui-receiving.piece.permanentLocationId"
isLoading={isFetching}
required
/>
</Col>
Expand Down
28 changes: 7 additions & 21 deletions src/TitleReceive/TitleReceiveContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ import {
} from '@folio/stripes/components';
import {
PIECE_FORMAT,
useLocationsQuery,
useShowCallout,
} from '@folio/stripes-acq-components';

import {
useHoldingsAndLocations,
useReceive,
useReceivingTenantIdsAndLocations,
useTitleHydratedPieces,
} from '../common/hooks';
import {
Expand All @@ -38,6 +37,7 @@ function TitleReceiveContainer({ history, location, match }) {
const showCallout = useShowCallout();
const {
crossTenant,
isCentralOrderingEnabled,
isCentralRouting,
targetTenantId,
} = useReceivingSearchContext();
Expand All @@ -59,24 +59,10 @@ function TitleReceiveContainer({ history, location, match }) {

const { receive } = useReceive();

const receivingTenants = useMemo(() => {
if (pieces?.length) {
return pieces.map(({ receivingTenantId }) => receivingTenantId);
}

return [];
}, [pieces]);

const { receivingTenantIds } = useReceivingTenantIdsAndLocations({
receivingTenantIds: receivingTenants,
currentReceivingTenantId: targetTenantId,
});

const { locations, isFetching } = useHoldingsAndLocations({
instanceId,
receivingTenantIds,
tenantId: targetTenantId,
});
const {
isLoading: isLocationsLoading,
locations,
} = useLocationsQuery({ consortium: isCentralOrderingEnabled });

const onCancel = useCallback(
() => {
Expand Down Expand Up @@ -136,7 +122,7 @@ function TitleReceiveContainer({ history, location, match }) {
[poLine],
);

const isLoading = isPiecesLoading || isFetching;
const isLoading = isPiecesLoading || isLocationsLoading;

if (isLoading) {
return (
Expand Down
74 changes: 27 additions & 47 deletions src/common/hooks/useHoldingsAndLocations/useHoldingsAndLocations.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,23 @@
import { useQuery } from 'react-query';

import { LIMIT_MAX } from '@folio/stripes-acq-components';
import {
batchRequest,
HOLDINGS_API,
LIMIT_MAX,
LOCATIONS_API,
} from '@folio/stripes-acq-components';
import {
useNamespace,
useOkapiKy,
} from '@folio/stripes/core';

import {
getHoldingLocations,
getHoldingLocationsByTenants,
} from '../../utils/getHoldingLocations';

const DEFAULT_DATA = [];

export const useHoldingsAndLocations = ({
instanceId,
options = {},
tenantId,
/*
`receivingTenantIds` is a unique list of tenantIds from the pieces list.
The purpose is that we need to be able to fetch locations from other
tenants so that we can display all the locations on the full-screen page
*/
receivingTenantIds = DEFAULT_DATA,
/*
ECS mode:
`additionalTenantLocationIdsMap` is a map of tenantId to locationIds for ECS mode.
The format can be: { tenantId: [locationId1, locationId2] }
The purpose is that we need to fetch newly added locations when we select a location
from "Create new holdings for location" modal so that the value is displayed in the selection
*/
additionalTenantLocationIdsMap = {},
/*
Non-ECS mode:
`additionalLocationIds` is a list of locationIds for the case when we need to fetch additional
locations for the selected location in the form so that the value is displayed in the selection.
*/
additionalLocationIds = [],
}) => {
const { enabled = true, ...queryOptions } = options;
export const useHoldingsAndLocations = ({ instanceId, tenantId, options = {} }) => {
const { enabled = true, ...otherOptions } = options;

const ky = useOkapiKy({ tenant: tenantId });
const [namespace] = useNamespace({ key: 'holdings-and-location' });
const queryKey = [
namespace,
tenantId,
instanceId,
...receivingTenantIds,
...additionalLocationIds,
...Object.values(additionalTenantLocationIdsMap),
];
const searchParams = {
query: `instanceId==${instanceId}`,
limit: LIMIT_MAX,
Expand All @@ -60,20 +28,32 @@ export const useHoldingsAndLocations = ({
isFetching,
isLoading,
} = useQuery({
queryKey,
queryFn: ({ signal }) => {
return receivingTenantIds.length
? getHoldingLocationsByTenants({ ky, instanceId, receivingTenantIds, additionalTenantLocationIdsMap })
: getHoldingLocations({ ky, searchParams, signal, additionalLocationIds });
queryKey: [namespace, tenantId, instanceId],
queryFn: async ({ signal }) => {
const holdings = await ky
.get(HOLDINGS_API, { searchParams, signal })
.json()
.then(response => response.holdingsRecords);

const locationIds = holdings?.map(({ permanentLocationId }) => permanentLocationId) || DEFAULT_DATA;

const locations = await batchRequest(
({ params }) => ky.get(LOCATIONS_API, { searchParams: params, signal }).json(),
locationIds,
).then(responses => responses.flatMap(response => response.locations));

return {
holdings,
locations,
};
},
enabled: enabled && Boolean(instanceId),
...queryOptions,
...otherOptions,
});

return ({
holdings: data?.holdings || DEFAULT_DATA,
locations: data?.locations || DEFAULT_DATA,
locationIds: data?.locationIds || DEFAULT_DATA,
isFetching,
isLoading,
});
Expand Down
Loading
Loading