2019-03-28 17:38:44 +00:00
|
|
|
/** @format */
|
2019-06-27 09:21:43 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
|
|
|
import { dispatch } from '@wordpress/data';
|
2019-03-28 17:38:44 +00:00
|
|
|
|
2019-04-01 22:16:51 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { getResourceName } from '../utils';
|
|
|
|
|
2019-06-27 09:21:43 +00:00
|
|
|
const updateProductStock = operations => async ( product, newStock ) => {
|
|
|
|
const { addNotice } = dispatch( 'wc-admin' );
|
|
|
|
const oldStockQuantity = product.stock_quantity;
|
|
|
|
const resourceName = getResourceName( 'items-query-products-item', product.id );
|
|
|
|
|
|
|
|
// Optimistically update product stock
|
|
|
|
operations.updateLocally( [ resourceName ], {
|
|
|
|
[ resourceName ]: { ...product, stock_quantity: newStock },
|
|
|
|
} );
|
|
|
|
|
|
|
|
const result = await operations.update( [ resourceName ], {
|
|
|
|
[ resourceName ]: {
|
|
|
|
id: product.id,
|
|
|
|
type: product.type,
|
|
|
|
parent_id: product.parent_id,
|
|
|
|
stock_quantity: newStock,
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
const response = result[ 0 ][ resourceName ];
|
|
|
|
if ( response && response.data ) {
|
|
|
|
addNotice( {
|
|
|
|
status: 'success',
|
|
|
|
message: sprintf( __( '%s stock updated.', 'woocommerce-admin' ), product.name ),
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
if ( response && response.error ) {
|
|
|
|
addNotice( {
|
|
|
|
status: 'error',
|
|
|
|
message: sprintf( __( '%s stock could not be updated.', 'woocommerce-admin' ), product.name ),
|
|
|
|
} );
|
|
|
|
// Revert local changes if the operation failed in the server
|
|
|
|
operations.updateLocally( [ resourceName ], {
|
|
|
|
[ resourceName ]: { ...product, stock_quantity: oldStockQuantity },
|
|
|
|
} );
|
|
|
|
}
|
2019-03-28 17:38:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
2019-06-27 09:21:43 +00:00
|
|
|
updateProductStock,
|
2019-03-28 17:38:44 +00:00
|
|
|
};
|