woocommerce-paypal-payments/modules/ppcp-settings/resources/js/data/common/resolvers.js

45 lines
1.1 KiB
JavaScript
Raw Normal View History

/**
* 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 { __ } from '@wordpress/i18n';
2025-02-06 14:20:22 +01:00
import apiFetch from '@wordpress/api-fetch';
2025-02-06 14:20:22 +01:00
import { REST_HYDRATE_PATH, REST_WEBHOOKS } from './constants';
2025-02-06 14:20:22 +01:00
/**
* Retrieve settings from the site's REST API.
*/
export function persistentData() {
return async ( { dispatch, registry } ) => {
try {
2025-02-06 14:20:22 +01:00
const [ result, webhooks ] = await Promise.all( [
apiFetch( { path: REST_HYDRATE_PATH } ),
apiFetch( { path: REST_WEBHOOKS } ),
] );
2025-02-06 14:20:22 +01:00
if ( result?.success && webhooks?.success && webhooks.data ) {
2025-01-09 20:28:34 +01:00
result.webhooks = webhooks.data;
}
2024-12-20 08:59:52 +01:00
2025-02-06 14:20:22 +01:00
await dispatch.hydrate( result );
await dispatch.setIsReady( true );
} catch ( e ) {
2025-02-06 14:20:22 +01:00
await registry
.dispatch( 'core/notices' )
.createErrorNotice(
__(
'Error retrieving plugin details.',
'woocommerce-paypal-payments'
)
);
}
2025-02-06 14:20:22 +01:00
};
}