Skip to content

Commit 4367bd1

Browse files
authored
OBPIH-6267 Do not select edited lines to receive automatically in par… (#4900)
1 parent f47a614 commit 4367bd1

1 file changed

Lines changed: 104 additions & 3 deletions

File tree

src/js/components/receiving/PartialReceivingPage.jsx

Lines changed: 104 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -750,33 +750,134 @@ class PartialReceivingPage extends Component {
750750
}));
751751
}
752752

753+
static mapShipmentItems(shipmentItems, rowIndex, editedLines) {
754+
// Group items for: before edited row, the edited row's original item, items after edited row
755+
// To be able to append a new item just underneath (or replace) the original item
756+
const shipmentItemsGrouped = shipmentItems.reduce((acc, item, idx) => {
757+
if (idx < rowIndex) {
758+
return {
759+
...acc,
760+
itemsBeforeCurrentRow: [...acc.itemsBeforeCurrentRow, item],
761+
};
762+
}
763+
if (idx === rowIndex) {
764+
return {
765+
...acc,
766+
originalItem: item,
767+
};
768+
}
769+
return {
770+
...acc,
771+
itemsAfterCurrentRow: [...acc.itemsAfterCurrentRow, item],
772+
};
773+
}, { itemsBeforeCurrentRow: [], originalItem: null, itemsAfterCurrentRow: [] });
774+
775+
/** If an original item is already persisted,
776+
we don't want to replace it with a new item, but we want to keep the original item
777+
This is the case if you pick one of the items,
778+
go to check step, go back, edit the line and split it
779+
(we want to keep the persisted item + add a new one underneath)
780+
To keep the order (originalItem and then the new item),
781+
we just add the original item at the end of the items before the current row
782+
*/
783+
if (shipmentItemsGrouped.originalItem?.receiptItemId) {
784+
shipmentItemsGrouped.itemsBeforeCurrentRow.push(shipmentItemsGrouped.originalItem);
785+
}
786+
787+
/** Since the edited items are not immedietaly sent to the API,
788+
and a few fields are not calculated and returned by the API, we have to set them statically
789+
*/
790+
const editedLinesWithQuantities = editedLines.map((item) => ({
791+
...item,
792+
quantityRemaining: item.quantityShipped,
793+
unitOfMeasure: shipmentItemsGrouped.originalItem?.unitOfMeasure,
794+
}));
795+
796+
/**
797+
* Returned merged lists
798+
* (items before current row that might include the original item,
799+
* edited items (new items),
800+
* items that were displayed under the edited item)
801+
*/
802+
803+
return [
804+
...shipmentItemsGrouped.itemsBeforeCurrentRow,
805+
...editedLinesWithQuantities,
806+
...shipmentItemsGrouped.itemsAfterCurrentRow,
807+
];
808+
}
809+
810+
static mapContainers(containers, parentIndex, rowIndex, items) {
811+
return containers.map((container, idx) => {
812+
if (idx === parentIndex) {
813+
const { shipmentItems } = container;
814+
return {
815+
...container,
816+
shipmentItems: PartialReceivingPage.mapShipmentItems(shipmentItems, rowIndex, items),
817+
};
818+
}
819+
return container;
820+
});
821+
}
822+
753823
/*
754824
* Saves changes made in edit line modal and updates data.
755825
* @param {object} editLines
756826
* @param {number} parentIndex
757827
* @public
758828
*/
759829
saveEditLine(editLines, parentIndex, formValues, rowIndex) {
830+
const { containers } = this.state.values;
831+
const editLinesGrouped = editLines.reduce((acc, line) => {
832+
if (line.receiptItemId) {
833+
return {
834+
...acc,
835+
itemsToSave: [...acc.itemsToSave, line],
836+
};
837+
}
838+
return {
839+
...acc,
840+
newItems: [...acc.newItems, line],
841+
};
842+
}, { itemsToSave: [], newItems: [] });
843+
if (!editLinesGrouped.itemsToSave.length) {
844+
const { newItems } = editLinesGrouped;
845+
const mappedContainers =
846+
PartialReceivingPage.mapContainers(containers, parentIndex, rowIndex, newItems);
847+
this.setState((prevState) =>
848+
({ values: { ...prevState.values, containers: mappedContainers } }));
849+
return;
850+
}
760851
this.props.showSpinner();
761852

762853
const editedLinesToSave = {
763854
...this.state.values,
764-
containers: [{ ...this.state.values.containers[parentIndex], shipmentItems: editLines }],
855+
containers: [
856+
{
857+
...this.state.values.containers[parentIndex],
858+
shipmentItems: editLinesGrouped.itemsToSave,
859+
},
860+
],
765861
};
766862

767863
this.saveValues(editedLinesToSave)
768864
.then((response) => {
769-
const containers = this.rewriteQuantitiesAfterSave({
865+
const updatedContainersAfterSave = this.rewriteQuantitiesAfterSave({
770866
formValues,
771867
editLines,
772868
parentIndex,
773869
fetchedContainers: response.data.data.containers,
774870
editLinesIndex: rowIndex,
775871
});
872+
const mappedContainers =
873+
PartialReceivingPage.mapContainers(updatedContainersAfterSave,
874+
parentIndex,
875+
rowIndex,
876+
editLinesGrouped.newItems);
776877
this.setState({
777878
values: parseResponse({
778879
...response.data.data,
779-
containers,
880+
containers: mappedContainers,
780881
}),
781882
});
782883
})

0 commit comments

Comments
 (0)