From 68e49a9574980206040b36d41557102801414648 Mon Sep 17 00:00:00 2001 From: Jeff Stieler Date: Thu, 28 Mar 2019 10:38:44 -0700 Subject: [PATCH] Add update method for products to wc-api. --- .../client/wc-api/items/index.js | 2 ++ .../client/wc-api/items/mutations.js | 9 ++++++++ .../client/wc-api/items/operations.js | 21 +++++++++++++++++++ .../client/wc-api/wc-api-spec.js | 6 ++++-- 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 plugins/woocommerce-admin/client/wc-api/items/mutations.js diff --git a/plugins/woocommerce-admin/client/wc-api/items/index.js b/plugins/woocommerce-admin/client/wc-api/items/index.js index cd0cff2f807..3cc7e47390d 100644 --- a/plugins/woocommerce-admin/client/wc-api/items/index.js +++ b/plugins/woocommerce-admin/client/wc-api/items/index.js @@ -2,10 +2,12 @@ /** * Internal dependencies */ +import mutations from './mutations'; import operations from './operations'; import selectors from './selectors'; export default { + mutations, operations, selectors, }; diff --git a/plugins/woocommerce-admin/client/wc-api/items/mutations.js b/plugins/woocommerce-admin/client/wc-api/items/mutations.js new file mode 100644 index 00000000000..df73385cbe8 --- /dev/null +++ b/plugins/woocommerce-admin/client/wc-api/items/mutations.js @@ -0,0 +1,9 @@ +/** @format */ + +const updateItem = operations => ( itemType, id, itemData ) => { + operations.update( [ itemType ], { [ itemType ]: { id, ...itemData } } ); +}; + +export default { + updateItem, +}; diff --git a/plugins/woocommerce-admin/client/wc-api/items/operations.js b/plugins/woocommerce-admin/client/wc-api/items/operations.js index 862c5c73e82..c6cf63360df 100644 --- a/plugins/woocommerce-admin/client/wc-api/items/operations.js +++ b/plugins/woocommerce-admin/client/wc-api/items/operations.js @@ -64,6 +64,27 @@ function read( resourceNames, fetch = apiFetch ) { } ); } +function update( resourceNames, data, fetch = apiFetch ) { + const updateableTypes = [ 'products' ]; + const filteredNames = resourceNames.filter( name => { + return updateableTypes.includes( name ); + } ); + + return filteredNames.map( async resourceName => { + const { id, ...itemData } = data[ resourceName ]; + const url = NAMESPACE + `/${ resourceName }/${ id }`; + + return fetch( { path: url, method: 'PUT', data: itemData } ) + .then( item => { + return { [ resourceName + ':' + id ]: { data: item } }; + } ) + .catch( error => { + return { [ resourceName + ':' + id ]: { error } }; + } ); + } ); +} + export default { read, + update, }; diff --git a/plugins/woocommerce-admin/client/wc-api/wc-api-spec.js b/plugins/woocommerce-admin/client/wc-api/wc-api-spec.js index 6e559cb4288..c0e4d5bcc4d 100644 --- a/plugins/woocommerce-admin/client/wc-api/wc-api-spec.js +++ b/plugins/woocommerce-admin/client/wc-api/wc-api-spec.js @@ -14,9 +14,10 @@ import user from './user'; function createWcApiSpec() { return { mutations: { + ...items.mutations, + ...notes.mutations, ...settings.mutations, ...user.mutations, - ...notes.mutations, }, selectors: { ...items.selectors, @@ -41,9 +42,10 @@ function createWcApiSpec() { }, update( resourceNames, data ) { return [ + ...items.operations.update( resourceNames, data ), + ...notes.operations.update( resourceNames, data ), ...settings.operations.update( resourceNames, data ), ...user.operations.update( resourceNames, data ), - ...notes.operations.update( resourceNames, data ), ]; }, },