woocommerce/plugins/woocommerce-admin/client/marketing/data/resolvers.js

58 lines
1.2 KiB
JavaScript

/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { apiFetch } from '@wordpress/data-controls';
/**
* Internal dependencies
*/
import {
handleFetchError,
receiveRecommendedPlugins,
receiveBlogPosts,
setError,
} from './actions';
import { API_NAMESPACE } from './constants';
export function* getRecommendedPlugins( category ) {
try {
const categoryParam = yield category ? `&category=${ category }` : '';
const response = yield apiFetch( {
path: `${ API_NAMESPACE }/recommended?per_page=6${ categoryParam }`,
} );
if ( response ) {
yield receiveRecommendedPlugins( response, category );
} else {
throw new Error();
}
} catch ( error ) {
yield handleFetchError(
error,
__(
'There was an error loading recommended extensions.',
'woocommerce-admin'
)
);
}
}
export function* getBlogPosts( category ) {
try {
const categoryParam = yield category ? `?category=${ category }` : '';
const response = yield apiFetch( {
path: `${ API_NAMESPACE }/knowledge-base${ categoryParam }`,
method: 'GET',
} );
if ( response ) {
yield receiveBlogPosts( response, category );
} else {
throw new Error();
}
} catch ( error ) {
yield setError( category, error );
}
}