2019-01-31 01:04:11 +00:00
|
|
|
/** @format */
|
|
|
|
|
2019-02-13 11:44:58 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { isNil } from 'lodash';
|
|
|
|
|
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-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 = {};
|
|
|
|
|
|
|
|
ids.forEach( id => {
|
|
|
|
settings[ id ] = getResource( getResourceName( resourceName, id ) ).data;
|
|
|
|
} );
|
|
|
|
|
|
|
|
return settings;
|
2019-02-13 11:44:58 +00:00
|
|
|
};
|
|
|
|
|
2019-05-24 07:22:21 +00:00
|
|
|
const getSettingsError = getResource => group => {
|
|
|
|
return getResource( `settings/${ group }` ).error;
|
2019-02-13 11:44:58 +00:00
|
|
|
};
|
|
|
|
|
2019-05-24 07:22: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastRequested > lastReceived;
|
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
|
|
|
};
|