2018-12-12 12:55:10 +00:00
|
|
|
/** @format */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { isNil } from 'lodash';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { getResourceName } from '../utils';
|
|
|
|
import { DEFAULT_REQUIREMENT } from '../constants';
|
|
|
|
|
|
|
|
const getCustomers = ( getResource, requireResource ) => (
|
|
|
|
query = {},
|
|
|
|
requirement = DEFAULT_REQUIREMENT
|
|
|
|
) => {
|
|
|
|
const resourceName = getResourceName( 'customers-query', query );
|
|
|
|
const ids = requireResource( requirement, resourceName ).data || [];
|
|
|
|
return ids.map( id => getResource( getResourceName( 'customer', id ) ).data || {} );
|
|
|
|
};
|
|
|
|
|
2018-12-15 12:09:47 +00:00
|
|
|
const getCustomersError = getResource => ( query = {} ) => {
|
|
|
|
const resourceName = getResourceName( 'customers-query', query );
|
|
|
|
return getResource( resourceName ).error;
|
|
|
|
};
|
|
|
|
|
2018-12-12 12:55:10 +00:00
|
|
|
const isGetCustomersRequesting = getResource => ( query = {} ) => {
|
|
|
|
const resourceName = getResourceName( 'customers-query', query );
|
|
|
|
const { lastRequested, lastReceived } = getResource( resourceName );
|
|
|
|
|
|
|
|
if ( isNil( lastRequested ) || isNil( lastReceived ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastRequested > lastReceived;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
getCustomers,
|
2018-12-15 12:09:47 +00:00
|
|
|
getCustomersError,
|
2018-12-12 12:55:10 +00:00
|
|
|
isGetCustomersRequesting,
|
|
|
|
};
|