Include item ID in product update resource name.

Keep state in sync when updating product stock quantity.
This commit is contained in:
Jeff Stieler 2019-04-01 16:16:51 -06:00
parent 5d09ceb272
commit f4691827b1
3 changed files with 19 additions and 17 deletions

View File

@ -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 );
} );
}

View File

@ -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 {

View File

@ -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 } };
} );
} );
}