2019-01-31 01:04:11 +00:00
|
|
|
/** @format */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { NAMESPACE } from '../constants';
|
|
|
|
|
|
|
|
function read( resourceNames, fetch = apiFetch ) {
|
|
|
|
return [ ...readSettings( resourceNames, fetch ) ];
|
|
|
|
}
|
|
|
|
|
|
|
|
function update( resourceNames, data, fetch = apiFetch ) {
|
|
|
|
return [ ...updateSettings( resourceNames, data, fetch ) ];
|
|
|
|
}
|
|
|
|
|
|
|
|
function readSettings( resourceNames, fetch ) {
|
2019-05-24 07:22:21 +00:00
|
|
|
const filteredNames = resourceNames.filter( name => {
|
|
|
|
return name.startsWith( 'settings/' );
|
|
|
|
} );
|
2019-01-31 01:04:11 +00:00
|
|
|
|
2019-05-24 07:22:21 +00:00
|
|
|
return filteredNames.map( async resourceName => {
|
|
|
|
const url = NAMESPACE + '/' + resourceName;
|
|
|
|
|
|
|
|
return fetch( { path: url } )
|
|
|
|
.then( settingsToSettingsResource.bind( null, resourceName ) )
|
|
|
|
.catch( error => {
|
|
|
|
return { [ resourceName ]: { error: String( error.message ) } };
|
|
|
|
} );
|
|
|
|
} );
|
2019-01-31 01:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateSettings( resourceNames, data, fetch ) {
|
2019-05-24 07:22:21 +00:00
|
|
|
const filteredNames = resourceNames.filter( name => {
|
|
|
|
return name.startsWith( 'settings/' );
|
|
|
|
} );
|
2019-01-31 01:04:11 +00:00
|
|
|
|
2019-05-24 07:22:21 +00:00
|
|
|
return filteredNames.map( async resourceName => {
|
|
|
|
const url = NAMESPACE + '/' + resourceName + '/batch';
|
|
|
|
const settingsData = Object.keys( data[ resourceName ] ).map( key => {
|
|
|
|
return { id: key, value: data[ resourceName ][ key ] };
|
2019-01-31 01:04:11 +00:00
|
|
|
} );
|
|
|
|
|
2019-05-24 07:22:21 +00:00
|
|
|
return fetch( {
|
|
|
|
path: url,
|
|
|
|
method: 'POST',
|
|
|
|
data: { update: settingsData },
|
|
|
|
} )
|
|
|
|
.then( settingToSettingsResource.bind( null, resourceName ) )
|
|
|
|
.catch( error => {
|
|
|
|
return { [ resourceName ]: { error } };
|
|
|
|
} );
|
|
|
|
} );
|
2019-01-31 01:04:11 +00:00
|
|
|
}
|
|
|
|
|
2019-05-24 07:22:21 +00:00
|
|
|
function settingsToSettingsResource( resourceName, settings ) {
|
2019-01-31 01:04:11 +00:00
|
|
|
const settingsData = {};
|
|
|
|
settings.forEach( setting => ( settingsData[ setting.id ] = setting.value ) );
|
2019-05-24 07:22:21 +00:00
|
|
|
return { [ resourceName ]: { data: settingsData } };
|
2019-01-31 01:04:11 +00:00
|
|
|
}
|
|
|
|
|
2019-05-24 07:22:21 +00:00
|
|
|
function settingToSettingsResource( resourceName, data ) {
|
|
|
|
const settings = {};
|
|
|
|
if ( 'undefined' === typeof data.update ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// @todo This will only return updated fields so fields
|
|
|
|
// not updated may be temporarily overwritten in the store.
|
|
|
|
data.update.forEach( setting => ( settings[ setting.id ] = setting.value ) );
|
|
|
|
|
|
|
|
return { [ resourceName ]: { data: settings } };
|
2019-02-13 11:44:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 01:04:11 +00:00
|
|
|
export default {
|
|
|
|
read,
|
|
|
|
update,
|
|
|
|
};
|