diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c0499ed..0f0455ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ * *BREAKING* Upgrade React to v18. Refs UIREC-280. * *BREAKING* bump `react-intl` to `v6.4.4`. Refs UIREC-286. * Bump optional plugins to their `@folio/stripes` `v9` compatible versions. Refs UIREC-290. +* Check if a holding exists during the abandonment check. Refs UIREC-294. ## [3.0.0](https://github.com/folio-org/ui-receiving/tree/v3.0.0) (2023-02-22) [Full Changelog](https://github.com/folio-org/ui-receiving/compare/v2.3.1...v3.0.0) diff --git a/src/TitleDetails/AddPieceModal/AddPieceModal.js b/src/TitleDetails/AddPieceModal/AddPieceModal.js index 11c4dd57..421e0fd6 100644 --- a/src/TitleDetails/AddPieceModal/AddPieceModal.js +++ b/src/TitleDetails/AddPieceModal/AddPieceModal.js @@ -17,6 +17,7 @@ import { TextField, checkScope, } from '@folio/stripes/components'; +import { useOkapiKy } from '@folio/stripes/core'; import stripesFinalForm from '@folio/stripes/final-form'; import { FieldDatepickerFinal, @@ -33,6 +34,7 @@ import { CreateItemField, LineLocationsView, } from '../../common/components'; +import { HOLDINGS_API } from '../../common/constants'; import { DeletePieceModal } from '../DeletePieceModal'; import { DeleteHoldingsModal } from '../DeleteHoldingsModal'; @@ -62,6 +64,7 @@ const AddPieceModal = ({ const [isDeleteConfirmation, toggleDeleteConfirmation] = useModalToggle(); const [isDeleteHoldingsConfirmation, toggleDeleteHoldingsConfirmation] = useModalToggle(); + const ky = useOkapiKy(); const intl = useIntl(); const modalLabel = intl.formatMessage({ id: labelId }); @@ -88,33 +91,42 @@ const AddPieceModal = ({ if (!checked) change('discoverySuppress', checked); }; - const onSave = useCallback((e) => { + const checkHoldingAbandonment = useCallback((holdingId) => { + return ky.get(`${HOLDINGS_API}/${holdingId}`) + .json() + .then((holding) => getHoldingsItemsAndPieces(holding.id, { limit: 1 })) + .then(({ pieces, items }) => { + const willAbandoned = Boolean( + pieces && items + && (pieces.totalRecords === 1) + && ((items.totalRecords === 1 && itemId) || items.totalRecords === 0), + ); + + return { willAbandoned }; + }) + .catch(() => ({ willAbandoned: false })); + }, [getHoldingsItemsAndPieces, itemId, ky]); + + const onSave = useCallback(async (e) => { const holdingId = getState().values?.holdingId; - - if ((id && initialHoldingId) && (holdingId !== initialHoldingId)) { - return getHoldingsItemsAndPieces(initialHoldingId, { limit: 1 }) - .then(({ pieces, items }) => { - const canDeleteHolding = Boolean( - pieces && items - && (pieces.totalRecords === 1) - && ((items.totalRecords === 1 && itemId) || items.totalRecords === 0), - ); - - if (canDeleteHolding) { - return toggleDeleteHoldingsConfirmation(); - } - - return handleSubmit(e); - }); + const shouldCheckHoldingAbandonment = (id && initialHoldingId) && (holdingId !== initialHoldingId); + + if (shouldCheckHoldingAbandonment) { + return checkHoldingAbandonment(initialHoldingId) + .then(({ willAbandoned }) => ( + willAbandoned + ? toggleDeleteHoldingsConfirmation() + : handleSubmit(e) + )); } return handleSubmit(e); - }, []); + }, [checkHoldingAbandonment, getState, handleSubmit, id, initialHoldingId, toggleDeleteHoldingsConfirmation]); const onDeleteHoldings = useCallback(() => { change('deleteHolding', true); handleSubmit(); - }, []); + }, [change, handleSubmit]); const start = (