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
|
|
|
|
*/
|
|
|
|
|
2025-01-21 15:33:03 +01:00
|
|
|
import { createReducer, createReducerSetters } from '../utils';
|
2024-11-18 18:58:54 +01:00
|
|
|
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(),
|
2025-01-02 13:55:32 +01:00
|
|
|
activeModal: '',
|
2025-01-30 12:54:05 +01:00
|
|
|
activeHighlight: '',
|
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: '',
|
2025-02-10 18:54:09 +01:00
|
|
|
sellerType: 'unknown',
|
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
|
|
|
} ),
|
2025-01-08 16:50:38 +01:00
|
|
|
|
|
|
|
features: Object.freeze( {
|
|
|
|
save_paypal_and_venmo: {
|
|
|
|
enabled: false,
|
|
|
|
},
|
|
|
|
advanced_credit_and_debit_cards: {
|
|
|
|
enabled: false,
|
|
|
|
},
|
|
|
|
apple_pay: {
|
|
|
|
enabled: false,
|
|
|
|
},
|
|
|
|
google_pay: {
|
|
|
|
enabled: false,
|
|
|
|
},
|
2025-01-21 13:04:30 +01:00
|
|
|
alternative_payment_methods: {
|
|
|
|
enabled: false,
|
|
|
|
},
|
|
|
|
pay_later_messaging: {
|
|
|
|
enabled: false,
|
|
|
|
},
|
2025-01-08 16:50:38 +01:00
|
|
|
} ),
|
2025-01-08 16:52:21 +01:00
|
|
|
|
|
|
|
webhooks: Object.freeze( [] ),
|
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-06 19:22:32 +01:00
|
|
|
} );
|
2024-11-18 18:58:54 +01:00
|
|
|
|
|
|
|
// Reducer logic.
|
|
|
|
|
2025-01-21 15:33:03 +01:00
|
|
|
const [ changeTransient, changePersistent ] = createReducerSetters(
|
2024-11-18 18:58:54 +01:00
|
|
|
defaultTransient,
|
|
|
|
defaultPersistent
|
|
|
|
);
|
|
|
|
|
|
|
|
const commonReducer = createReducer( defaultTransient, defaultPersistent, {
|
|
|
|
[ ACTION_TYPES.SET_TRANSIENT ]: ( state, action ) =>
|
2025-01-21 15:33:03 +01:00
|
|
|
changeTransient( state, action ),
|
2024-11-18 18:58:54 +01:00
|
|
|
|
|
|
|
[ ACTION_TYPES.SET_PERSISTENT ]: ( state, action ) =>
|
2025-01-21 15:33:03 +01:00
|
|
|
changePersistent( state, action ),
|
2024-11-20 17:21:09 +01:00
|
|
|
|
2024-12-05 16:22:26 +01:00
|
|
|
[ ACTION_TYPES.RESET ]: ( state ) => {
|
2025-01-21 15:33:03 +01:00
|
|
|
const cleanState = changeTransient(
|
|
|
|
changePersistent( state, defaultPersistent ),
|
2024-12-05 16:22:26 +01:00
|
|
|
defaultTransient
|
|
|
|
);
|
|
|
|
|
|
|
|
// Keep "read-only" details and initialization flags.
|
|
|
|
cleanState.wooSettings = { ...state.wooSettings };
|
2025-01-13 14:42:59 +01:00
|
|
|
cleanState.merchant = { ...state.merchant };
|
|
|
|
cleanState.features = { ...state.features };
|
2024-12-05 16:22:26 +01:00
|
|
|
cleanState.isReady = true;
|
|
|
|
|
|
|
|
return cleanState;
|
|
|
|
},
|
|
|
|
|
2024-12-05 18:55:56 +01:00
|
|
|
[ ACTION_TYPES.START_ACTIVITY ]: ( state, payload ) => {
|
2025-01-21 15:33:03 +01:00
|
|
|
return changeTransient( state, {
|
2024-12-05 18:55:56 +01:00
|
|
|
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 );
|
2025-01-21 15:33:03 +01:00
|
|
|
return changeTransient( state, { activities: newActivities } );
|
2024-12-05 18:55:56 +01:00
|
|
|
},
|
|
|
|
|
2025-02-06 14:21:59 +01:00
|
|
|
// Instantly reset the merchant data and features before refreshing the details.
|
|
|
|
[ ACTION_TYPES.RESET_MERCHANT ]: ( state ) => ( {
|
2024-12-06 19:10:33 +01:00
|
|
|
...state,
|
|
|
|
merchant: Object.freeze( { ...defaultTransient.merchant } ),
|
2025-01-08 16:50:38 +01:00
|
|
|
features: Object.freeze( { ...defaultTransient.features } ),
|
2024-12-06 19:10:33 +01:00
|
|
|
} ),
|
|
|
|
|
2025-02-12 13:24:52 +01:00
|
|
|
[ ACTION_TYPES.SET_MERCHANT ]: ( state, payload ) => {
|
|
|
|
return changePersistent( state, { merchant: payload.merchant } );
|
|
|
|
},
|
|
|
|
|
2024-12-03 15:24:49 -04:00
|
|
|
[ ACTION_TYPES.HYDRATE ]: ( state, payload ) => {
|
2025-01-21 15:33:03 +01:00
|
|
|
const newState = changePersistent( state, payload.data );
|
2024-12-20 09:58:15 +01:00
|
|
|
|
2024-12-06 19:07:44 +01:00
|
|
|
// Populate read-only properties.
|
2025-01-08 16:52:21 +01:00
|
|
|
[ 'wooSettings', 'merchant', 'features', 'webhooks' ].forEach(
|
|
|
|
( key ) => {
|
|
|
|
if ( ! payload[ key ] ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
newState[ key ] = Object.freeze( {
|
|
|
|
...newState[ key ],
|
|
|
|
...payload[ key ],
|
|
|
|
} );
|
2024-12-06 19:07:44 +01:00
|
|
|
}
|
2025-01-08 16:52:21 +01:00
|
|
|
);
|
2024-12-03 15:24:49 -04:00
|
|
|
|
|
|
|
return newState;
|
|
|
|
},
|
2024-11-18 18:58:54 +01:00
|
|
|
} );
|
|
|
|
|
|
|
|
export default commonReducer;
|