UNPKG

@woocommerce/data

Version:
32 lines (31 loc) 1.04 kB
/** * External dependencies */ import { addQueryArgs } from '@wordpress/url'; /** * Internal dependencies */ import { NAMESPACE } from '../constants'; import { setError, updateReviews } from './actions'; import { fetchWithHeaders } from '../controls'; export function* getReviews(query) { try { const url = addQueryArgs(`${NAMESPACE}/products/reviews`, query); const response = yield fetchWithHeaders({ path: url, method: 'GET', }); const totalCountFromHeader = response.headers.get('x-wp-total'); if (totalCountFromHeader === undefined) { throw new Error("Malformed response from server. 'x-wp-total' header is missing when retriving ./products/reviews."); } const totalCount = parseInt(totalCountFromHeader, 10); yield updateReviews(query, response.data, totalCount); } catch (error) { yield setError(JSON.stringify(query), error); } } export function* getReviewsTotalCount(query) { yield getReviews(query); }