From 6e8351e9a870a0235b8c941cc108b1a4a82f21b1 Mon Sep 17 00:00:00 2001 From: Michael Kelly Date: Sat, 20 Oct 2018 21:15:39 -0700 Subject: [PATCH] #145: Delete related prices when deleting a product. --- src/state/prices.js | 18 +++++++++++++++++- src/state/products.js | 2 +- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/state/prices.js b/src/state/prices.js index ce0af89..9cc0c68 100644 --- a/src/state/prices.js +++ b/src/state/prices.js @@ -14,7 +14,12 @@ import orderBy from 'lodash.orderby'; import pt from 'prop-types'; import config from 'commerce/config'; -import {ADD_PRODUCT, getProductIdFromExtracted} from 'commerce/state/products'; +import { + ADD_PRODUCT, + getProduct, + getProductIdFromExtracted, + REMOVE_MARKED_PRODUCTS, +} from 'commerce/state/products'; // Types @@ -125,6 +130,17 @@ export default function reducer(state = initialState(), action) { }; } + // Delete related prices when deleting products. + case REMOVE_MARKED_PRODUCTS: { + return { + ...state, + prices: state.prices.filter((price) => { + const product = getProduct(state, price.productId); + return product && !product.isDeleted; + }), + }; + } + case ADD_PRICE_ALERT: { // New alerts are active by default. const newAlert = { diff --git a/src/state/products.js b/src/state/products.js index 221876b..155840b 100644 --- a/src/state/products.js +++ b/src/state/products.js @@ -46,7 +46,7 @@ export const extractedProductShape = pt.shape({ export const ADD_PRODUCT = 'commerce/products/ADD_PRODUCT'; // Used by price duck const SET_DELETION_FLAG = 'commerce/products/SET_DELETION_FLAG'; -const REMOVE_MARKED_PRODUCTS = 'commerce/products/REMOVE_MARKED_PRODUCTS'; +export const REMOVE_MARKED_PRODUCTS = 'commerce/products/REMOVE_MARKED_PRODUCTS'; // Reducer