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
*/
import mutations from './mutations';
import operations from './operations';
import selectors from './selectors';
export default {
mutations,
operations,
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 {
read,
update,
};

View File

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