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

108 lines
2.9 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';
import {
STYLING_COLORS,
STYLING_LABELS,
STYLING_LAYOUTS,
STYLING_LOCATIONS,
STYLING_SHAPES,
} from './configuration';
// Store structure.
// Transient: Values that are _not_ saved to the DB (like app lifecycle-flags).
const defaultTransient = Object.freeze( {
isReady: false,
location: STYLING_LOCATIONS.cart.value, // Which location is selected in the Styling tab.
} );
// Persistent: Values that are loaded from the DB.
const defaultPersistent = Object.freeze( {
[ STYLING_LOCATIONS.cart.value ]: Object.freeze( {
2025-01-14 10:28:10 +01:00
enabled: true,
methods: [],
label: STYLING_LABELS.pay.value,
shape: STYLING_SHAPES.rect.value,
color: STYLING_COLORS.gold.value,
layout: STYLING_LAYOUTS.vertical.value,
tagline: false,
} ),
[ STYLING_LOCATIONS[ 'classic-checkout' ].value ]: Object.freeze( {
2025-01-14 10:28:10 +01:00
enabled: true,
methods: [],
label: STYLING_LABELS.checkout.value,
shape: STYLING_SHAPES.rect.value,
color: STYLING_COLORS.gold.value,
layout: STYLING_LAYOUTS.vertical.value,
tagline: false,
} ),
[ STYLING_LOCATIONS[ 'express-checkout' ].value ]: Object.freeze( {
2025-01-14 10:28:10 +01:00
enabled: true,
methods: [],
label: STYLING_LABELS.checkout.value,
shape: STYLING_SHAPES.rect.value,
color: STYLING_COLORS.gold.value,
layout: STYLING_LAYOUTS.vertical.value,
tagline: false,
} ),
[ STYLING_LOCATIONS[ 'mini-cart' ].value ]: Object.freeze( {
2025-01-14 10:28:10 +01:00
enabled: true,
methods: [],
label: STYLING_LABELS.pay.value,
shape: STYLING_SHAPES.rect.value,
color: STYLING_COLORS.gold.value,
layout: STYLING_LAYOUTS.vertical.value,
tagline: false,
} ),
[ STYLING_LOCATIONS.product.value ]: Object.freeze( {
2025-01-14 10:28:10 +01:00
enabled: true,
methods: [],
label: STYLING_LABELS.buynow.value,
shape: STYLING_SHAPES.rect.value,
color: STYLING_COLORS.gold.value,
layout: STYLING_LAYOUTS.vertical.value,
tagline: false,
} ),
} );
// 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;