Add update method for products to wc-api.
This commit is contained in:
parent
150088d312
commit
68e49a9574
|
@ -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,
|
||||||
};
|
};
|
||||||
|
|
|
@ -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 {
|
export default {
|
||||||
read,
|
read,
|
||||||
|
update,
|
||||||
};
|
};
|
||||||
|
|
|
@ -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 ),
|
|
||||||
];
|
];
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue