2025-01-13 17:06:05 +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
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { createReducer, createSetters } from '../utils';
|
|
|
|
import ACTION_TYPES from './action-types';
|
2025-01-16 20:04:20 +01:00
|
|
|
import {
|
|
|
|
STYLING_COLORS,
|
|
|
|
STYLING_LABELS,
|
|
|
|
STYLING_LAYOUTS,
|
|
|
|
STYLING_LOCATIONS,
|
|
|
|
STYLING_SHAPES,
|
|
|
|
} from './configuration';
|
2025-01-13 17:06:05 +01:00
|
|
|
|
|
|
|
// Store structure.
|
|
|
|
|
|
|
|
// Transient: Values that are _not_ saved to the DB (like app lifecycle-flags).
|
|
|
|
const defaultTransient = Object.freeze( {
|
|
|
|
isReady: false,
|
2025-01-16 20:04:20 +01:00
|
|
|
location: STYLING_LOCATIONS.cart.value, // Which location is selected in the Styling tab.
|
2025-01-13 17:06:05 +01:00
|
|
|
} );
|
|
|
|
|
|
|
|
// Persistent: Values that are loaded from the DB.
|
|
|
|
const defaultPersistent = Object.freeze( {
|
2025-01-16 20:04:20 +01:00
|
|
|
[ STYLING_LOCATIONS.cart.value ]: Object.freeze( {
|
2025-01-14 10:28:10 +01:00
|
|
|
enabled: true,
|
2025-01-14 16:19:18 +01:00
|
|
|
methods: [],
|
2025-01-16 20:04:20 +01:00
|
|
|
label: STYLING_LABELS.pay.value,
|
2025-01-14 16:19:18 +01:00
|
|
|
shape: STYLING_SHAPES.rect.value,
|
|
|
|
color: STYLING_COLORS.gold.value,
|
2025-01-16 20:04:20 +01:00
|
|
|
} ),
|
2025-01-17 16:34:06 +01:00
|
|
|
[ STYLING_LOCATIONS.classicCheckout.value ]: Object.freeze( {
|
2025-01-14 10:28:10 +01:00
|
|
|
enabled: true,
|
2025-01-14 16:19:18 +01:00
|
|
|
methods: [],
|
2025-01-16 20:04:20 +01:00
|
|
|
label: STYLING_LABELS.checkout.value,
|
2025-01-14 16:19:18 +01:00
|
|
|
shape: STYLING_SHAPES.rect.value,
|
|
|
|
color: STYLING_COLORS.gold.value,
|
2025-01-16 20:04:20 +01:00
|
|
|
layout: STYLING_LAYOUTS.vertical.value,
|
|
|
|
tagline: false,
|
|
|
|
} ),
|
2025-01-17 16:34:06 +01:00
|
|
|
[ STYLING_LOCATIONS.expressCheckout.value ]: Object.freeze( {
|
2025-01-14 10:28:10 +01:00
|
|
|
enabled: true,
|
2025-01-14 16:19:18 +01:00
|
|
|
methods: [],
|
2025-01-16 20:04:20 +01:00
|
|
|
label: STYLING_LABELS.checkout.value,
|
2025-01-14 16:19:18 +01:00
|
|
|
shape: STYLING_SHAPES.rect.value,
|
|
|
|
color: STYLING_COLORS.gold.value,
|
2025-01-16 20:04:20 +01:00
|
|
|
} ),
|
2025-01-17 16:34:06 +01:00
|
|
|
[ STYLING_LOCATIONS.miniCart.value ]: Object.freeze( {
|
2025-01-14 10:28:10 +01:00
|
|
|
enabled: true,
|
2025-01-14 16:19:18 +01:00
|
|
|
methods: [],
|
2025-01-16 20:04:20 +01:00
|
|
|
label: STYLING_LABELS.pay.value,
|
2025-01-14 16:19:18 +01:00
|
|
|
shape: STYLING_SHAPES.rect.value,
|
|
|
|
color: STYLING_COLORS.gold.value,
|
2025-01-16 20:04:20 +01:00
|
|
|
layout: STYLING_LAYOUTS.vertical.value,
|
|
|
|
tagline: false,
|
|
|
|
} ),
|
|
|
|
[ STYLING_LOCATIONS.product.value ]: Object.freeze( {
|
2025-01-14 10:28:10 +01:00
|
|
|
enabled: true,
|
2025-01-14 16:19:18 +01:00
|
|
|
methods: [],
|
2025-01-16 20:04:20 +01:00
|
|
|
label: STYLING_LABELS.buynow.value,
|
2025-01-14 16:19:18 +01:00
|
|
|
shape: STYLING_SHAPES.rect.value,
|
|
|
|
color: STYLING_COLORS.gold.value,
|
2025-01-16 20:04:20 +01:00
|
|
|
layout: STYLING_LAYOUTS.vertical.value,
|
|
|
|
tagline: false,
|
|
|
|
} ),
|
2025-01-13 17:06:05 +01:00
|
|
|
} );
|
|
|
|
|
|
|
|
// 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;
|