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-292: fix update item status on unreceive piece #484

Merged
merged 12 commits into from
Jan 30, 2024
39 changes: 33 additions & 6 deletions src/TitleDetails/AddPieceModal/AddPieceModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ import {
PIECE_STATUS,
handleKeyCommand,
useModalToggle,
useShowCallout,
} from '@folio/stripes-acq-components';

import { HOLDINGS_API } from '../../common/constants';
import { getClaimingIntervalFromDate } from '../../common/utils';
import {
getClaimingIntervalFromDate,
unreceivePieces,
} from '../../common/utils';
import {
PIECE_MODAL_ACCORDION,
PIECE_MODAL_ACCORDION_LABELS,
Expand All @@ -51,23 +55,24 @@ import { PieceFields } from './PieceFields';
import { ReceivingStatusChangeLog } from './ReceivingStatusChangeLog';

const AddPieceModal = ({
canDeletePiece,
close,
createInventoryValues,
deletePiece,
canDeletePiece,
form,
initialValues,
getHoldingsItemsAndPieces,
handleSubmit,
hasValidationErrors,
pristine,
initialValues,
instanceId,
locationIds,
locations,
mutator,
alisher-epam marked this conversation as resolved.
Show resolved Hide resolved
onCheckIn,
pieceFormatOptions,
values: formValues,
poLine,
getHoldingsItemsAndPieces,
pristine,
values: formValues,
}) => {
const {
batch,
Expand Down Expand Up @@ -107,6 +112,7 @@ const AddPieceModal = ({
const intl = useIntl();
const accordionStatusRef = useRef();
const modalLabel = intl.formatMessage({ id: labelId });
const showCallout = useShowCallout();

const initialHoldingId = useMemo(() => getState().initialValues?.holdingId, [getState]);

Expand Down Expand Up @@ -179,6 +185,25 @@ const AddPieceModal = ({
onSave();
}, [change, onSave]);

const onUnreceivePiece = useCallback(() => {
const currentPiece = {
...formValues,
checked: true,
};

return unreceivePieces([currentPiece], mutator.unreceive)
.then(() => {
onStatusChange(PIECE_STATUS.expected);
Copy link
Contributor

Choose a reason for hiding this comment

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

If I'm not mistaken, calling "unreceive" API updates the piece status and you don't need to do it again.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let me check it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Screen.Recording.2024-01-29.at.13.31.27.mov

})
.catch(() => {
showCallout({
type: 'error',
messageId: 'ui-receiving.title.actions.unreceive.error',
});
});
},
[formValues, mutator, onStatusChange, showCallout]);

const onClaimDelay = useCallback(({ claimingDate }) => {
change('claimingInterval', getClaimingIntervalFromDate(claimingDate));
onStatusChange(PIECE_STATUS.claimDelayed);
Expand Down Expand Up @@ -219,6 +244,7 @@ const AddPieceModal = ({
onClaimSend={toggleClaimSendModal}
onDelete={toggleDeleteConfirmation}
onReceive={onReceive}
onUnreceivePiece={onUnreceivePiece}
onSave={onSave}
onStatusChange={onStatusChange}
status={receivingStatus}
Expand Down Expand Up @@ -384,6 +410,7 @@ AddPieceModal.propTypes = {
getHoldingsItemsAndPieces: PropTypes.func.isRequired,
initialValues: PropTypes.object.isRequired,
pristine: PropTypes.bool.isRequired,
mutator: PropTypes.object.isRequired,
};

AddPieceModal.defaultProps = {
Expand Down
40 changes: 40 additions & 0 deletions src/TitleDetails/AddPieceModal/AddPieceModal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ jest.mock('@folio/stripes-acq-components', () => {
});
jest.mock('../../common/components/LineLocationsView/LineLocationsView',
() => jest.fn().mockReturnValue('LineLocationsView'));
jest.mock('../../common/utils', () => ({
...jest.requireActual('../../common/utils'),
unreceivePieces: jest.fn(() => Promise.resolve()),
}));
jest.mock('../hooks', () => ({
...jest.requireActual('../hooks'),
usePieceStatusChangeLog: jest.fn(),
Expand Down Expand Up @@ -262,6 +266,42 @@ describe('AddPieceModal', () => {
expect(defaultProps.onSubmit).toHaveBeenCalled();
});

it('should unreceive piece', async () => {
const onChange = jest.fn();

renderAddPieceModal({
form: {
...defaultProps.form,
change: onChange,
},
hasValidationErrors: false,
initialValues: {
'id': 'cd3fd1e7-c195-4d8e-af75-525e1039d643',
'format': 'Other',
'poLineId': 'a92ae36c-e093-4daf-b234-b4c6dc33a258',
'titleId': '03329fea-1b5d-43ab-b955-20bcd9ba530d',
'holdingId': '60c67dc5-b646-425e-bf08-a8bf2d0681fb',
'isCreateAnother': false,
'isCreateItem': false,
receivingStatus: PIECE_STATUS.received,
receivedDate: new Date().toISOString(),
},
mutator: {
unreceive: {
POST: jest.fn(),
},
},
});

await user.click(screen.getByTestId('dropdown-trigger-button'));
const unReceiveButton = await screen.findByTestId('unReceive-piece-button');

expect(unReceiveButton).toBeInTheDocument();
await user.click(unReceiveButton);

expect(defaultProps.onSubmit).toHaveBeenCalled();
});

describe('Actions', () => {
const initialValues = {
format: PIECE_FORMAT.other,
Expand Down
3 changes: 3 additions & 0 deletions src/TitleDetails/AddPieceModal/AddPieceModalContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const AddPieceModalContainer = ({
onSubmit,
poLine,
getHoldingsItemsAndPieces,
mutator,
}) => {
const createInventoryValues = useMemo(
() => ({
Expand Down Expand Up @@ -72,6 +73,7 @@ const AddPieceModalContainer = ({
pieceFormatOptions={pieceFormatOptions}
poLine={poLine}
getHoldingsItemsAndPieces={getHoldingsItemsAndPieces}
mutator={mutator}
/>
);
};
Expand All @@ -88,6 +90,7 @@ AddPieceModalContainer.propTypes = {
onSubmit: PropTypes.func.isRequired,
poLine: PropTypes.object.isRequired,
getHoldingsItemsAndPieces: PropTypes.func.isRequired,
mutator: PropTypes.object.isRequired,
};

export default AddPieceModalContainer;
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const ModalActionButtons = ({
onReceive,
onSave,
onStatusChange,
onUnreceivePiece,
status,
}) => {
const actionMenu = getPieceActionMenu({
Expand All @@ -37,6 +38,7 @@ export const ModalActionButtons = ({
onDelete,
onReceive,
onStatusChange,
onUnreceivePiece,
status,
});
const saveButtonLabelId = 'ui-receiving.piece.actions.saveAndClose';
Expand Down Expand Up @@ -95,6 +97,7 @@ ModalActionButtons.propTypes = {
onReceive: PropTypes.func.isRequired,
onSave: PropTypes.func.isRequired,
onStatusChange: PropTypes.func.isRequired,
onUnreceivePiece: PropTypes.func.isRequired,
status: PropTypes.string,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const PIECE_ACTIONS = ({
onStatusChange,
onDelete,
onReceive,
onUnreceivePiece,
}) => ({
delayClaim: (
<Button
Expand Down Expand Up @@ -131,7 +132,7 @@ export const PIECE_ACTIONS = ({
disabled={actionsDisabled[PIECE_ACTION_NAMES.unReceive]}
buttonStyle="dropdownItem"
data-testid="unReceive-piece-button"
onClick={() => onStatusChange(PIECE_STATUS.expected)}
onClick={onUnreceivePiece}
>
<Icon icon="cancel">
<FormattedMessage id="ui-receiving.piece.action.button.unReceive" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ describe('getPieceActionMenus', () => {
});

describe('unReceive action', () => {
it('should `onStatusChange` be called with `Expected` status value', () => {
const onStatusChange = jest.fn();
const result = getPieceActionMenu({ status: received, onStatusChange });
it('should `onUnreceivePiece` be called with `Expected` status value', () => {
const onUnreceivePiece = jest.fn();
const result = getPieceActionMenu({ status: received, onUnreceivePiece });
const receiveButton = result.find(i => i.props['data-testid'] === 'unReceive-piece-button');

receiveButton.props.onClick();

expect(onStatusChange).toHaveBeenCalledWith(PIECE_STATUS.expected);
expect(onUnreceivePiece).toHaveBeenCalledWith();
});
});

Expand Down
3 changes: 3 additions & 0 deletions src/TitleDetails/TitleDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ const TitleDetails = ({
vendorsMap,
getHoldingsItemsAndPieces,
getPieceValues,
mutator,
}) => {
const intl = useIntl();
const stripes = useStripes();
Expand Down Expand Up @@ -601,6 +602,7 @@ const TitleDetails = ({
onCheckIn={onQuickReceive}
onSubmit={onSave}
poLine={poLine}
mutator={mutator}
getHoldingsItemsAndPieces={getHoldingsItemsAndPieces}
/>
)}
Expand Down Expand Up @@ -638,6 +640,7 @@ TitleDetails.propTypes = {
vendorsMap: PropTypes.object.isRequired,
getHoldingsItemsAndPieces: PropTypes.func.isRequired,
getPieceValues: PropTypes.func.isRequired,
mutator: PropTypes.object.isRequired,
};

export default withRouter(TitleDetails);
3 changes: 3 additions & 0 deletions src/TitleDetails/TitleDetailsContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
} from '@folio/stripes-acq-components';

import {
receivingResource,
titleResource,
} from '../common/resources';
import {
Expand Down Expand Up @@ -106,7 +107,7 @@
items: itemsInHolding,
}))
.catch(() => ({}));
}, []);

Check warning on line 110 in src/TitleDetails/TitleDetailsContainer.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

React Hook useCallback has missing dependencies: 'mutator.items' and 'mutator.pieces'. Either include them or remove the dependency array

Check warning on line 110 in src/TitleDetails/TitleDetailsContainer.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

React Hook useCallback has missing dependencies: 'mutator.items' and 'mutator.pieces'. Either include them or remove the dependency array

useEffect(
() => {
Expand Down Expand Up @@ -293,6 +294,7 @@
vendorsMap={vendorsMap}
getHoldingsItemsAndPieces={getHoldingsItemsAndPieces}
getPieceValues={getPieceById(mutator.orderPieces)}
mutator={mutator}
/>
);
};
Expand Down Expand Up @@ -320,6 +322,7 @@
...locationsManifest,
fetch: false,
},
unreceive: receivingResource,
vendors: {
...organizationsManifest,
fetch: false,
Expand Down
Loading