mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 10:36:38 +08:00
67 lines
1.5 KiB
JavaScript
67 lines
1.5 KiB
JavaScript
// 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,
|
|
isAxoScriptLoaded: false,
|
|
};
|
|
|
|
// Actions
|
|
const actions = {
|
|
setIsGuest: ( isGuest ) => ( {
|
|
type: 'SET_IS_GUEST',
|
|
payload: isGuest,
|
|
} ),
|
|
setIsAxoActive: ( isAxoActive ) => ( {
|
|
type: 'SET_IS_AXO_ACTIVE',
|
|
payload: isAxoActive,
|
|
} ),
|
|
setIsAxoScriptLoaded: ( isAxoScriptLoaded ) => ( {
|
|
type: 'SET_IS_AXO_SCRIPT_LOADED',
|
|
payload: isAxoScriptLoaded,
|
|
} ),
|
|
};
|
|
|
|
// 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 };
|
|
case 'SET_IS_AXO_SCRIPT_LOADED':
|
|
return { ...state, isAxoScriptLoaded: action.payload };
|
|
default:
|
|
return state;
|
|
}
|
|
};
|
|
|
|
// Selectors
|
|
const selectors = {
|
|
getIsGuest: ( state ) => state.isGuest,
|
|
getIsAxoActive: ( state ) => state.isAxoActive,
|
|
isAxoScriptLoaded: ( state ) => state.isAxoScriptLoaded,
|
|
};
|
|
|
|
// 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 );
|
|
}
|
|
};
|