mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 10:55:00 +08:00
♻️ Settings store: Refactor to use thunks
This commit is contained in:
parent
05ad9cfc48
commit
1ecb5ae610
5 changed files with 43 additions and 94 deletions
|
@ -8,50 +8,31 @@
|
|||
* @file
|
||||
*/
|
||||
|
||||
import { dispatch } from '@wordpress/data';
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { apiFetch } from '@wordpress/data-controls';
|
||||
import { STORE_NAME, REST_HYDRATE_PATH } from './constants';
|
||||
import apiFetch from '@wordpress/api-fetch';
|
||||
|
||||
export const resolvers = {
|
||||
/**
|
||||
* Retrieve PayPal settings from the site's REST API.
|
||||
* Hydrates the store with the retrieved data and marks it as ready.
|
||||
*
|
||||
* @generator
|
||||
* @yield {Object} API fetch and dispatch actions
|
||||
*/
|
||||
*persistentData() {
|
||||
import { REST_HYDRATE_PATH } from './constants';
|
||||
|
||||
/**
|
||||
* Retrieve settings from the site's REST API.
|
||||
*/
|
||||
export function persistentData() {
|
||||
return async ( { dispatch, registry } ) => {
|
||||
try {
|
||||
// Fetch settings data from REST API
|
||||
const result = yield apiFetch( {
|
||||
path: REST_HYDRATE_PATH,
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Accept: 'application/json',
|
||||
},
|
||||
} );
|
||||
const result = await apiFetch( { path: REST_HYDRATE_PATH } );
|
||||
|
||||
// Update store with retrieved data
|
||||
yield dispatch( STORE_NAME ).hydrate( result );
|
||||
// Mark store as ready for use
|
||||
yield dispatch( STORE_NAME ).setIsReady( true );
|
||||
await dispatch.hydrate( result );
|
||||
await dispatch.setIsReady( true );
|
||||
} catch ( e ) {
|
||||
// Log detailed error information for debugging
|
||||
console.error( 'Full error details:', {
|
||||
error: e,
|
||||
path: REST_HYDRATE_PATH,
|
||||
store: STORE_NAME,
|
||||
} );
|
||||
|
||||
// Display user-friendly error notice
|
||||
yield dispatch( 'core/notices' ).createErrorNotice(
|
||||
__(
|
||||
'Error retrieving PayPal settings details.',
|
||||
'woocommerce-paypal-payments'
|
||||
)
|
||||
);
|
||||
// TODO: Add the module name to the error message.
|
||||
await registry
|
||||
.dispatch( 'core/notices' )
|
||||
.createErrorNotice(
|
||||
__(
|
||||
'Error retrieving PayPal Settings details.',
|
||||
'woocommerce-paypal-payments'
|
||||
)
|
||||
);
|
||||
}
|
||||
},
|
||||
};
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue