2018-12-12 13:35:56 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2019-07-16 08:56:24 +00:00
|
|
|
import { addQueryArgs } from '@wordpress/url';
|
2018-12-12 13:35:56 +00:00
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-02-14 02:23:21 +00:00
|
|
|
import {
|
|
|
|
isResourcePrefix,
|
|
|
|
getResourceIdentifier,
|
|
|
|
getResourceName,
|
|
|
|
} from '../utils';
|
2018-12-12 13:35:56 +00:00
|
|
|
import { NAMESPACE } from '../constants';
|
|
|
|
|
|
|
|
function read( resourceNames, fetch = apiFetch ) {
|
2020-02-14 02:23:21 +00:00
|
|
|
return [
|
|
|
|
...readReviews( resourceNames, fetch ),
|
|
|
|
...readReviewQueries( resourceNames, fetch ),
|
|
|
|
];
|
2018-12-12 13:35:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function readReviewQueries( resourceNames, fetch ) {
|
2020-02-14 02:23:21 +00:00
|
|
|
const filteredNames = resourceNames.filter( ( name ) =>
|
|
|
|
isResourcePrefix( name, 'review-query' )
|
|
|
|
);
|
2018-12-12 13:35:56 +00:00
|
|
|
|
2020-02-14 02:23:21 +00:00
|
|
|
return filteredNames.map( async ( resourceName ) => {
|
2018-12-12 13:35:56 +00:00
|
|
|
const query = getResourceIdentifier( resourceName );
|
2019-07-16 08:56:24 +00:00
|
|
|
const url = addQueryArgs( `${ NAMESPACE }/products/reviews`, query );
|
2018-12-12 13:35:56 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await fetch( {
|
|
|
|
parse: false,
|
|
|
|
path: url,
|
|
|
|
} );
|
|
|
|
|
|
|
|
const reviews = await response.json();
|
2020-07-28 02:32:58 +00:00
|
|
|
const totalCount = parseInt(
|
|
|
|
response.headers.get( 'x-wp-total' ),
|
|
|
|
10
|
|
|
|
);
|
2020-02-14 02:23:21 +00:00
|
|
|
const ids = reviews.map( ( review ) => review.id );
|
2018-12-12 13:35:56 +00:00
|
|
|
const reviewResources = reviews.reduce( ( resources, review ) => {
|
2020-02-14 02:23:21 +00:00
|
|
|
resources[ getResourceName( 'review', review.id ) ] = {
|
|
|
|
data: review,
|
|
|
|
};
|
2018-12-12 13:35:56 +00:00
|
|
|
return resources;
|
|
|
|
}, {} );
|
|
|
|
|
|
|
|
return {
|
|
|
|
[ resourceName ]: {
|
|
|
|
data: ids,
|
|
|
|
totalCount,
|
|
|
|
},
|
|
|
|
...reviewResources,
|
|
|
|
};
|
|
|
|
} catch ( error ) {
|
|
|
|
return { [ resourceName ]: { error } };
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
function readReviews( resourceNames, fetch ) {
|
2020-02-14 02:23:21 +00:00
|
|
|
const filteredNames = resourceNames.filter( ( name ) =>
|
|
|
|
isResourcePrefix( name, 'review' )
|
|
|
|
);
|
|
|
|
return filteredNames.map( ( resourceName ) =>
|
|
|
|
readReview( resourceName, fetch )
|
|
|
|
);
|
2018-12-12 13:35:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function readReview( resourceName, fetch ) {
|
|
|
|
const id = getResourceIdentifier( resourceName );
|
|
|
|
const url = `${ NAMESPACE }/products/reviews/${ id }`;
|
|
|
|
|
|
|
|
return fetch( { path: url } )
|
2020-02-14 02:23:21 +00:00
|
|
|
.then( ( review ) => {
|
2018-12-12 13:35:56 +00:00
|
|
|
return { [ resourceName ]: { data: review } };
|
|
|
|
} )
|
2020-02-14 02:23:21 +00:00
|
|
|
.catch( ( error ) => {
|
2018-12-12 13:35:56 +00:00
|
|
|
return { [ resourceName ]: { error } };
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
read,
|
|
|
|
};
|