2024-11-18 18:58:54 +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
|
|
|
|
*/
|
|
|
|
|
2024-12-17 12:45:54 +01:00
|
|
|
import { dispatch, select } from '@wordpress/data';
|
2024-11-21 16:16:20 +01:00
|
|
|
|
2024-11-18 18:58:54 +01:00
|
|
|
import ACTION_TYPES from './action-types';
|
2024-11-21 16:16:20 +01:00
|
|
|
import { STORE_NAME } from './constants';
|
2024-11-18 18:58:54 +01:00
|
|
|
|
2024-11-20 17:21:09 +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-12-05 16:22:26 +01:00
|
|
|
/**
|
|
|
|
* Special. Resets all values in the onboarding store to initial defaults.
|
|
|
|
*
|
|
|
|
* @return {Action} The action.
|
|
|
|
*/
|
|
|
|
export const reset = () => ( { type: ACTION_TYPES.RESET } );
|
|
|
|
|
2024-11-21 17:43:22 +01:00
|
|
|
/**
|
|
|
|
* Persistent. Set the full onboarding details, usually during app initialization.
|
|
|
|
*
|
|
|
|
* @param {{data: {}, flags?: {}}} payload
|
|
|
|
* @return {Action} The action.
|
|
|
|
*/
|
|
|
|
export const hydrate = ( payload ) => ( {
|
|
|
|
type: ACTION_TYPES.HYDRATE,
|
|
|
|
payload,
|
|
|
|
} );
|
|
|
|
|
2024-11-18 18:58:54 +01:00
|
|
|
/**
|
|
|
|
* Transient. Marks the onboarding details as "ready", i.e., fully initialized.
|
|
|
|
*
|
|
|
|
* @param {boolean} isReady
|
2024-11-20 17:21:09 +01:00
|
|
|
* @return {Action} The action.
|
2024-11-18 18:58:54 +01:00
|
|
|
*/
|
2024-11-20 17:21:09 +01:00
|
|
|
export const setIsReady = ( isReady ) => ( {
|
|
|
|
type: ACTION_TYPES.SET_TRANSIENT,
|
|
|
|
payload: { isReady },
|
|
|
|
} );
|
2024-11-18 18:58:54 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Transient. Changes the "saving" flag.
|
|
|
|
*
|
|
|
|
* @param {boolean} isSaving
|
2024-11-20 17:21:09 +01:00
|
|
|
* @return {Action} The action.
|
2024-11-18 18:58:54 +01:00
|
|
|
*/
|
2024-11-20 17:21:09 +01:00
|
|
|
export const setIsSaving = ( isSaving ) => ( {
|
|
|
|
type: ACTION_TYPES.SET_TRANSIENT,
|
|
|
|
payload: { isSaving },
|
|
|
|
} );
|
|
|
|
|
|
|
|
/**
|
2024-12-05 18:55:56 +01:00
|
|
|
* Transient (Activity): Marks the start of an async activity
|
|
|
|
* Think of it as "setIsBusy(true)"
|
2024-11-20 17:21:09 +01:00
|
|
|
*
|
2024-12-05 18:55:56 +01:00
|
|
|
* @param {string} id Internal ID/key of the action, used to stop it again.
|
|
|
|
* @param {?string} description Optional, description for logging/debugging
|
|
|
|
* @return {?Action} The action.
|
|
|
|
*/
|
|
|
|
export const startActivity = ( id, description = null ) => {
|
|
|
|
if ( ! id || 'string' !== typeof id ) {
|
|
|
|
console.warn( 'Activity ID must be a non-empty string' );
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: ACTION_TYPES.START_ACTIVITY,
|
|
|
|
payload: { id, description },
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transient (Activity): Marks the end of an async activity.
|
|
|
|
* Think of it as "setIsBusy(false)"
|
|
|
|
*
|
|
|
|
* @param {string} id Internal ID/key of the action, used to stop it again.
|
2024-11-20 17:21:09 +01:00
|
|
|
* @return {Action} The action.
|
|
|
|
*/
|
2024-12-05 18:55:56 +01:00
|
|
|
export const stopActivity = ( id ) => ( {
|
|
|
|
type: ACTION_TYPES.STOP_ACTIVITY,
|
|
|
|
payload: { id },
|
2024-11-20 17:21:09 +01:00
|
|
|
} );
|
2024-11-18 18:58:54 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Persistent. Sets the sandbox mode on or off.
|
|
|
|
*
|
|
|
|
* @param {boolean} useSandbox
|
2024-11-20 17:21:09 +01:00
|
|
|
* @return {Action} The action.
|
2024-11-18 18:58:54 +01:00
|
|
|
*/
|
2024-11-20 17:21:09 +01:00
|
|
|
export const setSandboxMode = ( useSandbox ) => ( {
|
|
|
|
type: ACTION_TYPES.SET_PERSISTENT,
|
|
|
|
payload: { useSandbox },
|
|
|
|
} );
|
2024-11-18 18:58:54 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Persistent. Toggles the "Manual Connection" mode on or off.
|
|
|
|
*
|
|
|
|
* @param {boolean} useManualConnection
|
2024-11-20 17:21:09 +01:00
|
|
|
* @return {Action} The action.
|
2024-11-18 18:58:54 +01:00
|
|
|
*/
|
2024-11-20 17:21:09 +01:00
|
|
|
export const setManualConnectionMode = ( useManualConnection ) => ( {
|
|
|
|
type: ACTION_TYPES.SET_PERSISTENT,
|
|
|
|
payload: { useManualConnection },
|
|
|
|
} );
|
2024-11-18 18:58:54 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Persistent. Changes the "client ID" value.
|
|
|
|
*
|
|
|
|
* @param {string} clientId
|
2024-11-20 17:21:09 +01:00
|
|
|
* @return {Action} The action.
|
2024-11-18 18:58:54 +01:00
|
|
|
*/
|
2024-11-20 17:21:09 +01:00
|
|
|
export const setClientId = ( clientId ) => ( {
|
|
|
|
type: ACTION_TYPES.SET_PERSISTENT,
|
|
|
|
payload: { clientId },
|
|
|
|
} );
|
2024-11-18 18:58:54 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Persistent. Changes the "client secret" value.
|
|
|
|
*
|
|
|
|
* @param {string} clientSecret
|
2024-11-20 17:21:09 +01:00
|
|
|
* @return {Action} The action.
|
2024-11-18 18:58:54 +01:00
|
|
|
*/
|
2024-11-20 17:21:09 +01:00
|
|
|
export const setClientSecret = ( clientSecret ) => ( {
|
|
|
|
type: ACTION_TYPES.SET_PERSISTENT,
|
|
|
|
payload: { clientSecret },
|
|
|
|
} );
|
2024-11-18 18:58:54 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Side effect. Saves the persistent details to the WP database.
|
|
|
|
*
|
2024-11-20 17:21:09 +01:00
|
|
|
* @return {Action} The action.
|
2024-11-18 18:58:54 +01:00
|
|
|
*/
|
2024-11-20 17:21:09 +01:00
|
|
|
export const persist = function* () {
|
2024-11-21 17:42:48 +01:00
|
|
|
const data = yield select( STORE_NAME ).persistentData();
|
|
|
|
|
|
|
|
yield { type: ACTION_TYPES.DO_PERSIST_DATA, data };
|
2024-11-20 17:21:09 +01:00
|
|
|
};
|
2024-11-21 16:16:20 +01:00
|
|
|
|
2024-11-21 19:07:32 +01:00
|
|
|
/**
|
2024-12-05 18:56:23 +01:00
|
|
|
* Side effect. Fetches the ISU-login URL for a sandbox account.
|
2024-11-21 19:07:32 +01:00
|
|
|
*
|
|
|
|
* @return {Action} The action.
|
|
|
|
*/
|
2024-12-03 17:54:09 +01:00
|
|
|
export const connectToSandbox = function* () {
|
2024-12-05 18:56:23 +01:00
|
|
|
return yield { type: ACTION_TYPES.DO_SANDBOX_LOGIN };
|
2024-11-21 19:07:32 +01:00
|
|
|
};
|
|
|
|
|
2024-12-03 16:28:28 +01:00
|
|
|
/**
|
2024-12-05 18:56:23 +01:00
|
|
|
* Side effect. Fetches the ISU-login URL for a production account.
|
2024-12-03 16:28:28 +01:00
|
|
|
*
|
2024-12-03 17:54:09 +01:00
|
|
|
* @param {string[]} products Which products/features to display in the ISU popup.
|
2024-12-03 16:28:28 +01:00
|
|
|
* @return {Action} The action.
|
|
|
|
*/
|
2024-12-03 17:54:09 +01:00
|
|
|
export const connectToProduction = function* ( products = [] ) {
|
2024-12-05 18:56:23 +01:00
|
|
|
return yield { type: ACTION_TYPES.DO_PRODUCTION_LOGIN, products };
|
2024-12-03 16:28:28 +01:00
|
|
|
};
|
|
|
|
|
2024-11-21 16:16:20 +01:00
|
|
|
/**
|
|
|
|
* Side effect. Initiates a manual connection attempt using the provided client ID and secret.
|
|
|
|
*
|
|
|
|
* @return {Action} The action.
|
|
|
|
*/
|
|
|
|
export const connectViaIdAndSecret = function* () {
|
|
|
|
const { clientId, clientSecret, useSandbox } =
|
|
|
|
yield select( STORE_NAME ).persistentData();
|
|
|
|
|
2024-12-05 18:56:23 +01:00
|
|
|
return yield {
|
2024-11-21 16:16:20 +01:00
|
|
|
type: ACTION_TYPES.DO_MANUAL_CONNECTION,
|
|
|
|
clientId,
|
|
|
|
clientSecret,
|
|
|
|
useSandbox,
|
|
|
|
};
|
|
|
|
};
|
2024-12-06 19:10:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Side effect. Clears and refreshes the merchant data via a REST request.
|
|
|
|
*
|
|
|
|
* @return {Action} The action.
|
|
|
|
*/
|
|
|
|
export const refreshMerchantData = function* () {
|
2024-12-17 15:05:34 +01:00
|
|
|
const result = yield { type: ACTION_TYPES.DO_REFRESH_MERCHANT };
|
|
|
|
|
|
|
|
if ( result.success && result.merchant ) {
|
|
|
|
yield hydrate( result );
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2024-12-06 19:10:33 +01:00
|
|
|
};
|
2024-12-16 13:12:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Side effect.
|
2024-12-17 12:45:54 +01:00
|
|
|
* Purges all feature status data via a REST request.
|
2024-12-16 13:12:38 +01:00
|
|
|
* Refreshes the merchant data via a REST request.
|
|
|
|
*
|
|
|
|
* @return {Action} The action.
|
|
|
|
*/
|
|
|
|
export const refreshFeatureStatuses = function* () {
|
2024-12-17 12:45:54 +01:00
|
|
|
const result = yield { type: ACTION_TYPES.DO_REFRESH_FEATURES };
|
|
|
|
|
|
|
|
if ( result && result.success ) {
|
2024-12-17 15:05:34 +01:00
|
|
|
// TODO: Review if we can get the updated feature details in the result.data instead of
|
|
|
|
// doing a second refreshMerchantData() request.
|
|
|
|
yield refreshMerchantData();
|
2024-12-17 12:45:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2024-12-16 13:12:38 +01:00
|
|
|
};
|