2024-11-18 18:58:54 +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';
|
|
|
|
|
|
|
|
// Store structure.
|
|
|
|
|
2024-12-06 19:22:32 +01:00
|
|
|
const defaultTransient = Object.freeze( {
|
2024-11-18 18:58:54 +01:00
|
|
|
isReady: false,
|
2024-12-05 18:55:56 +01:00
|
|
|
activities: new Map(),
|
2024-12-04 12:11:21 +01:00
|
|
|
|
|
|
|
// Read only values, provided by the server via hydrate.
|
2024-12-06 19:22:32 +01:00
|
|
|
merchant: Object.freeze( {
|
2024-12-06 19:07:44 +01:00
|
|
|
isConnected: false,
|
|
|
|
isSandbox: false,
|
|
|
|
id: '',
|
|
|
|
email: '',
|
2025-01-08 15:07:37 +01:00
|
|
|
clientId: '',
|
|
|
|
clientSecret: '',
|
2024-12-06 19:22:32 +01:00
|
|
|
} ),
|
2024-12-06 19:07:44 +01:00
|
|
|
|
2024-12-06 19:22:32 +01:00
|
|
|
wooSettings: Object.freeze( {
|
2024-12-04 12:11:21 +01:00
|
|
|
storeCountry: '',
|
|
|
|
storeCurrency: '',
|
2024-12-06 19:22:32 +01:00
|
|
|
} ),
|
|
|
|
} );
|
2024-11-18 18:58:54 +01:00
|
|
|
|
2024-12-06 19:22:32 +01:00
|
|
|
const defaultPersistent = Object.freeze( {
|
2024-11-18 18:58:54 +01:00
|
|
|
useSandbox: false,
|
|
|
|
useManualConnection: false,
|
2024-12-20 12:38:42 +01:00
|
|
|
webhooks: [],
|
2024-12-06 19:22:32 +01:00
|
|
|
} );
|
2024-11-18 18:58:54 +01:00
|
|
|
|
|
|
|
// Reducer logic.
|
|
|
|
|
|
|
|
const [ setTransient, setPersistent ] = createSetters(
|
|
|
|
defaultTransient,
|
|
|
|
defaultPersistent
|
|
|
|
);
|
|
|
|
|
|
|
|
const commonReducer = createReducer( defaultTransient, defaultPersistent, {
|
|
|
|
[ ACTION_TYPES.SET_TRANSIENT ]: ( state, action ) =>
|
|
|
|
setTransient( state, action ),
|
|
|
|
|
|
|
|
[ ACTION_TYPES.SET_PERSISTENT ]: ( state, action ) =>
|
|
|
|
setPersistent( state, action ),
|
2024-11-20 17:21:09 +01:00
|
|
|
|
2024-12-05 16:22:26 +01:00
|
|
|
[ ACTION_TYPES.RESET ]: ( state ) => {
|
|
|
|
const cleanState = setTransient(
|
|
|
|
setPersistent( state, defaultPersistent ),
|
|
|
|
defaultTransient
|
|
|
|
);
|
|
|
|
|
|
|
|
// Keep "read-only" details and initialization flags.
|
|
|
|
cleanState.wooSettings = { ...state.wooSettings };
|
|
|
|
cleanState.isReady = true;
|
|
|
|
|
|
|
|
return cleanState;
|
|
|
|
},
|
|
|
|
|
2024-12-05 18:55:56 +01:00
|
|
|
[ ACTION_TYPES.START_ACTIVITY ]: ( state, payload ) => {
|
|
|
|
return setTransient( state, {
|
|
|
|
activities: new Map( state.activities ).set(
|
|
|
|
payload.id,
|
|
|
|
payload.description
|
|
|
|
),
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
|
|
|
|
[ ACTION_TYPES.STOP_ACTIVITY ]: ( state, payload ) => {
|
|
|
|
const newActivities = new Map( state.activities );
|
|
|
|
newActivities.delete( payload.id );
|
|
|
|
return setTransient( state, { activities: newActivities } );
|
|
|
|
},
|
|
|
|
|
2024-12-06 19:10:33 +01:00
|
|
|
[ ACTION_TYPES.DO_REFRESH_MERCHANT ]: ( state ) => ( {
|
|
|
|
...state,
|
|
|
|
merchant: Object.freeze( { ...defaultTransient.merchant } ),
|
|
|
|
} ),
|
|
|
|
|
2024-12-03 15:24:49 -04:00
|
|
|
[ ACTION_TYPES.HYDRATE ]: ( state, payload ) => {
|
|
|
|
const newState = setPersistent( state, payload.data );
|
2024-12-20 09:58:15 +01:00
|
|
|
|
2024-12-06 19:07:44 +01:00
|
|
|
// Populate read-only properties.
|
2024-12-20 09:58:15 +01:00
|
|
|
[ 'wooSettings', 'merchant' ].forEach( ( key ) => {
|
2024-12-06 19:07:44 +01:00
|
|
|
if ( ! payload[ key ] ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
newState[ key ] = Object.freeze( {
|
|
|
|
...newState[ key ],
|
|
|
|
...payload[ key ],
|
|
|
|
} );
|
|
|
|
} );
|
2024-12-03 15:24:49 -04:00
|
|
|
|
|
|
|
return newState;
|
|
|
|
},
|
2024-11-18 18:58:54 +01:00
|
|
|
} );
|
|
|
|
|
|
|
|
export default commonReducer;
|