2024-09-10 12:03:50 +02:00
|
|
|
import { createReduxStore, register, dispatch } from '@wordpress/data';
|
|
|
|
|
|
|
|
export const STORE_NAME = 'woocommerce-paypal-payments/axo-block';
|
|
|
|
|
2024-09-24 14:46:12 +02:00
|
|
|
// Initial state
|
2024-09-10 12:03:50 +02:00
|
|
|
const DEFAULT_STATE = {
|
|
|
|
isGuest: true,
|
|
|
|
isAxoActive: false,
|
2024-09-11 00:39:09 +02:00
|
|
|
isAxoScriptLoaded: false,
|
2024-09-12 14:31:10 +02:00
|
|
|
isEmailSubmitted: false,
|
2024-09-13 19:24:23 +02:00
|
|
|
isEmailLookupCompleted: false,
|
2024-09-24 02:09:38 +02:00
|
|
|
shippingAddress: null,
|
|
|
|
cardDetails: null,
|
2024-09-24 14:46:12 +02:00
|
|
|
phoneNumber: '',
|
2024-09-10 12:03:50 +02:00
|
|
|
};
|
|
|
|
|
2024-09-24 14:46:12 +02:00
|
|
|
// Actions
|
2024-09-10 12:03:50 +02:00
|
|
|
const actions = {
|
|
|
|
setIsGuest: ( isGuest ) => ( {
|
|
|
|
type: 'SET_IS_GUEST',
|
|
|
|
payload: isGuest,
|
|
|
|
} ),
|
|
|
|
setIsAxoActive: ( isAxoActive ) => ( {
|
|
|
|
type: 'SET_IS_AXO_ACTIVE',
|
|
|
|
payload: isAxoActive,
|
|
|
|
} ),
|
2024-09-11 00:39:09 +02:00
|
|
|
setIsAxoScriptLoaded: ( isAxoScriptLoaded ) => ( {
|
|
|
|
type: 'SET_IS_AXO_SCRIPT_LOADED',
|
|
|
|
payload: isAxoScriptLoaded,
|
|
|
|
} ),
|
2024-09-12 14:31:10 +02:00
|
|
|
setIsEmailSubmitted: ( isEmailSubmitted ) => ( {
|
|
|
|
type: 'SET_IS_EMAIL_SUBMITTED',
|
|
|
|
payload: isEmailSubmitted,
|
|
|
|
} ),
|
2024-09-13 19:24:23 +02:00
|
|
|
setIsEmailLookupCompleted: ( isEmailLookupCompleted ) => ( {
|
|
|
|
type: 'SET_IS_EMAIL_LOOKUP_COMPLETED',
|
|
|
|
payload: isEmailLookupCompleted,
|
|
|
|
} ),
|
2024-09-24 02:09:38 +02:00
|
|
|
setShippingAddress: ( shippingAddress ) => ( {
|
|
|
|
type: 'SET_SHIPPING_ADDRESS',
|
|
|
|
payload: shippingAddress,
|
|
|
|
} ),
|
|
|
|
setCardDetails: ( cardDetails ) => ( {
|
|
|
|
type: 'SET_CARD_DETAILS',
|
|
|
|
payload: cardDetails,
|
|
|
|
} ),
|
2024-09-24 14:46:12 +02:00
|
|
|
setPhoneNumber: ( phoneNumber ) => ( {
|
|
|
|
type: 'SET_PHONE_NUMBER',
|
|
|
|
payload: phoneNumber,
|
|
|
|
} ),
|
2024-09-10 12:03:50 +02:00
|
|
|
};
|
|
|
|
|
2024-09-24 14:46:12 +02:00
|
|
|
// Reducer
|
2024-09-10 12:03:50 +02:00
|
|
|
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 };
|
2024-09-11 00:39:09 +02:00
|
|
|
case 'SET_IS_AXO_SCRIPT_LOADED':
|
|
|
|
return { ...state, isAxoScriptLoaded: action.payload };
|
2024-09-12 14:31:10 +02:00
|
|
|
case 'SET_IS_EMAIL_SUBMITTED':
|
|
|
|
return { ...state, isEmailSubmitted: action.payload };
|
2024-09-13 19:24:23 +02:00
|
|
|
case 'SET_IS_EMAIL_LOOKUP_COMPLETED':
|
|
|
|
return { ...state, isEmailLookupCompleted: action.payload };
|
2024-09-24 02:09:38 +02:00
|
|
|
case 'SET_SHIPPING_ADDRESS':
|
|
|
|
return { ...state, shippingAddress: action.payload };
|
|
|
|
case 'SET_CARD_DETAILS':
|
|
|
|
return { ...state, cardDetails: action.payload };
|
2024-09-24 14:46:12 +02:00
|
|
|
case 'SET_PHONE_NUMBER':
|
|
|
|
return { ...state, phoneNumber: action.payload };
|
2024-09-10 12:03:50 +02:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-09-24 14:46:12 +02:00
|
|
|
// Selectors
|
2024-09-10 12:03:50 +02:00
|
|
|
const selectors = {
|
|
|
|
getIsGuest: ( state ) => state.isGuest,
|
|
|
|
getIsAxoActive: ( state ) => state.isAxoActive,
|
2024-09-13 19:24:23 +02:00
|
|
|
getIsAxoScriptLoaded: ( state ) => state.isAxoScriptLoaded,
|
|
|
|
getIsEmailSubmitted: ( state ) => state.isEmailSubmitted,
|
|
|
|
getIsEmailLookupCompleted: ( state ) => state.isEmailLookupCompleted,
|
2024-09-24 02:09:38 +02:00
|
|
|
getShippingAddress: ( state ) => state.shippingAddress,
|
|
|
|
getCardDetails: ( state ) => state.cardDetails,
|
2024-09-24 14:46:12 +02:00
|
|
|
getPhoneNumber: ( state ) => state.phoneNumber,
|
2024-09-10 12:03:50 +02:00
|
|
|
};
|
|
|
|
|
2024-09-24 14:46:12 +02:00
|
|
|
// Create and register the store
|
2024-09-10 12:03:50 +02:00
|
|
|
const store = createReduxStore( STORE_NAME, {
|
|
|
|
reducer,
|
|
|
|
actions,
|
|
|
|
selectors,
|
|
|
|
} );
|
|
|
|
|
|
|
|
register( store );
|
|
|
|
|
2024-09-24 14:46:12 +02:00
|
|
|
// Action dispatchers
|
2024-09-10 12:03:50 +02:00
|
|
|
export const setIsGuest = ( isGuest ) => {
|
2024-09-24 02:09:38 +02:00
|
|
|
dispatch( STORE_NAME ).setIsGuest( isGuest );
|
2024-09-10 12:03:50 +02:00
|
|
|
};
|
2024-09-13 19:24:23 +02:00
|
|
|
|
|
|
|
export const setIsEmailLookupCompleted = ( isEmailLookupCompleted ) => {
|
2024-09-24 02:09:38 +02:00
|
|
|
dispatch( STORE_NAME ).setIsEmailLookupCompleted( isEmailLookupCompleted );
|
|
|
|
};
|
|
|
|
|
|
|
|
export const setShippingAddress = ( shippingAddress ) => {
|
|
|
|
dispatch( STORE_NAME ).setShippingAddress( shippingAddress );
|
|
|
|
};
|
|
|
|
|
|
|
|
export const setCardDetails = ( cardDetails ) => {
|
|
|
|
dispatch( STORE_NAME ).setCardDetails( cardDetails );
|
2024-09-13 19:24:23 +02:00
|
|
|
};
|
2024-09-24 14:46:12 +02:00
|
|
|
|
|
|
|
export const setPhoneNumber = ( phoneNumber ) => {
|
|
|
|
dispatch( STORE_NAME ).setPhoneNumber( phoneNumber );
|
|
|
|
};
|