2019-09-02 03:45:56 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { isNil } from 'lodash';
|
|
|
|
|
2019-10-07 11:51:25 +00:00
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
|
|
|
import { getSetting } from '@woocommerce/wc-admin-settings';
|
|
|
|
|
2019-09-02 03:45:56 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { DEFAULT_REQUIREMENT } from '../constants';
|
|
|
|
import { getResourceName } from '../utils';
|
|
|
|
|
|
|
|
const getOptions = ( getResource, requireResource ) => (
|
|
|
|
optionNames,
|
|
|
|
requirement = DEFAULT_REQUIREMENT
|
|
|
|
) => {
|
|
|
|
const resourceName = getResourceName( 'options', optionNames );
|
|
|
|
const options = {};
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const names =
|
|
|
|
requireResource( requirement, resourceName ).data || optionNames;
|
2019-09-02 03:45:56 +00:00
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
names.forEach( ( name ) => {
|
2019-10-07 11:51:25 +00:00
|
|
|
const data = getSetting(
|
|
|
|
'preloadOptions',
|
|
|
|
{},
|
2020-02-14 02:23:21 +00:00
|
|
|
( po ) =>
|
|
|
|
getResource( getResourceName( 'options', name ) ).data ||
|
|
|
|
po[ name ]
|
2019-10-07 11:51:25 +00:00
|
|
|
);
|
2019-10-03 16:03:29 +00:00
|
|
|
if ( data ) {
|
|
|
|
options[ name ] = data;
|
|
|
|
}
|
2019-09-02 03:45:56 +00:00
|
|
|
} );
|
|
|
|
return options;
|
|
|
|
};
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const getOptionsError = ( getResource ) => ( optionNames ) => {
|
2019-09-02 03:45:56 +00:00
|
|
|
return getResource( getResourceName( 'options', optionNames ) ).error;
|
|
|
|
};
|
|
|
|
|
2020-03-16 11:09:29 +00:00
|
|
|
const getUpdateOptionsError = ( getResource ) => ( optionNames ) => {
|
|
|
|
return getResource( getResourceName( 'options-update', optionNames ) )
|
|
|
|
.error;
|
|
|
|
};
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const isGetOptionsRequesting = ( getResource ) => ( optionNames ) => {
|
|
|
|
const { lastReceived, lastRequested } = getResource(
|
|
|
|
getResourceName( 'options', optionNames )
|
|
|
|
);
|
2019-09-02 03:45:56 +00:00
|
|
|
|
|
|
|
if ( ! isNil( lastRequested ) && isNil( lastReceived ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastRequested > lastReceived;
|
|
|
|
};
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const isUpdateOptionsRequesting = ( getResource ) => ( optionNames ) => {
|
2019-09-06 14:18:44 +00:00
|
|
|
const { lastReceived, lastRequested } = getResource(
|
|
|
|
getResourceName( 'options-update', optionNames )
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( ! isNil( lastRequested ) && isNil( lastReceived ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastRequested > lastReceived;
|
|
|
|
};
|
|
|
|
|
2019-09-02 03:45:56 +00:00
|
|
|
export default {
|
|
|
|
getOptions,
|
|
|
|
getOptionsError,
|
2020-03-16 11:09:29 +00:00
|
|
|
getUpdateOptionsError,
|
2019-09-06 14:18:44 +00:00
|
|
|
isGetOptionsRequesting,
|
|
|
|
isUpdateOptionsRequesting,
|
2019-09-02 03:45:56 +00:00
|
|
|
};
|