Fix updating product that has no variants (#649)

* Fix updating product that has no variants

* Update changelog
This commit is contained in:
Dominik Żegleń 2020-08-18 16:09:01 +02:00 committed by GitHub
parent 355c796bd8
commit cc2fb0ae4e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 16 deletions

View file

@ -22,6 +22,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Reset modal state after closing - #644 by @dominik-zeglen
- Fix incorrect messages - #643 by @dominik-zeglen
- Do not use devserver to run cypress tests - #650 by @dominik-zeglen
- Fix updating product that has no variants - #649 by @dominik-zeglen
## 2.10.1

View file

@ -167,23 +167,33 @@ export const ProductUpdatePage: React.FC<ProductUpdatePageProps> = ({
const hasVariants = maybe(() => product.productType.hasVariants, false);
const handleSubmit = (data: ProductUpdatePageFormData) => {
const dataStocks = stocks.map(stock => stock.id);
const variantStocks = product.variants[0].stocks.map(
stock => stock.warehouse.id
);
const stockDiff = diff(variantStocks, dataStocks);
if (product.productType.hasVariants) {
onSubmit({
...data,
addStocks: [],
attributes,
removeStocks: [],
updateStocks: []
});
} else {
const dataStocks = stocks.map(stock => stock.id);
const variantStocks = product.variants[0]?.stocks.map(
stock => stock.warehouse.id
);
const stockDiff = diff(variantStocks, dataStocks);
onSubmit({
...data,
addStocks: stocks.filter(stock =>
stockDiff.added.some(addedStock => addedStock === stock.id)
),
attributes,
removeStocks: stockDiff.removed,
updateStocks: stocks.filter(
stock => !stockDiff.added.some(addedStock => addedStock === stock.id)
)
});
onSubmit({
...data,
addStocks: stocks.filter(stock =>
stockDiff.added.some(addedStock => addedStock === stock.id)
),
attributes,
removeStocks: stockDiff.removed,
updateStocks: stocks.filter(
stock => !stockDiff.added.some(addedStock => addedStock === stock.id)
)
});
}
};
return (