2018-12-12 13:35:56 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { isNil } from 'lodash';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { getResourceName } from '../utils';
|
|
|
|
import { DEFAULT_REQUIREMENT } from '../constants';
|
|
|
|
|
|
|
|
const getReviews = ( getResource, requireResource ) => (
|
|
|
|
query = {},
|
|
|
|
requirement = DEFAULT_REQUIREMENT
|
|
|
|
) => {
|
|
|
|
const resourceName = getResourceName( 'review-query', query );
|
|
|
|
const ids = requireResource( requirement, resourceName ).data || [];
|
2020-02-14 02:23:21 +00:00
|
|
|
const reviews = ids.map(
|
|
|
|
( id ) => getResource( getResourceName( 'review', id ) ).data || {}
|
|
|
|
);
|
2018-12-12 13:35:56 +00:00
|
|
|
return reviews;
|
|
|
|
};
|
|
|
|
|
|
|
|
const getReviewsTotalCount = ( getResource, requireResource ) => (
|
|
|
|
query = {},
|
|
|
|
requirement = DEFAULT_REQUIREMENT
|
|
|
|
) => {
|
|
|
|
const resourceName = getResourceName( 'review-query', query );
|
|
|
|
return requireResource( requirement, resourceName ).totalCount || 0;
|
|
|
|
};
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const getReviewsError = ( getResource ) => ( query = {} ) => {
|
2018-12-15 12:38:54 +00:00
|
|
|
const resourceName = getResourceName( 'review-query', query );
|
|
|
|
return getResource( resourceName ).error;
|
|
|
|
};
|
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
const isGetReviewsRequesting = ( getResource ) => ( query = {} ) => {
|
2018-12-12 13:35:56 +00:00
|
|
|
const resourceName = getResourceName( 'review-query', query );
|
|
|
|
const { lastRequested, lastReceived } = getResource( resourceName );
|
|
|
|
|
|
|
|
if ( isNil( lastRequested ) || isNil( lastReceived ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastRequested > lastReceived;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
getReviews,
|
2018-12-15 12:38:54 +00:00
|
|
|
getReviewsError,
|
2018-12-12 13:35:56 +00:00
|
|
|
getReviewsTotalCount,
|
|
|
|
isGetReviewsRequesting,
|
|
|
|
};
|