2024-10-22 15:15:55 +02:00
|
|
|
import { dispatch, select } from '@wordpress/data';
|
|
|
|
import { apiFetch } from '@wordpress/data-controls';
|
2024-10-22 18:56:37 +02:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2024-10-22 15:15:55 +02:00
|
|
|
import ACTION_TYPES from './action-types';
|
|
|
|
import { NAMESPACE, STORE_NAME } from '../constants';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Persistent. Set the full onboarding details, usually during app initialization.
|
|
|
|
*
|
|
|
|
* @param {Object} payload
|
2024-10-23 15:38:00 +02:00
|
|
|
* @return {{type: string, payload}} The action.
|
2024-10-22 15:15:55 +02:00
|
|
|
*/
|
|
|
|
export const updateOnboardingDetails = ( payload ) => {
|
|
|
|
return {
|
|
|
|
type: ACTION_TYPES.SET_ONBOARDING_DETAILS,
|
|
|
|
payload,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-10-22 18:56:37 +02:00
|
|
|
/**
|
|
|
|
* Persistent. Sets the onboarding wizard to a new step.
|
|
|
|
*
|
|
|
|
* @param {number} step
|
|
|
|
* @return {{type: string, step}} An action.
|
|
|
|
*/
|
|
|
|
export const setOnboardingStep = ( step ) => {
|
|
|
|
return {
|
|
|
|
type: ACTION_TYPES.SET_ONBOARDING_STEP,
|
|
|
|
step,
|
|
|
|
};
|
2024-10-23 15:38:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Persistent. Sets the sandbox mode on or off.
|
|
|
|
*
|
|
|
|
* @param {boolean} sandboxMode
|
|
|
|
* @return {{type: string, useSandbox}} An action.
|
|
|
|
*/
|
|
|
|
export const setSandboxMode = ( sandboxMode ) => {
|
|
|
|
return {
|
|
|
|
type: ACTION_TYPES.SET_SANDBOX_MODE,
|
|
|
|
useSandbox: sandboxMode,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Persistent. Toggles the "Manual Connection" mode on or off.
|
|
|
|
*
|
|
|
|
* @param {boolean} manualConnectionMode
|
|
|
|
* @return {{type: string, useManualConnection}} An action.
|
|
|
|
*/
|
|
|
|
export const setManualConnectionMode = ( manualConnectionMode ) => {
|
|
|
|
return {
|
|
|
|
type: ACTION_TYPES.SET_MANUAL_CONNECTION_MODE,
|
|
|
|
useManualConnection: manualConnectionMode,
|
|
|
|
};
|
2024-10-22 18:56:37 +02:00
|
|
|
};
|
|
|
|
|
2024-10-22 15:15:55 +02:00
|
|
|
/**
|
|
|
|
* Non-persistent. Changes the "saving" flag.
|
|
|
|
*
|
|
|
|
* @param {boolean} isSaving
|
|
|
|
* @return {{type: string, isSaving}} The action.
|
|
|
|
*/
|
2024-10-22 18:56:37 +02:00
|
|
|
export const updateIsSaving = ( isSaving ) => {
|
2024-10-22 15:15:55 +02:00
|
|
|
return {
|
|
|
|
type: ACTION_TYPES.SET_IS_SAVING_ONBOARDING_DETAILS,
|
|
|
|
isSaving,
|
|
|
|
};
|
2024-10-22 18:56:37 +02:00
|
|
|
};
|
2024-10-22 15:15:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves the persistent details to the WP database.
|
|
|
|
*
|
|
|
|
* @return {Generator<any>} A generator function that handles the saving process.
|
|
|
|
*/
|
2024-10-22 18:56:37 +02:00
|
|
|
export function* persist() {
|
2024-10-22 15:15:55 +02:00
|
|
|
let error = null;
|
|
|
|
|
|
|
|
try {
|
2024-10-22 18:56:37 +02:00
|
|
|
const path = `${ NAMESPACE }/onboarding`;
|
|
|
|
const data = select( STORE_NAME ).getOnboardingData();
|
2024-10-22 15:15:55 +02:00
|
|
|
|
2024-10-22 18:56:37 +02:00
|
|
|
yield updateIsSaving( true );
|
2024-10-22 15:15:55 +02:00
|
|
|
|
|
|
|
yield apiFetch( {
|
2024-10-22 18:56:37 +02:00
|
|
|
path,
|
|
|
|
method: 'post',
|
|
|
|
data,
|
2024-10-22 15:15:55 +02:00
|
|
|
} );
|
|
|
|
|
|
|
|
yield dispatch( 'core/notices' ).createSuccessNotice(
|
|
|
|
__( 'Progress saved.', 'woocommerce-paypal-payments' )
|
|
|
|
);
|
|
|
|
} catch ( e ) {
|
|
|
|
error = e;
|
|
|
|
yield dispatch( 'core/notices' ).createErrorNotice(
|
|
|
|
__( 'Error saving progress.', 'woocommerce-paypal-payments' )
|
|
|
|
);
|
|
|
|
} finally {
|
2024-10-22 18:56:37 +02:00
|
|
|
yield updateIsSaving( false );
|
2024-10-22 15:15:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return error === null;
|
|
|
|
}
|