/** * 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( { enabled: true, methods: [], label: STYLING_LABELS.pay.value, shape: STYLING_SHAPES.rect.value, color: STYLING_COLORS.gold.value, } ), [ STYLING_LOCATIONS[ 'classic-checkout' ].value ]: Object.freeze( { 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( { enabled: true, methods: [], label: STYLING_LABELS.checkout.value, shape: STYLING_SHAPES.rect.value, color: STYLING_COLORS.gold.value, } ), [ STYLING_LOCATIONS[ 'mini-cart' ].value ]: Object.freeze( { 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( { 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;