woocommerce/plugins/woocommerce-admin/client/wc-api/settings/selectors.js

85 lines
2.2 KiB
JavaScript

/**
* External dependencies
*/
import { isNil } from 'lodash';
/**
* WooCommerce dependencies
*/
import { getSetting } from '@woocommerce/wc-admin-settings';
/**
* Internal dependencies
*/
import { DEFAULT_REQUIREMENT } from '../constants';
import { getResourceName } from '../utils';
import { analyticsSettings } from 'analytics/settings/config';
const getSettings = ( getResource, requireResource ) => (
group,
requirement = DEFAULT_REQUIREMENT
) => {
const resourceName = `settings/${ group }`;
const ids = requireResource( requirement, resourceName ).data || [];
const settings = {};
if ( ! ids.length ) {
const preloadSettings = getSetting( 'preloadSettings', {} );
if ( preloadSettings[ group ] ) {
return preloadSettings[ group ];
}
}
ids.forEach( ( id ) => {
settings[ id ] = getSetting(
'preloadSettings',
{},
( preloadSettings ) =>
getResource( getResourceName( resourceName, id ) ).data ||
preloadSettings[ id ]
);
} );
return settings;
};
const getSettingsError = ( getResource ) => ( group ) => {
return getResource( `settings/${ group }` ).error;
};
const isGetSettingsRequesting = ( getResource ) => ( group ) => {
const { lastReceived, lastRequested } = getResource(
`settings/${ group }`
);
if ( isNil( lastRequested ) || isNil( lastReceived ) ) {
return true;
}
// This selector is used by other "groups", so return early if not looking at wc_admin settings.
if ( group !== 'wc_admin' ) {
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;
};
export default {
getSettings,
getSettingsError,
isGetSettingsRequesting,
};