2019-09-02 03:45:56 +00:00
|
|
|
/** @format */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { isNil } from 'lodash';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 = {};
|
|
|
|
|
|
|
|
const names = requireResource( requirement, resourceName ).data || [];
|
|
|
|
|
|
|
|
names.forEach( name => {
|
|
|
|
options[ name ] = getResource( getResourceName( 'options', name ) ).data;
|
|
|
|
} );
|
|
|
|
|
|
|
|
return options;
|
|
|
|
};
|
|
|
|
|
|
|
|
const getOptionsError = getResource => optionNames => {
|
|
|
|
return getResource( getResourceName( 'options', optionNames ) ).error;
|
|
|
|
};
|
|
|
|
|
2019-09-06 14:18:44 +00:00
|
|
|
const isGetOptionsRequesting = getResource => optionNames => {
|
2019-09-02 03:45:56 +00:00
|
|
|
const { lastReceived, lastRequested } = getResource( getResourceName( 'options', optionNames ) );
|
|
|
|
|
|
|
|
if ( ! isNil( lastRequested ) && isNil( lastReceived ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastRequested > lastReceived;
|
|
|
|
};
|
|
|
|
|
2019-09-06 14:18:44 +00:00
|
|
|
const isUpdateOptionsRequesting = getResource => optionNames => {
|
|
|
|
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,
|
2019-09-06 14:18:44 +00:00
|
|
|
isGetOptionsRequesting,
|
|
|
|
isUpdateOptionsRequesting,
|
2019-09-02 03:45:56 +00:00
|
|
|
};
|