woocommerce-paypal-payments/modules/ppcp-settings/resources/js/data/styling/reducer.js

90 lines
2.1 KiB
JavaScript
Raw Normal View History

/**
* 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, createSetters } 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( {
2025-01-14 10:28:10 +01:00
cart: {
enabled: true,
methods: [ 'venmo', 'applepay', 'googlepay', 'credit card' ],
shape: 'rect',
label: 'Pay',
color: 'gold',
},
'classic-checkout': {
enabled: true,
methods: [ 'venmo', 'applepay', 'googlepay', 'credit card' ],
shape: 'rect',
label: 'Checkout',
color: 'gold',
},
'express-checkout': {
enabled: true,
methods: [ 'venmo', 'applepay', 'googlepay', 'credit card' ],
shape: 'rect',
label: 'Checkout',
color: 'gold',
},
'mini-cart': {
enabled: true,
methods: [ 'venmo', 'applepay', 'googlepay', 'credit card' ],
shape: 'rect',
label: 'Pay',
color: 'gold',
},
product: {
enabled: true,
methods: [ 'venmo', 'applepay', 'googlepay', 'credit card' ],
shape: 'rect',
label: 'Buy',
color: 'gold',
},
} );
// Reducer logic.
const [ setTransient, setPersistent ] = createSetters(
defaultTransient,
defaultPersistent
);
const reducer = createReducer( defaultTransient, defaultPersistent, {
[ ACTION_TYPES.SET_TRANSIENT ]: ( state, payload ) =>
setTransient( state, payload ),
[ ACTION_TYPES.SET_PERSISTENT ]: ( state, payload ) =>
setPersistent( state, payload ),
[ ACTION_TYPES.RESET ]: ( state ) => {
const cleanState = setTransient(
setPersistent( state, defaultPersistent ),
defaultTransient
);
// Keep "read-only" details and initialization flags.
cleanState.isReady = true;
return cleanState;
},
[ ACTION_TYPES.HYDRATE ]: ( state, payload ) =>
setPersistent( state, payload.data ),
} );
export default reducer;