Move card details and shipping address to the Redux store

This commit is contained in:
Daniel Dudzic 2024-09-24 02:09:38 +02:00
parent 75960fc4a4
commit 5a31fdd183
No known key found for this signature in database
GPG key ID: 31B40D33E3465483
6 changed files with 122 additions and 72 deletions

View file

@ -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 );
};