2019-09-02 03:45:56 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { getResourceIdentifier, getResourceName } from '../utils';
|
|
|
|
import { WC_ADMIN_NAMESPACE } from '../constants';
|
|
|
|
|
|
|
|
function read( resourceNames, fetch = apiFetch ) {
|
|
|
|
return [ ...readOptions( resourceNames, fetch ) ];
|
|
|
|
}
|
|
|
|
|
|
|
|
function update( resourceNames, data, fetch = apiFetch ) {
|
|
|
|
return [ ...updateOptions( resourceNames, data, fetch ) ];
|
|
|
|
}
|
|
|
|
|
|
|
|
function readOptions( resourceNames, fetch ) {
|
2020-02-14 02:23:21 +00:00
|
|
|
const filteredNames = resourceNames.filter( ( name ) => {
|
2019-09-02 03:45:56 +00:00
|
|
|
return name.startsWith( 'options' );
|
|
|
|
} );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
return filteredNames.map( async ( resourceName ) => {
|
2019-09-02 03:45:56 +00:00
|
|
|
const optionNames = getResourceIdentifier( resourceName );
|
2020-02-14 02:23:21 +00:00
|
|
|
const url =
|
|
|
|
WC_ADMIN_NAMESPACE + '/options?options=' + optionNames.join( ',' );
|
2019-09-02 03:45:56 +00:00
|
|
|
|
|
|
|
return fetch( { path: url } )
|
|
|
|
.then( optionsToResource )
|
2020-02-14 02:23:21 +00:00
|
|
|
.catch( ( error ) => {
|
2019-09-02 03:45:56 +00:00
|
|
|
return { [ resourceName ]: { error: String( error.message ) } };
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateOptions( resourceNames, data, fetch ) {
|
|
|
|
const url = WC_ADMIN_NAMESPACE + '/options';
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const filteredNames = resourceNames.filter( ( name ) => {
|
2019-09-06 14:18:44 +00:00
|
|
|
return name.startsWith( 'options-update' );
|
2019-09-02 03:45:56 +00:00
|
|
|
} );
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
return filteredNames.map( async ( resourceName ) => {
|
|
|
|
return fetch( {
|
|
|
|
path: url,
|
|
|
|
method: 'POST',
|
|
|
|
data: data[ resourceName ],
|
|
|
|
} )
|
2019-09-06 14:18:44 +00:00
|
|
|
.then( () => optionsToResource( data[ resourceName ], true ) )
|
2020-02-14 02:23:21 +00:00
|
|
|
.catch( ( error ) => {
|
2020-03-16 11:09:29 +00:00
|
|
|
return { [ resourceName ]: { data: {}, error } };
|
2019-09-02 03:45:56 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2019-09-06 14:18:44 +00:00
|
|
|
function optionsToResource( options, updateResource = false ) {
|
2019-09-02 03:45:56 +00:00
|
|
|
const optionNames = Object.keys( options );
|
2019-09-06 14:18:44 +00:00
|
|
|
const resourceName = getResourceName(
|
|
|
|
updateResource ? 'options-update' : 'options',
|
|
|
|
optionNames
|
|
|
|
);
|
2019-09-02 03:45:56 +00:00
|
|
|
const resources = {};
|
|
|
|
|
|
|
|
optionNames.forEach(
|
2020-02-14 02:23:21 +00:00
|
|
|
( optionName ) =>
|
|
|
|
( resources[ getResourceName( 'options', optionName ) ] = {
|
|
|
|
data: options[ optionName ],
|
|
|
|
} )
|
2019-09-02 03:45:56 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
[ resourceName ]: {
|
|
|
|
data: optionNames,
|
|
|
|
},
|
|
|
|
...resources,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
read,
|
|
|
|
update,
|
|
|
|
};
|