Add update method for products to wc-api.
This commit is contained in:
parent
150088d312
commit
68e49a9574
|
@ -2,10 +2,12 @@
|
|||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import mutations from './mutations';
|
||||
import operations from './operations';
|
||||
import selectors from './selectors';
|
||||
|
||||
export default {
|
||||
mutations,
|
||||
operations,
|
||||
selectors,
|
||||
};
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
/** @format */
|
||||
|
||||
const updateItem = operations => ( itemType, id, itemData ) => {
|
||||
operations.update( [ itemType ], { [ itemType ]: { id, ...itemData } } );
|
||||
};
|
||||
|
||||
export default {
|
||||
updateItem,
|
||||
};
|
|
@ -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,
|
||||
};
|
||||
|
|
|
@ -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 ),
|
||||
];
|
||||
},
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue