mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-07 19:54:15 +08:00
Add better state management utilizing Redux
This commit is contained in:
parent
db449b5d70
commit
bccfe4c436
11 changed files with 432 additions and 172 deletions
59
modules/ppcp-axo-block/resources/js/stores/axoStore.js
Normal file
59
modules/ppcp-axo-block/resources/js/stores/axoStore.js
Normal file
|
@ -0,0 +1,59 @@
|
|||
// File: axoStore.js
|
||||
|
||||
import { createReduxStore, register, dispatch } from '@wordpress/data';
|
||||
|
||||
export const STORE_NAME = 'woocommerce-paypal-payments/axo-block';
|
||||
|
||||
// Initial state
|
||||
const DEFAULT_STATE = {
|
||||
isGuest: true,
|
||||
isAxoActive: false,
|
||||
};
|
||||
|
||||
// Actions
|
||||
const actions = {
|
||||
setIsGuest: ( isGuest ) => ( {
|
||||
type: 'SET_IS_GUEST',
|
||||
payload: isGuest,
|
||||
} ),
|
||||
setIsAxoActive: ( isAxoActive ) => ( {
|
||||
type: 'SET_IS_AXO_ACTIVE',
|
||||
payload: isAxoActive,
|
||||
} ),
|
||||
};
|
||||
|
||||
// Reducer
|
||||
const reducer = ( state = DEFAULT_STATE, action ) => {
|
||||
switch ( action.type ) {
|
||||
case 'SET_IS_GUEST':
|
||||
return { ...state, isGuest: action.payload };
|
||||
case 'SET_IS_AXO_ACTIVE':
|
||||
return { ...state, isAxoActive: action.payload };
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
||||
|
||||
// Selectors
|
||||
const selectors = {
|
||||
getIsGuest: ( state ) => state.isGuest,
|
||||
getIsAxoActive: ( state ) => state.isAxoActive,
|
||||
};
|
||||
|
||||
// Create and register the store
|
||||
const store = createReduxStore( STORE_NAME, {
|
||||
reducer,
|
||||
actions,
|
||||
selectors,
|
||||
} );
|
||||
|
||||
register( store );
|
||||
|
||||
// Utility functions
|
||||
export const setIsGuest = ( isGuest ) => {
|
||||
try {
|
||||
dispatch( STORE_NAME ).setIsGuest( isGuest );
|
||||
} catch ( error ) {
|
||||
console.error( 'Error updating isGuest state:', error );
|
||||
}
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue