2019-02-13 11:44:58 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { isNil } from 'lodash';
|
|
|
|
|
2019-11-05 00:05:20 +00:00
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
|
|
|
import { getSetting } from '@woocommerce/wc-admin-settings';
|
|
|
|
|
2019-01-31 01:04:11 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { DEFAULT_REQUIREMENT } from '../constants';
|
2019-05-28 14:23:05 +00:00
|
|
|
import { getResourceName } from '../utils';
|
2019-07-22 21:15:41 +00:00
|
|
|
import { analyticsSettings } from 'analytics/settings/config';
|
2019-01-31 01:04:11 +00:00
|
|
|
|
2019-05-24 07:22:21 +00:00
|
|
|
const getSettings = ( getResource, requireResource ) => (
|
|
|
|
group,
|
|
|
|
requirement = DEFAULT_REQUIREMENT
|
|
|
|
) => {
|
2019-05-28 14:23:05 +00:00
|
|
|
const resourceName = `settings/${ group }`;
|
|
|
|
const ids = requireResource( requirement, resourceName ).data || [];
|
|
|
|
const settings = {};
|
|
|
|
|
2019-11-05 00:05:20 +00:00
|
|
|
if ( ! ids.length ) {
|
|
|
|
const preloadSettings = getSetting( 'preloadSettings', {} );
|
|
|
|
if ( preloadSettings[ group ] ) {
|
|
|
|
return preloadSettings[ group ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
ids.forEach( ( id ) => {
|
2019-11-05 00:05:20 +00:00
|
|
|
settings[ id ] = getSetting(
|
|
|
|
'preloadSettings',
|
|
|
|
{},
|
2020-02-14 02:23:21 +00:00
|
|
|
( preloadSettings ) =>
|
|
|
|
getResource( getResourceName( resourceName, id ) ).data ||
|
|
|
|
preloadSettings[ id ]
|
2019-11-05 00:05:20 +00:00
|
|
|
);
|
2019-05-28 14:23:05 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
return settings;
|
2019-02-13 11:44:58 +00:00
|
|
|
};
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const getSettingsError = ( getResource ) => ( group ) => {
|
2019-05-24 07:22:21 +00:00
|
|
|
return getResource( `settings/${ group }` ).error;
|
2019-02-13 11:44:58 +00:00
|
|
|
};
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const isGetSettingsRequesting = ( getResource ) => ( group ) => {
|
|
|
|
const { lastReceived, lastRequested } = getResource(
|
|
|
|
`settings/${ group }`
|
|
|
|
);
|
2019-02-13 11:44:58 +00:00
|
|
|
if ( isNil( lastRequested ) || isNil( lastReceived ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-22 21:15:41 +00:00
|
|
|
// This selector is used by other "groups", so return early if not looking at wc_admin settings.
|
2020-02-14 02:23:21 +00:00
|
|
|
if ( group !== 'wc_admin' ) {
|
2019-07-22 21:15:41 +00:00
|
|
|
return lastRequested > lastReceived;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mutation operations for `wc_admin` settings update a different resource (batch endpoint) in fresh-data.
|
|
|
|
// As such we must use lastReceived stamp from that resource to properly compare lastRequested to lastReceived.
|
|
|
|
const settingName = analyticsSettings.length
|
|
|
|
? analyticsSettings[ 0 ].name
|
|
|
|
: 'woocommerce_actionable_order_statuses';
|
|
|
|
const { lastReceived: lastMutationReceived } = getResource(
|
|
|
|
getResourceName( 'settings/wc_admin', settingName )
|
|
|
|
);
|
|
|
|
|
|
|
|
// If we don't have a lastReceived on mutations, use the standard resource times.
|
|
|
|
if ( isNil( lastMutationReceived ) ) {
|
|
|
|
return lastRequested > lastReceived;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastRequested > lastMutationReceived;
|
2019-01-31 01:04:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
getSettings,
|
2019-02-13 11:44:58 +00:00
|
|
|
getSettingsError,
|
|
|
|
isGetSettingsRequesting,
|
2019-01-31 01:04:11 +00:00
|
|
|
};
|