Add update method for products to wc-api.

This commit is contained in:
Jeff Stieler 2019-03-28 10:38:44 -07:00
parent 150088d312
commit 68e49a9574
4 changed files with 36 additions and 2 deletions

View File

@ -2,10 +2,12 @@
/** /**
* Internal dependencies * Internal dependencies
*/ */
import mutations from './mutations';
import operations from './operations'; import operations from './operations';
import selectors from './selectors'; import selectors from './selectors';
export default { export default {
mutations,
operations, operations,
selectors, selectors,
}; };

View File

@ -0,0 +1,9 @@
/** @format */
const updateItem = operations => ( itemType, id, itemData ) => {
operations.update( [ itemType ], { [ itemType ]: { id, ...itemData } } );
};
export default {
updateItem,
};

View File

@ -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 { export default {
read, read,
update,
}; };

View File

@ -14,9 +14,10 @@ import user from './user';
function createWcApiSpec() { function createWcApiSpec() {
return { return {
mutations: { mutations: {
...items.mutations,
...notes.mutations,
...settings.mutations, ...settings.mutations,
...user.mutations, ...user.mutations,
...notes.mutations,
}, },
selectors: { selectors: {
...items.selectors, ...items.selectors,
@ -41,9 +42,10 @@ function createWcApiSpec() {
}, },
update( resourceNames, data ) { update( resourceNames, data ) {
return [ return [
...items.operations.update( resourceNames, data ),
...notes.operations.update( resourceNames, data ),
...settings.operations.update( resourceNames, data ), ...settings.operations.update( resourceNames, data ),
...user.operations.update( resourceNames, data ), ...user.operations.update( resourceNames, data ),
...notes.operations.update( resourceNames, data ),
]; ];
}, },
}, },