2024-11-18 18:51:11 +01:00
|
|
|
/**
|
|
|
|
* Reducer: Defines store structure and state updates for this module.
|
|
|
|
*
|
|
|
|
* Manages both transient (temporary) and persistent (saved) state.
|
|
|
|
* The initial state must define all properties, as dynamic additions are not supported.
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*/
|
|
|
|
|
2025-01-21 15:33:03 +01:00
|
|
|
import { createReducer, createReducerSetters } from '../utils';
|
2024-10-22 15:15:55 +02:00
|
|
|
import ACTION_TYPES from './action-types';
|
|
|
|
|
2024-11-18 17:43:15 +01:00
|
|
|
// Store structure.
|
|
|
|
|
2024-12-06 19:22:32 +01:00
|
|
|
const defaultTransient = Object.freeze( {
|
2024-10-28 18:57:39 +01:00
|
|
|
isReady: false,
|
2025-01-08 14:41:27 +01:00
|
|
|
manualClientId: '',
|
|
|
|
manualClientSecret: '',
|
2024-10-30 16:52:06 +01:00
|
|
|
|
|
|
|
// Read only values, provided by the server.
|
2024-12-06 19:22:32 +01:00
|
|
|
flags: Object.freeze( {
|
2024-10-25 13:01:01 +02:00
|
|
|
canUseCasualSelling: false,
|
|
|
|
canUseVaulting: false,
|
|
|
|
canUseCardPayments: false,
|
2024-12-05 10:33:34 -04:00
|
|
|
canUseSubscriptions: false,
|
2025-02-25 14:50:09 +01:00
|
|
|
shouldSkipPaymentMethods: false,
|
2024-12-06 19:22:32 +01:00
|
|
|
} ),
|
|
|
|
} );
|
2024-10-22 15:15:55 +02:00
|
|
|
|
2024-12-06 19:22:32 +01:00
|
|
|
const defaultPersistent = Object.freeze( {
|
2024-11-18 17:43:15 +01:00
|
|
|
completed: false,
|
|
|
|
step: 0,
|
|
|
|
isCasualSeller: null, // null value will uncheck both options in the UI.
|
2024-12-05 09:33:24 -04:00
|
|
|
areOptionalPaymentMethodsEnabled: null,
|
2024-11-18 17:43:15 +01:00
|
|
|
products: [],
|
2024-12-06 19:22:32 +01:00
|
|
|
} );
|
2024-11-18 17:43:15 +01:00
|
|
|
|
|
|
|
// Reducer logic.
|
|
|
|
|
2025-01-21 15:33:03 +01:00
|
|
|
const [ changeTransient, changePersistent ] = createReducerSetters(
|
2024-11-18 17:43:15 +01:00
|
|
|
defaultTransient,
|
|
|
|
defaultPersistent
|
|
|
|
);
|
|
|
|
|
2024-11-18 18:26:10 +01:00
|
|
|
const onboardingReducer = createReducer( defaultTransient, defaultPersistent, {
|
2024-11-20 16:53:46 +01:00
|
|
|
[ ACTION_TYPES.SET_TRANSIENT ]: ( state, payload ) =>
|
2025-01-21 15:33:03 +01:00
|
|
|
changeTransient( state, payload ),
|
2024-10-30 16:52:06 +01:00
|
|
|
|
2024-11-20 16:53:46 +01:00
|
|
|
[ ACTION_TYPES.SET_PERSISTENT ]: ( state, payload ) =>
|
2025-01-21 15:33:03 +01:00
|
|
|
changePersistent( state, payload ),
|
2024-10-28 18:56:25 +01:00
|
|
|
|
2024-12-05 16:22:26 +01:00
|
|
|
[ ACTION_TYPES.RESET ]: ( state ) => {
|
2025-01-21 15:33:03 +01:00
|
|
|
const cleanState = changeTransient(
|
|
|
|
changePersistent( state, defaultPersistent ),
|
2024-12-05 16:22:26 +01:00
|
|
|
defaultTransient
|
|
|
|
);
|
|
|
|
|
|
|
|
// Keep "read-only" details and initialization flags.
|
|
|
|
cleanState.flags = { ...state.flags };
|
|
|
|
cleanState.isReady = true;
|
|
|
|
|
|
|
|
return cleanState;
|
|
|
|
},
|
2024-10-28 18:27:57 +01:00
|
|
|
|
2024-11-20 16:53:46 +01:00
|
|
|
[ ACTION_TYPES.HYDRATE ]: ( state, payload ) => {
|
2025-01-21 15:33:03 +01:00
|
|
|
const newState = changePersistent( state, payload.data );
|
2024-10-23 18:15:37 +02:00
|
|
|
|
2025-01-21 15:33:03 +01:00
|
|
|
// Flags are not updated by `changePersistent()`.
|
2024-11-18 18:26:10 +01:00
|
|
|
if ( payload.flags ) {
|
2024-12-06 19:22:32 +01:00
|
|
|
newState.flags = Object.freeze( {
|
|
|
|
...newState.flags,
|
|
|
|
...payload.flags,
|
|
|
|
} );
|
2024-11-18 18:26:10 +01:00
|
|
|
}
|
2024-10-23 18:15:37 +02:00
|
|
|
|
2024-11-18 18:26:10 +01:00
|
|
|
return newState;
|
|
|
|
},
|
|
|
|
} );
|
2024-10-22 15:15:55 +02:00
|
|
|
|
|
|
|
export default onboardingReducer;
|