2019-01-31 01:04:11 +00:00
|
|
|
/** @format */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2019-05-28 14:23:05 +00:00
|
|
|
import { getResourceName } from '../utils';
|
2019-01-31 01:04:11 +00:00
|
|
|
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-05-28 14:23:05 +00:00
|
|
|
const resources = {};
|
|
|
|
|
|
|
|
const settingIds = settings.map( setting => setting.id );
|
|
|
|
settings.forEach(
|
|
|
|
setting =>
|
|
|
|
( resources[ getResourceName( resourceName, setting.id ) ] = { data: setting.value } )
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
[ resourceName ]: {
|
|
|
|
data: settingIds,
|
|
|
|
},
|
|
|
|
...resources,
|
|
|
|
};
|
2019-01-31 01:04:11 +00:00
|
|
|
}
|
|
|
|
|
2019-05-24 07:22:21 +00:00
|
|
|
function settingToSettingsResource( resourceName, data ) {
|
|
|
|
if ( 'undefined' === typeof data.update ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2019-08-21 05:58:47 +00:00
|
|
|
// Override lastReceived time for group when batch updating.
|
|
|
|
const resources = { [ resourceName ]: { lastReceived: Date.now() } };
|
2019-05-28 14:23:05 +00:00
|
|
|
data.update.forEach(
|
|
|
|
setting =>
|
|
|
|
( resources[ getResourceName( resourceName, setting.id ) ] = { data: setting.value } )
|
|
|
|
);
|
2019-05-24 07:22:21 +00:00
|
|
|
|
2019-05-28 14:23:05 +00:00
|
|
|
return resources;
|
2019-02-13 11:44:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 01:04:11 +00:00
|
|
|
export default {
|
|
|
|
read,
|
|
|
|
update,
|
|
|
|
};
|