mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 14:57:26 +08:00
♻️ Common store: Convert to thunks
This commit is contained in:
parent
e18f7fd7ae
commit
d90a893a38
5 changed files with 232 additions and 278 deletions
|
@ -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'
|
||||
)
|
||||
);
|
||||
}
|
||||
},
|
||||
};
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue