Store state settings individually to prevent overwriting (https://github.com/woocommerce/woocommerce-admin/pull/2317)

This commit is contained in:
Joshua T Flowers 2019-05-28 22:23:05 +08:00 committed by GitHub
parent 821408fc83
commit 74d02bc717
2 changed files with 31 additions and 9 deletions

View File

@ -8,6 +8,7 @@ import apiFetch from '@wordpress/api-fetch';
/**
* Internal dependencies
*/
import { getResourceName } from '../utils';
import { NAMESPACE } from '../constants';
function read( resourceNames, fetch = apiFetch ) {
@ -58,22 +59,34 @@ function updateSettings( resourceNames, data, fetch ) {
}
function settingsToSettingsResource( resourceName, settings ) {
const settingsData = {};
settings.forEach( setting => ( settingsData[ setting.id ] = setting.value ) );
return { [ resourceName ]: { data: settingsData } };
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,
};
}
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 ) );
const resources = {};
data.update.forEach(
setting =>
( resources[ getResourceName( resourceName, setting.id ) ] = { data: setting.value } )
);
return { [ resourceName ]: { data: settings } };
return resources;
}
export default {

View File

@ -9,12 +9,21 @@ import { isNil } from 'lodash';
* Internal dependencies
*/
import { DEFAULT_REQUIREMENT } from '../constants';
import { getResourceName } from '../utils';
const getSettings = ( getResource, requireResource ) => (
group,
requirement = DEFAULT_REQUIREMENT
) => {
return requireResource( requirement, `settings/${ group }` ).data || {};
const resourceName = `settings/${ group }`;
const ids = requireResource( requirement, resourceName ).data || [];
const settings = {};
ids.forEach( id => {
settings[ id ] = getResource( getResourceName( resourceName, id ) ).data;
} );
return settings;
};
const getSettingsError = getResource => group => {