mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 18:16:38 +08:00
♻️ Apply new code style to “common” store
This commit is contained in:
parent
8f12e978f3
commit
7ae4184d30
9 changed files with 130 additions and 109 deletions
|
@ -1,9 +1,6 @@
|
||||||
/**
|
/**
|
||||||
* Action Types: Define unique identifiers for actions across all store modules.
|
* Action Types: Define unique identifiers for actions across all store modules.
|
||||||
*
|
*
|
||||||
* Keys are module-internal and can have any value.
|
|
||||||
* Values must be unique across all store modules to avoid collisions.
|
|
||||||
*
|
|
||||||
* @file
|
* @file
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -13,6 +10,7 @@ export default {
|
||||||
|
|
||||||
// Persistent data.
|
// Persistent data.
|
||||||
SET_PERSISTENT: 'COMMON:SET_PERSISTENT',
|
SET_PERSISTENT: 'COMMON:SET_PERSISTENT',
|
||||||
|
HYDRATE: 'COMMON:HYDRATE',
|
||||||
|
|
||||||
// Controls - always start with "DO_".
|
// Controls - always start with "DO_".
|
||||||
DO_PERSIST_DATA: 'COMMON:DO_PERSIST_DATA',
|
DO_PERSIST_DATA: 'COMMON:DO_PERSIST_DATA',
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
* Action Creators: Define functions to create action objects.
|
* Action Creators: Define functions to create action objects.
|
||||||
*
|
*
|
||||||
* These functions update state or trigger side effects (e.g., async operations).
|
* These functions update state or trigger side effects (e.g., async operations).
|
||||||
* Exported functions must have unique names across all store modules.
|
|
||||||
* Actions are categorized as Transient, Persistent, or Side effect.
|
* Actions are categorized as Transient, Persistent, or Side effect.
|
||||||
*
|
*
|
||||||
* @file
|
* @file
|
||||||
|
@ -10,91 +9,98 @@
|
||||||
|
|
||||||
import ACTION_TYPES from './action-types';
|
import ACTION_TYPES from './action-types';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {Object} Action An action object that is handled by a reducer or control.
|
||||||
|
* @property {string} type - The action type.
|
||||||
|
* @property {Object?} payload - Optional payload for the action.
|
||||||
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transient. Marks the onboarding details as "ready", i.e., fully initialized.
|
* Transient. Marks the onboarding details as "ready", i.e., fully initialized.
|
||||||
*
|
*
|
||||||
* @param {boolean} isReady
|
* @param {boolean} isReady
|
||||||
* @return {{type: string, isReady: boolean}} The action.
|
* @return {Action} The action.
|
||||||
*/
|
*/
|
||||||
export const setIsReady = ( isReady ) => {
|
export const setIsReady = ( isReady ) => ( {
|
||||||
return {
|
|
||||||
type: ACTION_TYPES.SET_TRANSIENT,
|
type: ACTION_TYPES.SET_TRANSIENT,
|
||||||
isReady,
|
payload: { isReady },
|
||||||
};
|
} );
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transient. Changes the "saving" flag.
|
* Transient. Changes the "saving" flag.
|
||||||
*
|
*
|
||||||
* @param {boolean} isSaving
|
* @param {boolean} isSaving
|
||||||
* @return {{type: string, isSaving: boolean}} The action.
|
* @return {Action} The action.
|
||||||
*/
|
*/
|
||||||
export const setIsSaving = ( isSaving ) => {
|
export const setIsSaving = ( isSaving ) => ( {
|
||||||
return {
|
|
||||||
type: ACTION_TYPES.SET_TRANSIENT,
|
type: ACTION_TYPES.SET_TRANSIENT,
|
||||||
isSaving,
|
payload: { isSaving },
|
||||||
};
|
} );
|
||||||
};
|
|
||||||
|
/**
|
||||||
|
* Transient. Changes the "manual connection is busy" flag.
|
||||||
|
*
|
||||||
|
* @param {boolean} isBusy
|
||||||
|
* @return {Action} The action.
|
||||||
|
*/
|
||||||
|
export const setIsBusy = ( isBusy ) => ( {
|
||||||
|
type: ACTION_TYPES.SET_TRANSIENT,
|
||||||
|
payload: { isBusy },
|
||||||
|
} );
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Persistent. Sets the sandbox mode on or off.
|
* Persistent. Sets the sandbox mode on or off.
|
||||||
*
|
*
|
||||||
* @param {boolean} useSandbox
|
* @param {boolean} useSandbox
|
||||||
* @return {{type: string, useSandbox: boolean}} An action.
|
* @return {Action} The action.
|
||||||
*/
|
*/
|
||||||
export const setSandboxMode = ( useSandbox ) => {
|
export const setSandboxMode = ( useSandbox ) => ( {
|
||||||
return {
|
|
||||||
type: ACTION_TYPES.SET_PERSISTENT,
|
type: ACTION_TYPES.SET_PERSISTENT,
|
||||||
useSandbox,
|
payload: { useSandbox },
|
||||||
};
|
} );
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Persistent. Toggles the "Manual Connection" mode on or off.
|
* Persistent. Toggles the "Manual Connection" mode on or off.
|
||||||
*
|
*
|
||||||
* @param {boolean} useManualConnection
|
* @param {boolean} useManualConnection
|
||||||
* @return {{type: string, useManualConnection: boolean}} An action.
|
* @return {Action} The action.
|
||||||
*/
|
*/
|
||||||
export const setManualConnectionMode = ( useManualConnection ) => {
|
export const setManualConnectionMode = ( useManualConnection ) => ( {
|
||||||
return {
|
|
||||||
type: ACTION_TYPES.SET_PERSISTENT,
|
type: ACTION_TYPES.SET_PERSISTENT,
|
||||||
useManualConnection,
|
payload: { useManualConnection },
|
||||||
};
|
} );
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Persistent. Changes the "client ID" value.
|
* Persistent. Changes the "client ID" value.
|
||||||
*
|
*
|
||||||
* @param {string} clientId
|
* @param {string} clientId
|
||||||
* @return {{type: string, clientId: string}} The action.
|
* @return {Action} The action.
|
||||||
*/
|
*/
|
||||||
export const setClientId = ( clientId ) => {
|
export const setClientId = ( clientId ) => ( {
|
||||||
return {
|
|
||||||
type: ACTION_TYPES.SET_PERSISTENT,
|
type: ACTION_TYPES.SET_PERSISTENT,
|
||||||
clientId,
|
payload: { clientId },
|
||||||
};
|
} );
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Persistent. Changes the "client secret" value.
|
* Persistent. Changes the "client secret" value.
|
||||||
*
|
*
|
||||||
* @param {string} clientSecret
|
* @param {string} clientSecret
|
||||||
* @return {{type: string, clientSecret: string}} The action.
|
* @return {Action} The action.
|
||||||
*/
|
*/
|
||||||
export const setClientSecret = ( clientSecret ) => {
|
export const setClientSecret = ( clientSecret ) => ( {
|
||||||
return {
|
|
||||||
type: ACTION_TYPES.SET_PERSISTENT,
|
type: ACTION_TYPES.SET_PERSISTENT,
|
||||||
clientSecret,
|
payload: { clientSecret },
|
||||||
};
|
} );
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Side effect. Saves the persistent details to the WP database.
|
* Side effect. Saves the persistent details to the WP database.
|
||||||
*
|
*
|
||||||
* @return {{type: string}} The action.
|
* @return {Action} The action.
|
||||||
*/
|
*/
|
||||||
export function* commonPersist() {
|
export const persist = function* () {
|
||||||
return {
|
yield setIsBusy( true );
|
||||||
|
yield {
|
||||||
type: ACTION_TYPES.DO_PERSIST_DATA,
|
type: ACTION_TYPES.DO_PERSIST_DATA,
|
||||||
};
|
};
|
||||||
}
|
yield setIsBusy( false );
|
||||||
|
};
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
*
|
*
|
||||||
* @type {string}
|
* @type {string}
|
||||||
*/
|
*/
|
||||||
export const STORE_KEY = 'common';
|
export const STORE_NAME = 'wc/paypal/common';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* REST path to hydrate data of this module by loading data from the WP DB..
|
* REST path to hydrate data of this module by loading data from the WP DB..
|
||||||
|
@ -14,7 +14,7 @@ export const STORE_KEY = 'common';
|
||||||
*
|
*
|
||||||
* @type {string}
|
* @type {string}
|
||||||
*/
|
*/
|
||||||
export const REST_HYDRATE_PATH = 'common';
|
export const REST_HYDRATE_PATH = '/wc/v3/wc_paypal/common';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* REST path to persist data of this module to the WP DB.
|
* REST path to persist data of this module to the WP DB.
|
||||||
|
@ -23,4 +23,4 @@ export const REST_HYDRATE_PATH = 'common';
|
||||||
*
|
*
|
||||||
* @type {string}
|
* @type {string}
|
||||||
*/
|
*/
|
||||||
export const REST_PERSIST_PATH = 'common';
|
export const REST_PERSIST_PATH = '/wc/v3/wc_paypal/common';
|
||||||
|
|
|
@ -1,32 +1,27 @@
|
||||||
/**
|
/**
|
||||||
* Controls: Implement side effects, typically asynchronous operations.
|
* Controls: Implement side effects, typically asynchronous operations.
|
||||||
*
|
*
|
||||||
* Controls use ACTION_TYPES keys as identifiers to ensure uniqueness.
|
* Controls use ACTION_TYPES keys as identifiers.
|
||||||
* They are triggered by corresponding actions and handle external interactions.
|
* They are triggered by corresponding actions and handle external interactions.
|
||||||
*
|
*
|
||||||
* @file
|
* @file
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { select } from '@wordpress/data';
|
import apiFetch from '@wordpress/api-fetch';
|
||||||
import { apiFetch } from '@wordpress/api-fetch';
|
|
||||||
|
|
||||||
import { NAMESPACE, STORE_NAME } from '../constants';
|
|
||||||
import { REST_PERSIST_PATH } from './constants';
|
import { REST_PERSIST_PATH } from './constants';
|
||||||
import ACTION_TYPES from './action-types';
|
import ACTION_TYPES from './action-types';
|
||||||
export const controls = {
|
|
||||||
[ ACTION_TYPES.DO_PERSIST_DATA ]: async () => {
|
|
||||||
const path = `${ NAMESPACE }/${ REST_PERSIST_PATH }`;
|
|
||||||
const data = select( STORE_NAME ).getPersistentData();
|
|
||||||
|
|
||||||
|
export const controls = {
|
||||||
|
async [ ACTION_TYPES.DO_PERSIST_DATA ]( { data } ) {
|
||||||
try {
|
try {
|
||||||
return await apiFetch( {
|
return await apiFetch( {
|
||||||
path,
|
path: REST_PERSIST_PATH,
|
||||||
method: 'post',
|
method: 'POST',
|
||||||
data,
|
data,
|
||||||
} );
|
} );
|
||||||
} catch ( error ) {
|
} catch ( error ) {
|
||||||
console.error( 'Error saving progress.', error );
|
console.error( 'Error saving data.', error );
|
||||||
throw error;
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -3,7 +3,22 @@
|
||||||
*
|
*
|
||||||
* These encapsulate store interactions, offering a consistent interface.
|
* These encapsulate store interactions, offering a consistent interface.
|
||||||
* Hooks simplify data access and manipulation for components.
|
* Hooks simplify data access and manipulation for components.
|
||||||
* Exported hooks must have unique names across all store modules.
|
|
||||||
*
|
*
|
||||||
* @file
|
* @file
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { useSelect } from '@wordpress/data';
|
||||||
|
|
||||||
|
import { STORE_NAME } from './constants';
|
||||||
|
|
||||||
|
const useTransient = ( key ) =>
|
||||||
|
useSelect(
|
||||||
|
( select ) => select( STORE_NAME ).transientData()?.[ key ],
|
||||||
|
[ key ]
|
||||||
|
);
|
||||||
|
|
||||||
|
const usePersistent = ( key ) =>
|
||||||
|
useSelect(
|
||||||
|
( select ) => select( STORE_NAME ).persistentData()?.[ key ],
|
||||||
|
[ key ]
|
||||||
|
);
|
||||||
|
|
|
@ -1,8 +1,26 @@
|
||||||
import { STORE_KEY } from './constants';
|
import { createReduxStore, register } from '@wordpress/data';
|
||||||
|
import { controls as wpControls } from '@wordpress/data-controls';
|
||||||
|
|
||||||
|
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 resolvers from './resolvers';
|
import * as hooks from './hooks';
|
||||||
|
import { resolvers } from './resolvers';
|
||||||
import { controls } from './controls';
|
import { controls } from './controls';
|
||||||
|
|
||||||
export { reducer, selectors, actions, resolvers, controls, STORE_KEY };
|
export const initStore = () => {
|
||||||
|
const store = createReduxStore( STORE_NAME, {
|
||||||
|
reducer,
|
||||||
|
controls: { ...wpControls, ...controls },
|
||||||
|
actions,
|
||||||
|
selectors,
|
||||||
|
resolvers,
|
||||||
|
} );
|
||||||
|
|
||||||
|
register( store );
|
||||||
|
};
|
||||||
|
|
||||||
|
export { hooks };
|
||||||
|
|
||||||
|
export { STORE_NAME };
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
* Reducer: Defines store structure and state updates for this module.
|
* Reducer: Defines store structure and state updates for this module.
|
||||||
*
|
*
|
||||||
* Manages both transient (temporary) and persistent (saved) state.
|
* Manages both transient (temporary) and persistent (saved) state.
|
||||||
* Each module uses isolated memory objects to prevent conflicts.
|
|
||||||
* The initial state must define all properties, as dynamic additions are not supported.
|
* The initial state must define all properties, as dynamic additions are not supported.
|
||||||
*
|
*
|
||||||
* @file
|
* @file
|
||||||
|
@ -16,6 +15,7 @@ import ACTION_TYPES from './action-types';
|
||||||
const defaultTransient = {
|
const defaultTransient = {
|
||||||
isReady: false,
|
isReady: false,
|
||||||
isSaving: false,
|
isSaving: false,
|
||||||
|
isBusy: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
const defaultPersistent = {
|
const defaultPersistent = {
|
||||||
|
@ -38,6 +38,9 @@ const commonReducer = createReducer( defaultTransient, defaultPersistent, {
|
||||||
|
|
||||||
[ ACTION_TYPES.SET_PERSISTENT ]: ( state, action ) =>
|
[ ACTION_TYPES.SET_PERSISTENT ]: ( state, action ) =>
|
||||||
setPersistent( state, action ),
|
setPersistent( state, action ),
|
||||||
|
|
||||||
|
[ ACTION_TYPES.HYDRATE ]: ( state, payload ) =>
|
||||||
|
setPersistent( state, payload.data ),
|
||||||
} );
|
} );
|
||||||
|
|
||||||
export default commonReducer;
|
export default commonReducer;
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
* Resolvers: Handle asynchronous data fetching for the store.
|
* Resolvers: Handle asynchronous data fetching for the store.
|
||||||
*
|
*
|
||||||
* These functions update store state with data from external sources.
|
* 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.
|
* Resolvers are called automatically when selectors request unavailable data.
|
||||||
*
|
*
|
||||||
* @file
|
* @file
|
||||||
|
@ -12,20 +12,18 @@ import { dispatch } from '@wordpress/data';
|
||||||
import { __ } from '@wordpress/i18n';
|
import { __ } from '@wordpress/i18n';
|
||||||
import { apiFetch } from '@wordpress/data-controls';
|
import { apiFetch } from '@wordpress/data-controls';
|
||||||
|
|
||||||
import { NAMESPACE } from '../constants';
|
import { STORE_NAME, REST_HYDRATE_PATH } from './constants';
|
||||||
import { setIsReady, setCommonDetails } from './actions';
|
|
||||||
import { REST_HYDRATE_PATH } from './constants';
|
|
||||||
|
|
||||||
/**
|
export const resolvers = {
|
||||||
|
/**
|
||||||
* Retrieve settings from the site's REST API.
|
* Retrieve settings from the site's REST API.
|
||||||
*/
|
*/
|
||||||
export function* commonPersistentData() {
|
*persistentData() {
|
||||||
const path = `${ NAMESPACE }/${ REST_HYDRATE_PATH }`;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = yield apiFetch( { path } );
|
const result = yield apiFetch( { path: REST_HYDRATE_PATH } );
|
||||||
yield setCommonDetails( result );
|
|
||||||
yield setIsReady( true );
|
yield dispatch( STORE_NAME ).hydrate( result );
|
||||||
|
yield dispatch( STORE_NAME ).setIsReady( true );
|
||||||
} catch ( e ) {
|
} catch ( e ) {
|
||||||
yield dispatch( 'core/notices' ).createErrorNotice(
|
yield dispatch( 'core/notices' ).createErrorNotice(
|
||||||
__(
|
__(
|
||||||
|
@ -34,4 +32,5 @@ export function* commonPersistentData() {
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
};
|
||||||
|
|
|
@ -3,32 +3,19 @@
|
||||||
*
|
*
|
||||||
* These functions provide a consistent interface for accessing store data.
|
* These functions provide a consistent interface for accessing store data.
|
||||||
* They allow components to retrieve data without knowing the store structure.
|
* They allow components to retrieve data without knowing the store structure.
|
||||||
* Exported functions must have unique names across all store modules.
|
|
||||||
*
|
*
|
||||||
* @file
|
* @file
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { STORE_KEY } from './constants';
|
|
||||||
|
|
||||||
const EMPTY_OBJ = Object.freeze( {} );
|
const EMPTY_OBJ = Object.freeze( {} );
|
||||||
|
|
||||||
const getState = ( state ) => {
|
const getState = ( state ) => state || EMPTY_OBJ;
|
||||||
if ( ! state ) {
|
|
||||||
return EMPTY_OBJ;
|
|
||||||
}
|
|
||||||
|
|
||||||
return state[ STORE_KEY ] || EMPTY_OBJ;
|
export const persistentData = ( state ) => {
|
||||||
};
|
|
||||||
|
|
||||||
export const commonPersistentData = ( state ) => {
|
|
||||||
return getState( state ).data || EMPTY_OBJ;
|
return getState( state ).data || EMPTY_OBJ;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const commonTransientData = ( state ) => {
|
export const transientData = ( state ) => {
|
||||||
const { data, flags, ...transientState } = getState( state );
|
const { data, ...transientState } = getState( state );
|
||||||
return transientState || EMPTY_OBJ;
|
return transientState || EMPTY_OBJ;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const commonFlags = ( state ) => {
|
|
||||||
return getState( state ).flags || EMPTY_OBJ;
|
|
||||||
};
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue