mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-07 19:54:15 +08:00
♻️ Refactor onboarding actions to use controls
This commit is contained in:
parent
cf3798f610
commit
84d2af5f39
4 changed files with 105 additions and 62 deletions
64
modules/ppcp-settings/resources/js/data/onboarding/controls.js
vendored
Normal file
64
modules/ppcp-settings/resources/js/data/onboarding/controls.js
vendored
Normal file
|
@ -0,0 +1,64 @@
|
|||
import { select } from '@wordpress/data';
|
||||
import { apiFetch } from '@wordpress/api-fetch';
|
||||
|
||||
import { NAMESPACE, STORE_NAME } from '../constants';
|
||||
import { REST_PERSIST_PATH, REST_MANUAL_CONNECTION_PATH } from './constants';
|
||||
import ACTION_TYPES from './action-types';
|
||||
import { setIsSaving, setManualConnectionIsBusy } from './actions';
|
||||
|
||||
export const controls = {
|
||||
[ ACTION_TYPES.DO_PERSIST_DATA ]: async ( { dispatch } ) => {
|
||||
let error = null;
|
||||
|
||||
try {
|
||||
const path = `${ NAMESPACE }/${ REST_PERSIST_PATH }`;
|
||||
const data = select( STORE_NAME ).onboardingPersistentData();
|
||||
|
||||
dispatch( setIsSaving( true ) );
|
||||
|
||||
await apiFetch( {
|
||||
path,
|
||||
method: 'post',
|
||||
data,
|
||||
} );
|
||||
} catch ( e ) {
|
||||
error = e;
|
||||
console.error( 'Error saving progress.', e );
|
||||
} finally {
|
||||
dispatch( setIsSaving( false ) );
|
||||
}
|
||||
|
||||
return error === null;
|
||||
},
|
||||
|
||||
[ ACTION_TYPES.DO_MANUAL_CONNECTION ]: async ( { dispatch } ) => {
|
||||
let result = null;
|
||||
|
||||
try {
|
||||
const path = `${ NAMESPACE }/${ REST_MANUAL_CONNECTION_PATH }`;
|
||||
const { clientId, clientSecret, useSandbox } =
|
||||
select( STORE_NAME ).onboardingPersistentData();
|
||||
|
||||
dispatch( setManualConnectionIsBusy( true ) );
|
||||
|
||||
result = await apiFetch( {
|
||||
path,
|
||||
method: 'POST',
|
||||
data: {
|
||||
clientId,
|
||||
clientSecret,
|
||||
useSandbox,
|
||||
},
|
||||
} );
|
||||
} catch ( e ) {
|
||||
result = {
|
||||
success: false,
|
||||
error: e,
|
||||
};
|
||||
} finally {
|
||||
dispatch( setManualConnectionIsBusy( false ) );
|
||||
}
|
||||
|
||||
return result;
|
||||
},
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue