2024-11-18 18:51:11 +01:00
|
|
|
/**
|
|
|
|
* Action Creators: Define functions to create action objects.
|
|
|
|
*
|
|
|
|
* These functions update state or trigger side effects (e.g., async operations).
|
|
|
|
* Actions are categorized as Transient, Persistent, or Side effect.
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*/
|
|
|
|
|
2025-01-28 12:21:48 +01:00
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
2024-11-20 16:54:29 +01:00
|
|
|
|
2024-10-22 15:15:55 +02:00
|
|
|
import ACTION_TYPES from './action-types';
|
2025-01-28 12:21:48 +01:00
|
|
|
import { REST_PERSIST_PATH } from './constants';
|
2024-11-18 16:31:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} Action An action object that is handled by a reducer or control.
|
|
|
|
* @property {string} type - The action type.
|
|
|
|
* @property {Object?} payload - Optional payload for the action.
|
|
|
|
*/
|
2024-10-22 15:15:55 +02:00
|
|
|
|
2024-10-30 16:55:44 +01:00
|
|
|
/**
|
|
|
|
* Special. Resets all values in the onboarding store to initial defaults.
|
|
|
|
*
|
2024-11-18 18:26:10 +01:00
|
|
|
* @return {Action} The action.
|
2024-10-30 16:55:44 +01:00
|
|
|
*/
|
2024-11-20 16:54:29 +01:00
|
|
|
export const reset = () => ( { type: ACTION_TYPES.RESET } );
|
2024-10-30 16:55:44 +01:00
|
|
|
|
2024-10-28 18:57:39 +01:00
|
|
|
/**
|
2024-11-21 17:42:48 +01:00
|
|
|
* Persistent. Set the full onboarding details, usually during app initialization.
|
2024-10-28 18:57:39 +01:00
|
|
|
*
|
2024-11-21 17:42:48 +01:00
|
|
|
* @param {{data: {}, flags?: {}}} payload
|
2024-11-18 18:26:10 +01:00
|
|
|
* @return {Action} The action.
|
2024-10-28 18:57:39 +01:00
|
|
|
*/
|
2024-11-21 17:42:48 +01:00
|
|
|
export const hydrate = ( payload ) => ( {
|
|
|
|
type: ACTION_TYPES.HYDRATE,
|
|
|
|
payload,
|
2024-11-20 16:54:29 +01:00
|
|
|
} );
|
2024-10-28 18:57:39 +01:00
|
|
|
|
2024-10-28 17:00:07 +01:00
|
|
|
/**
|
2025-01-17 19:25:44 +01:00
|
|
|
* Generic transient-data updater.
|
2024-10-28 17:00:07 +01:00
|
|
|
*
|
2025-01-17 19:25:44 +01:00
|
|
|
* @param {string} prop Name of the property to update.
|
|
|
|
* @param {any} value The new value of the property.
|
2024-11-18 18:26:10 +01:00
|
|
|
* @return {Action} The action.
|
2024-10-28 17:00:07 +01:00
|
|
|
*/
|
2025-01-17 19:25:44 +01:00
|
|
|
export const setTransient = ( prop, value ) => ( {
|
2024-11-21 17:42:48 +01:00
|
|
|
type: ACTION_TYPES.SET_TRANSIENT,
|
2025-01-17 19:25:44 +01:00
|
|
|
payload: { [ prop ]: value },
|
2024-11-20 16:54:29 +01:00
|
|
|
} );
|
2024-10-28 17:00:07 +01:00
|
|
|
|
2025-01-17 19:25:44 +01:00
|
|
|
/**
|
|
|
|
* Generic persistent-data updater.
|
|
|
|
*
|
|
|
|
* @param {string} prop Name of the property to update.
|
|
|
|
* @param {any} value The new value of the property.
|
|
|
|
* @return {Action} The action.
|
|
|
|
*/
|
|
|
|
export const setPersistent = ( prop, value ) => ( {
|
|
|
|
type: ACTION_TYPES.SET_PERSISTENT,
|
|
|
|
payload: { [ prop ]: value },
|
|
|
|
} );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transient. Marks the onboarding details as "ready", i.e., fully initialized.
|
|
|
|
*
|
|
|
|
* @param {boolean} isReady
|
|
|
|
* @return {Action} The action.
|
|
|
|
*/
|
|
|
|
export const setIsReady = ( isReady ) => setTransient( 'isReady', isReady );
|
|
|
|
|
2024-11-04 16:50:50 +01:00
|
|
|
/**
|
2025-01-28 12:21:48 +01:00
|
|
|
* Thunk action creator. Triggers the persistence of onboarding data to the server.
|
2024-11-04 16:50:50 +01:00
|
|
|
*
|
2025-01-28 12:21:48 +01:00
|
|
|
* @return {Function} The thunk function.
|
2024-11-18 16:31:05 +01:00
|
|
|
*/
|
2025-01-28 12:21:48 +01:00
|
|
|
export function persist() {
|
|
|
|
return async ( { select } ) => {
|
|
|
|
try {
|
|
|
|
await apiFetch( {
|
|
|
|
path: REST_PERSIST_PATH,
|
|
|
|
method: 'POST',
|
2025-02-07 12:07:52 +01:00
|
|
|
data: select.persistentData(),
|
2025-01-28 12:21:48 +01:00
|
|
|
} );
|
|
|
|
} catch ( e ) {
|
2025-02-07 12:07:52 +01:00
|
|
|
// We catch errors here, as the onboarding module is not handled by the persistAll hook.
|
2025-01-28 12:21:48 +01:00
|
|
|
console.error( 'Error saving progress.', e );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2025-02-17 13:18:34 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Thunk action creator. Forces a data refresh from the REST API, replacing the current Redux values.
|
|
|
|
*
|
|
|
|
* @return {Function} The thunk function.
|
|
|
|
*/
|
|
|
|
export function refresh() {
|
|
|
|
return ( { dispatch, select } ) => {
|
|
|
|
dispatch.invalidateResolutionForStore();
|
|
|
|
|
|
|
|
select.persistentData();
|
|
|
|
};
|
|
|
|
}
|
2025-03-20 15:17:46 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Persistent. Updates the gateway synced status.
|
|
|
|
*
|
|
|
|
* @param {boolean} synced The sync status to set
|
|
|
|
* @return {Action} The action.
|
|
|
|
*/
|
|
|
|
export const updateGatewaysSynced = ( synced = true ) =>
|
|
|
|
setPersistent( 'gatewaysSynced', synced );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Persistent. Updates the gateway refreshed status.
|
|
|
|
*
|
|
|
|
* @param {boolean} refreshed The refreshed status to set
|
|
|
|
* @return {Action} The action.
|
|
|
|
*/
|
|
|
|
export const updateGatewaysRefreshed = ( refreshed = true ) =>
|
|
|
|
setPersistent( 'gatewaysRefreshed', refreshed );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Action creator to sync payment gateways.
|
|
|
|
* This will both update the state and persist it.
|
|
|
|
*
|
|
|
|
* @return {Function} The thunk function.
|
|
|
|
*/
|
|
|
|
export function syncGateways() {
|
|
|
|
return async ( { dispatch } ) => {
|
2025-03-20 21:58:32 +01:00
|
|
|
dispatch( setPersistent( 'gatewaysSynced', true ) );
|
2025-03-20 15:17:46 +01:00
|
|
|
await dispatch( persist() );
|
|
|
|
return { success: true };
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function refreshGateways() {
|
|
|
|
return async ( { dispatch } ) => {
|
2025-03-20 21:58:32 +01:00
|
|
|
dispatch( setPersistent( 'gatewaysRefreshed', true ) );
|
2025-03-20 15:17:46 +01:00
|
|
|
await dispatch( persist() );
|
|
|
|
return { success: true };
|
|
|
|
};
|
|
|
|
}
|