mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 12:25:15 +08:00
60 lines
1.5 KiB
JavaScript
60 lines
1.5 KiB
JavaScript
/**
|
|
* 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
|
|
*/
|
|
|
|
import { createReducer, createReducerSetters } from '../utils';
|
|
import ACTION_TYPES from './action-types';
|
|
|
|
// Store structure.
|
|
|
|
// Transient: Values that are _not_ saved to the DB (like app lifecycle-flags).
|
|
const defaultTransient = Object.freeze( {
|
|
isReady: false,
|
|
} );
|
|
|
|
// Persistent: Values that are loaded from the DB.
|
|
const defaultPersistent = Object.freeze( {
|
|
cart: {},
|
|
checkout: {},
|
|
product: {},
|
|
shop: {},
|
|
home: {},
|
|
custom_placement: [],
|
|
} );
|
|
|
|
// Reducer logic.
|
|
|
|
const [ changeTransient, changePersistent ] = createReducerSetters(
|
|
defaultTransient,
|
|
defaultPersistent
|
|
);
|
|
|
|
const reducer = createReducer( defaultTransient, defaultPersistent, {
|
|
[ ACTION_TYPES.SET_TRANSIENT ]: ( state, payload ) =>
|
|
changeTransient( state, payload ),
|
|
|
|
[ ACTION_TYPES.SET_PERSISTENT ]: ( state, payload ) =>
|
|
changePersistent( state, payload ),
|
|
|
|
[ ACTION_TYPES.RESET ]: ( state ) => {
|
|
const cleanState = changeTransient(
|
|
changePersistent( state, defaultPersistent ),
|
|
defaultTransient
|
|
);
|
|
|
|
// Keep "read-only" details and initialization flags.
|
|
cleanState.isReady = true;
|
|
|
|
return cleanState;
|
|
},
|
|
|
|
[ ACTION_TYPES.HYDRATE ]: ( state, payload ) =>
|
|
changePersistent( state, payload.data ),
|
|
} );
|
|
|
|
export default reducer;
|