mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 09:08:09 +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
|
@ -31,10 +31,4 @@ export default {
|
||||||
* to set up the initial state with data from the server.
|
* to set up the initial state with data from the server.
|
||||||
*/
|
*/
|
||||||
HYDRATE: 'ppcp/settings/HYDRATE',
|
HYDRATE: 'ppcp/settings/HYDRATE',
|
||||||
|
|
||||||
/**
|
|
||||||
* Triggers the persistence of store data to the server.
|
|
||||||
* Used when changes need to be saved to the backend.
|
|
||||||
*/
|
|
||||||
DO_PERSIST_DATA: 'ppcp/settings/DO_PERSIST_DATA',
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -7,9 +7,10 @@
|
||||||
* @file
|
* @file
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { select } from '@wordpress/data';
|
import apiFetch from '@wordpress/api-fetch';
|
||||||
|
|
||||||
import ACTION_TYPES from './action-types';
|
import ACTION_TYPES from './action-types';
|
||||||
import { STORE_NAME } from './constants';
|
import { REST_PERSIST_PATH } from './constants';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {Object} Action An action object that is handled by a reducer or control.
|
* @typedef {Object} Action An action object that is handled by a reducer or control.
|
||||||
|
@ -70,12 +71,22 @@ export const setPersistent = ( prop, value ) => ( {
|
||||||
export const setIsReady = ( isReady ) => setTransient( 'isReady', isReady );
|
export const setIsReady = ( isReady ) => setTransient( 'isReady', isReady );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Side effect. Triggers the persistence of store data to the server.
|
* Thunk action creator. Triggers the persistence of store data to the server.
|
||||||
* Yields an action with the current persistent data to be saved.
|
|
||||||
*
|
*
|
||||||
* @return {Action} The action.
|
* @return {Function} The thunk function.
|
||||||
*/
|
*/
|
||||||
export const persist = function* () {
|
export function persist() {
|
||||||
const data = yield select( STORE_NAME ).persistentData();
|
return async ( { select } ) => {
|
||||||
yield { type: ACTION_TYPES.DO_PERSIST_DATA, data };
|
const data = select.persistentData();
|
||||||
};
|
|
||||||
|
try {
|
||||||
|
await apiFetch( {
|
||||||
|
path: REST_PERSIST_PATH,
|
||||||
|
method: 'POST',
|
||||||
|
data,
|
||||||
|
} );
|
||||||
|
} catch ( e ) {
|
||||||
|
console.error( 'Error saving progress.', e );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
|
@ -1,34 +0,0 @@
|
||||||
/**
|
|
||||||
* Controls: Implement side effects, typically asynchronous operations.
|
|
||||||
*
|
|
||||||
* Controls use ACTION_TYPES keys as identifiers.
|
|
||||||
* They are triggered by corresponding actions and handle external interactions.
|
|
||||||
*
|
|
||||||
* @file
|
|
||||||
*/
|
|
||||||
|
|
||||||
import apiFetch from '@wordpress/api-fetch';
|
|
||||||
import { REST_PERSIST_PATH } from './constants';
|
|
||||||
import ACTION_TYPES from './action-types';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Control handlers for settings store actions.
|
|
||||||
* Each handler maps to an ACTION_TYPE and performs the corresponding async operation.
|
|
||||||
*/
|
|
||||||
export const controls = {
|
|
||||||
/**
|
|
||||||
* Persists settings data to the server via REST API.
|
|
||||||
* Triggered by the DO_PERSIST_DATA action to save settings changes.
|
|
||||||
*
|
|
||||||
* @param {Object} action The action object
|
|
||||||
* @param {Object} action.data The settings data to persist
|
|
||||||
* @return {Promise<Object>} The API response
|
|
||||||
*/
|
|
||||||
async [ ACTION_TYPES.DO_PERSIST_DATA ]( { data } ) {
|
|
||||||
return await apiFetch( {
|
|
||||||
path: REST_PERSIST_PATH,
|
|
||||||
method: 'POST',
|
|
||||||
data,
|
|
||||||
} );
|
|
||||||
},
|
|
||||||
};
|
|
|
@ -8,15 +8,13 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { createReduxStore, register } from '@wordpress/data';
|
import { createReduxStore, register } from '@wordpress/data';
|
||||||
import { controls as wpControls } from '@wordpress/data-controls';
|
|
||||||
|
|
||||||
import { STORE_NAME } from './constants';
|
import { STORE_NAME } from './constants';
|
||||||
import reducer from './reducer';
|
import reducer from './reducer';
|
||||||
import * as selectors from './selectors';
|
import * as selectors from './selectors';
|
||||||
import * as actions from './actions';
|
import * as actions from './actions';
|
||||||
import * as hooks from './hooks';
|
import * as hooks from './hooks';
|
||||||
import { resolvers } from './resolvers';
|
import * as resolvers from './resolvers';
|
||||||
import { controls } from './controls';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes and registers the settings store with WordPress data layer.
|
* Initializes and registers the settings store with WordPress data layer.
|
||||||
|
@ -27,7 +25,6 @@ import { controls } from './controls';
|
||||||
export const initStore = () => {
|
export const initStore = () => {
|
||||||
const store = createReduxStore( STORE_NAME, {
|
const store = createReduxStore( STORE_NAME, {
|
||||||
reducer,
|
reducer,
|
||||||
controls: { ...wpControls, ...controls },
|
|
||||||
actions,
|
actions,
|
||||||
selectors,
|
selectors,
|
||||||
resolvers,
|
resolvers,
|
||||||
|
|
|
@ -8,50 +8,31 @@
|
||||||
* @file
|
* @file
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { dispatch } from '@wordpress/data';
|
|
||||||
import { __ } from '@wordpress/i18n';
|
import { __ } from '@wordpress/i18n';
|
||||||
import { apiFetch } from '@wordpress/data-controls';
|
import apiFetch from '@wordpress/api-fetch';
|
||||||
import { STORE_NAME, REST_HYDRATE_PATH } from './constants';
|
|
||||||
|
|
||||||
export const resolvers = {
|
import { REST_HYDRATE_PATH } from './constants';
|
||||||
/**
|
|
||||||
* Retrieve PayPal settings from the site's REST API.
|
/**
|
||||||
* Hydrates the store with the retrieved data and marks it as ready.
|
* Retrieve settings from the site's REST API.
|
||||||
*
|
*/
|
||||||
* @generator
|
export function persistentData() {
|
||||||
* @yield {Object} API fetch and dispatch actions
|
return async ( { dispatch, registry } ) => {
|
||||||
*/
|
|
||||||
*persistentData() {
|
|
||||||
try {
|
try {
|
||||||
// Fetch settings data from REST API
|
const result = await apiFetch( { path: REST_HYDRATE_PATH } );
|
||||||
const result = yield apiFetch( {
|
|
||||||
path: REST_HYDRATE_PATH,
|
|
||||||
method: 'GET',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
Accept: 'application/json',
|
|
||||||
},
|
|
||||||
} );
|
|
||||||
|
|
||||||
// Update store with retrieved data
|
await dispatch.hydrate( result );
|
||||||
yield dispatch( STORE_NAME ).hydrate( result );
|
await dispatch.setIsReady( true );
|
||||||
// Mark store as ready for use
|
|
||||||
yield dispatch( STORE_NAME ).setIsReady( true );
|
|
||||||
} catch ( e ) {
|
} catch ( e ) {
|
||||||
// Log detailed error information for debugging
|
// TODO: Add the module name to the error message.
|
||||||
console.error( 'Full error details:', {
|
await registry
|
||||||
error: e,
|
.dispatch( 'core/notices' )
|
||||||
path: REST_HYDRATE_PATH,
|
.createErrorNotice(
|
||||||
store: STORE_NAME,
|
__(
|
||||||
} );
|
'Error retrieving PayPal Settings details.',
|
||||||
|
'woocommerce-paypal-payments'
|
||||||
// Display user-friendly error notice
|
)
|
||||||
yield dispatch( 'core/notices' ).createErrorNotice(
|
);
|
||||||
__(
|
|
||||||
'Error retrieving PayPal settings details.',
|
|
||||||
'woocommerce-paypal-payments'
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
};
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue