♻️ Common store: Convert to thunks

This commit is contained in:
Philipp Stracker 2025-02-06 14:20:22 +01:00
parent e18f7fd7ae
commit d90a893a38
No known key found for this signature in database
5 changed files with 232 additions and 278 deletions

View file

@ -8,34 +8,37 @@
* @file
*/
import { dispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { apiFetch } from '@wordpress/data-controls';
import apiFetch from '@wordpress/api-fetch';
import { STORE_NAME, REST_HYDRATE_PATH, REST_WEBHOOKS } from './constants';
import { REST_HYDRATE_PATH, REST_WEBHOOKS } from './constants';
export const resolvers = {
/**
* Retrieve settings from the site's REST API.
*/
*persistentData() {
/**
* Retrieve settings from the site's REST API.
*/
export function persistentData() {
return async ( { dispatch, registry } ) => {
try {
const result = yield apiFetch( { path: REST_HYDRATE_PATH } );
const webhooks = yield apiFetch( { path: REST_WEBHOOKS } );
const [ result, webhooks ] = await Promise.all( [
apiFetch( { path: REST_HYDRATE_PATH } ),
apiFetch( { path: REST_WEBHOOKS } ),
] );
if ( webhooks.success && webhooks.data ) {
if ( result?.success && webhooks?.success && webhooks.data ) {
result.webhooks = webhooks.data;
}
yield dispatch( STORE_NAME ).hydrate( result );
yield dispatch( STORE_NAME ).setIsReady( true );
await dispatch.hydrate( result );
await dispatch.setIsReady( true );
} catch ( e ) {
yield dispatch( 'core/notices' ).createErrorNotice(
__(
'Error retrieving plugin details.',
'woocommerce-paypal-payments'
)
);
await registry
.dispatch( 'core/notices' )
.createErrorNotice(
__(
'Error retrieving plugin details.',
'woocommerce-paypal-payments'
)
);
}
},
};
};
}