woocommerce-paypal-payments/modules/ppcp-settings/resources/js/data/common/resolvers.js
2025-01-14 15:44:56 +01:00

48 lines
1.3 KiB
JavaScript

/**
* Resolvers: Handle asynchronous data fetching for the store.
*
* These functions update store state with data from external sources.
* Each resolver corresponds to a specific selector (selector with same name must exist).
* Resolvers are called automatically when selectors request unavailable data.
*
* @file
*/
import { dispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { apiFetch } from '@wordpress/data-controls';
import { STORE_NAME, REST_HYDRATE_PATH, REST_WEBHOOKS } from './constants';
export const resolvers = {
/**
* Retrieve settings from the site's REST API.
*/
*persistentData() {
let webhooks = null;
try {
webhooks = yield apiFetch( { path: REST_WEBHOOKS } );
} catch ( e ) {
// If webhook retrieval fails we can continue. Usually, this is temporary.
console.error( 'Could not retrieve webhooks: ', e );
}
try {
const result = yield apiFetch( { path: REST_HYDRATE_PATH } );
if ( webhooks?.success && webhooks?.data ) {
result.webhooks = webhooks.data;
}
yield dispatch( STORE_NAME ).hydrate( result );
yield dispatch( STORE_NAME ).setIsReady( true );
} catch ( e ) {
yield dispatch( 'core/notices' ).createErrorNotice(
__(
'Error retrieving plugin details.',
'woocommerce-paypal-payments'
)
);
}
},
};