woocommerce-paypal-payments/modules/ppcp-settings/resources/js/data/onboarding/actions.js

148 lines
3.8 KiB
JavaScript
Raw Normal View History

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
*/
2024-11-20 16:54:29 +01:00
import { select } from '@wordpress/data';
import ACTION_TYPES from './action-types';
2024-11-20 16:54:29 +01:00
import { STORE_NAME } from './constants';
/**
* @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.
*/
/**
* Special. Resets all values in the onboarding store to initial defaults.
*
* @return {Action} The action.
*/
2024-11-20 16:54:29 +01:00
export const reset = () => ( { type: ACTION_TYPES.RESET } );
/**
2024-11-21 17:42:48 +01:00
* Persistent. Set the full onboarding details, usually during app initialization.
*
2024-11-21 17:42:48 +01:00
* @param {{data: {}, flags?: {}}} payload
* @return {Action} The action.
*/
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
} );
/**
* Generic transient-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 setTransient = ( prop, value ) => ( {
2024-11-21 17:42:48 +01:00
type: ACTION_TYPES.SET_TRANSIENT,
payload: { [ prop ]: value },
2024-11-20 16:54:29 +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 );
/**
* Transient. Sets the "manualClientId" value.
*
* @param {string} manualClientId
* @return {Action} The action.
*/
export const setManualClientId = ( manualClientId ) =>
setTransient( 'manualClientId', manualClientId );
/**
* Transient. Sets the "manualClientSecret" value.
*
* @param {string} manualClientSecret
* @return {Action} The action.
*/
export const setManualClientSecret = ( manualClientSecret ) =>
setTransient( 'manualClientSecret', manualClientSecret );
/**
2024-10-28 18:11:04 +01:00
* Persistent.Set the "onboarding completed" flag which shows or hides the wizard.
*
2024-10-28 18:11:04 +01:00
* @param {boolean} completed
* @return {Action} The action.
*/
export const setCompleted = ( completed ) =>
setPersistent( 'completed', completed );
2024-10-22 18:56:37 +02:00
/**
* Persistent. Sets the onboarding wizard to a new step.
*
* @param {number} step
* @return {Action} The action.
2024-10-22 18:56:37 +02:00
*/
export const setStep = ( step ) => setPersistent( 'step', step );
/**
* Persistent. Sets the "isCasualSeller" value.
*
* @param {boolean} isCasualSeller
* @return {Action} The action.
*/
export const setIsCasualSeller = ( isCasualSeller ) =>
setPersistent( 'isCasualSeller', isCasualSeller );
/**
* Persistent. Sets the "areOptionalPaymentMethodsEnabled" value.
*
* @param {boolean} areOptionalPaymentMethodsEnabled
* @return {Action} The action.
*/
export const setAreOptionalPaymentMethodsEnabled = (
areOptionalPaymentMethodsEnabled
) =>
setPersistent(
'areOptionalPaymentMethodsEnabled',
areOptionalPaymentMethodsEnabled
);
/**
* Persistent. Sets the "products" array.
*
* @param {string[]} products
* @return {Action} The action.
*/
export const setProducts = ( products ) =>
setPersistent( 'products', products );
/**
* Side effect. Triggers the persistence of onboarding data to the server.
*
* @return {Action} The action.
*/
export const persist = function* () {
const data = yield select( STORE_NAME ).persistentData();
yield { type: ACTION_TYPES.DO_PERSIST_DATA, data };
};