2018-12-04 02:58:07 +00:00
|
|
|
/** @format */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { isNil } from 'lodash';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { getResourceName } from '../../utils';
|
|
|
|
import { DEFAULT_REQUIREMENT } from '../../constants';
|
|
|
|
|
|
|
|
const getReportStats = ( getResource, requireResource ) => (
|
2018-12-04 17:22:46 +00:00
|
|
|
type,
|
2018-12-04 02:58:07 +00:00
|
|
|
query = {},
|
|
|
|
requirement = DEFAULT_REQUIREMENT
|
|
|
|
) => {
|
2018-12-04 17:22:46 +00:00
|
|
|
const resourceName = getResourceName( `report-stats-query-${ type }`, query );
|
2018-12-04 02:58:07 +00:00
|
|
|
const data = requireResource( requirement, resourceName ) || {};
|
|
|
|
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
2018-12-15 12:38:54 +00:00
|
|
|
const getReportStatsError = getResource => ( type, query = {} ) => {
|
|
|
|
const resourceName = getResourceName( `report-stats-query-${ type }`, query );
|
|
|
|
return getResource( resourceName ).error;
|
|
|
|
};
|
|
|
|
|
2018-12-04 17:22:46 +00:00
|
|
|
const isReportStatsRequesting = getResource => ( type, query = {} ) => {
|
|
|
|
const resourceName = getResourceName( `report-stats-query-${ type }`, query );
|
2018-12-04 02:58:07 +00:00
|
|
|
const { lastRequested, lastReceived } = getResource( resourceName );
|
|
|
|
|
2018-12-05 18:39:51 +00:00
|
|
|
if ( isNil( lastRequested ) || isNil( lastReceived ) ) {
|
2018-12-04 02:58:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastRequested > lastReceived;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
getReportStats,
|
2018-12-15 12:38:54 +00:00
|
|
|
getReportStatsError,
|
2018-12-04 02:58:07 +00:00
|
|
|
isReportStatsRequesting,
|
|
|
|
};
|