♻️ Apply new code style to “common” store

This commit is contained in:
Philipp Stracker 2024-11-20 17:21:09 +01:00
parent 8f12e978f3
commit 7ae4184d30
No known key found for this signature in database
9 changed files with 130 additions and 109 deletions

View file

@ -2,7 +2,7 @@
* 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 but must have a unique name.
* Each resolver corresponds to a specific selector (selector with same name must exist).
* Resolvers are called automatically when selectors request unavailable data.
*
* @file
@ -12,26 +12,25 @@ import { dispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { apiFetch } from '@wordpress/data-controls';
import { NAMESPACE } from '../constants';
import { setIsReady, setCommonDetails } from './actions';
import { REST_HYDRATE_PATH } from './constants';
import { STORE_NAME, REST_HYDRATE_PATH } from './constants';
/**
* Retrieve settings from the site's REST API.
*/
export function* commonPersistentData() {
const path = `${ NAMESPACE }/${ REST_HYDRATE_PATH }`;
export const resolvers = {
/**
* Retrieve settings from the site's REST API.
*/
*persistentData() {
try {
const result = yield apiFetch( { path: REST_HYDRATE_PATH } );
try {
const result = yield apiFetch( { path } );
yield setCommonDetails( result );
yield setIsReady( true );
} catch ( e ) {
yield dispatch( 'core/notices' ).createErrorNotice(
__(
'Error retrieving plugin details.',
'woocommerce-paypal-payments'
)
);
}
}
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'
)
);
}
},
};