mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 18:16:38 +08:00
Move card details and shipping address to the Redux store
This commit is contained in:
parent
75960fc4a4
commit
5a31fdd183
6 changed files with 122 additions and 72 deletions
|
@ -1,19 +1,19 @@
|
|||
// File: axoStore.js
|
||||
|
||||
import { createReduxStore, register, dispatch } from '@wordpress/data';
|
||||
|
||||
export const STORE_NAME = 'woocommerce-paypal-payments/axo-block';
|
||||
|
||||
// Initial state
|
||||
// Initial state.
|
||||
const DEFAULT_STATE = {
|
||||
isGuest: true,
|
||||
isAxoActive: false,
|
||||
isAxoScriptLoaded: false,
|
||||
isEmailSubmitted: false,
|
||||
isEmailLookupCompleted: false,
|
||||
shippingAddress: null,
|
||||
cardDetails: null,
|
||||
};
|
||||
|
||||
// Actions
|
||||
// Actions.
|
||||
const actions = {
|
||||
setIsGuest: ( isGuest ) => ( {
|
||||
type: 'SET_IS_GUEST',
|
||||
|
@ -35,9 +35,17 @@ const actions = {
|
|||
type: 'SET_IS_EMAIL_LOOKUP_COMPLETED',
|
||||
payload: isEmailLookupCompleted,
|
||||
} ),
|
||||
setShippingAddress: ( shippingAddress ) => ( {
|
||||
type: 'SET_SHIPPING_ADDRESS',
|
||||
payload: shippingAddress,
|
||||
} ),
|
||||
setCardDetails: ( cardDetails ) => ( {
|
||||
type: 'SET_CARD_DETAILS',
|
||||
payload: cardDetails,
|
||||
} ),
|
||||
};
|
||||
|
||||
// Reducer
|
||||
// Reducer.
|
||||
const reducer = ( state = DEFAULT_STATE, action ) => {
|
||||
switch ( action.type ) {
|
||||
case 'SET_IS_GUEST':
|
||||
|
@ -50,21 +58,27 @@ const reducer = ( state = DEFAULT_STATE, action ) => {
|
|||
return { ...state, isEmailSubmitted: action.payload };
|
||||
case 'SET_IS_EMAIL_LOOKUP_COMPLETED':
|
||||
return { ...state, isEmailLookupCompleted: action.payload };
|
||||
case 'SET_SHIPPING_ADDRESS':
|
||||
return { ...state, shippingAddress: action.payload };
|
||||
case 'SET_CARD_DETAILS':
|
||||
return { ...state, cardDetails: action.payload };
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
||||
|
||||
// Selectors
|
||||
// Selectors.
|
||||
const selectors = {
|
||||
getIsGuest: ( state ) => state.isGuest,
|
||||
getIsAxoActive: ( state ) => state.isAxoActive,
|
||||
getIsAxoScriptLoaded: ( state ) => state.isAxoScriptLoaded,
|
||||
getIsEmailSubmitted: ( state ) => state.isEmailSubmitted,
|
||||
getIsEmailLookupCompleted: ( state ) => state.isEmailLookupCompleted,
|
||||
getShippingAddress: ( state ) => state.shippingAddress,
|
||||
getCardDetails: ( state ) => state.cardDetails,
|
||||
};
|
||||
|
||||
// Create and register the store
|
||||
// Create and register the store.
|
||||
const store = createReduxStore( STORE_NAME, {
|
||||
reducer,
|
||||
actions,
|
||||
|
@ -73,20 +87,19 @@ const store = createReduxStore( STORE_NAME, {
|
|||
|
||||
register( store );
|
||||
|
||||
// Action dispatchers.
|
||||
export const setIsGuest = ( isGuest ) => {
|
||||
try {
|
||||
dispatch( STORE_NAME ).setIsGuest( isGuest );
|
||||
} catch ( error ) {
|
||||
console.error( 'Error updating isGuest state:', error );
|
||||
}
|
||||
dispatch( STORE_NAME ).setIsGuest( isGuest );
|
||||
};
|
||||
|
||||
export const setIsEmailLookupCompleted = ( isEmailLookupCompleted ) => {
|
||||
try {
|
||||
dispatch( STORE_NAME ).setIsEmailLookupCompleted(
|
||||
isEmailLookupCompleted
|
||||
);
|
||||
} catch ( error ) {
|
||||
console.error( 'Error updating isEmailLookupCompleted state:', error );
|
||||
}
|
||||
dispatch( STORE_NAME ).setIsEmailLookupCompleted( isEmailLookupCompleted );
|
||||
};
|
||||
|
||||
export const setShippingAddress = ( shippingAddress ) => {
|
||||
dispatch( STORE_NAME ).setShippingAddress( shippingAddress );
|
||||
};
|
||||
|
||||
export const setCardDetails = ( cardDetails ) => {
|
||||
dispatch( STORE_NAME ).setCardDetails( cardDetails );
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue