From f4691827b1d94fb6cb4b7501ebbc86b87bd4a77e Mon Sep 17 00:00:00 2001 From: Jeff Stieler Date: Mon, 1 Apr 2019 16:16:51 -0600 Subject: [PATCH] Include item ID in product update resource name. Keep state in sync when updating product stock quantity. --- .../header/activity-panel/panels/stock/card.js | 10 +++------- .../client/wc-api/items/mutations.js | 10 ++++++++-- .../client/wc-api/items/operations.js | 16 ++++++++-------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/plugins/woocommerce-admin/client/header/activity-panel/panels/stock/card.js b/plugins/woocommerce-admin/client/header/activity-panel/panels/stock/card.js index 1c77541ba16..8fdb158237c 100644 --- a/plugins/woocommerce-admin/client/header/activity-panel/panels/stock/card.js +++ b/plugins/woocommerce-admin/client/header/activity-panel/panels/stock/card.js @@ -51,17 +51,13 @@ class ProductStockCard extends Component { const { product, updateItem } = this.props; this.setState( { editing: false }, () => { - let itemType = 'product'; const data = { stock_quantity: this.state.quantity, + type: product.type, + parent_id: product.parent_id, }; - if ( 'variation' === product.type ) { - itemType = 'variation'; - data.parent_id = product.parent_id; - } - - updateItem( itemType, product.id, data ); + updateItem( 'products', product.id, data ); } ); } diff --git a/plugins/woocommerce-admin/client/wc-api/items/mutations.js b/plugins/woocommerce-admin/client/wc-api/items/mutations.js index df73385cbe8..5ee10dfc95a 100644 --- a/plugins/woocommerce-admin/client/wc-api/items/mutations.js +++ b/plugins/woocommerce-admin/client/wc-api/items/mutations.js @@ -1,7 +1,13 @@ /** @format */ -const updateItem = operations => ( itemType, id, itemData ) => { - operations.update( [ itemType ], { [ itemType ]: { id, ...itemData } } ); +/** + * Internal dependencies + */ +import { getResourceName } from '../utils'; + +const updateItem = operations => ( type, id, itemData ) => { + const resourceName = getResourceName( `items-query-${ type }-item`, id ); + operations.update( [ resourceName ], { [ resourceName ]: { id, ...itemData } } ); }; export default { diff --git a/plugins/woocommerce-admin/client/wc-api/items/operations.js b/plugins/woocommerce-admin/client/wc-api/items/operations.js index 65a39e4874e..ad784c07c98 100644 --- a/plugins/woocommerce-admin/client/wc-api/items/operations.js +++ b/plugins/woocommerce-admin/client/wc-api/items/operations.js @@ -65,31 +65,31 @@ function read( resourceNames, fetch = apiFetch ) { } function update( resourceNames, data, fetch = apiFetch ) { - console.log( 'update', resourceNames, data ); - const updateableTypes = [ 'product', 'variation' ]; + const updateableTypes = [ 'items-query-products-item' ]; const filteredNames = resourceNames.filter( name => { - return updateableTypes.includes( name ); + return updateableTypes.includes( getResourcePrefix( name ) ); } ); return filteredNames.map( async resourceName => { - const { id, parent_id, ...itemData } = data[ resourceName ]; + const { id, parent_id, type, ...itemData } = data[ resourceName ]; let url = NAMESPACE; - switch ( resourceName ) { + switch ( type ) { case 'variation': url += `/products/${ parent_id }/variations/${ id }`; break; - case 'product': + case 'variable': + case 'simple': default: url += `/products/${ id }`; } return fetch( { path: url, method: 'PUT', data: itemData } ) .then( item => { - return { [ resourceName + ':' + id ]: { data: item } }; + return { [ resourceName ]: { data: item } }; } ) .catch( error => { - return { [ resourceName + ':' + id ]: { error } }; + return { [ resourceName ]: { error } }; } ); } ); }