mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
♻️ Refactor resolver and controls
This commit is contained in:
parent
79174459d8
commit
90c6cd1e7d
5 changed files with 42 additions and 57 deletions
|
@ -1,4 +1,3 @@
|
|||
export const NAMESPACE = '/wc/v3/wc_paypal';
|
||||
export const BUSINESS_TYPES = {
|
||||
CASUAL_SELLER: 'casual_seller',
|
||||
BUSINESS: 'business',
|
||||
|
|
|
@ -15,7 +15,7 @@ export const STORE_NAME = 'wc/paypal/onboarding';
|
|||
*
|
||||
* @type {string}
|
||||
*/
|
||||
export const REST_HYDRATE_PATH = 'onboarding';
|
||||
export const REST_HYDRATE_PATH = '/wc/v3/wc_paypal/onboarding';
|
||||
|
||||
/**
|
||||
* REST path to persist data of this module to the WP DB.
|
||||
|
@ -25,7 +25,7 @@ export const REST_HYDRATE_PATH = 'onboarding';
|
|||
*
|
||||
* @type {string}
|
||||
*/
|
||||
export const REST_PERSIST_PATH = 'onboarding';
|
||||
export const REST_PERSIST_PATH = '/wc/v3/wc_paypal/onboarding';
|
||||
|
||||
/**
|
||||
* REST path to perform the manual connection check, using client ID and secret,
|
||||
|
@ -35,4 +35,4 @@ export const REST_PERSIST_PATH = 'onboarding';
|
|||
*
|
||||
* @type {string}
|
||||
*/
|
||||
export const REST_MANUAL_CONNECTION_PATH = 'connect_manual';
|
||||
export const REST_MANUAL_CONNECTION_PATH = '/wc/v3/wc_paypal/connect_manual';
|
||||
|
|
|
@ -1,57 +1,46 @@
|
|||
/**
|
||||
* Controls: Implement side effects, typically asynchronous operations.
|
||||
*
|
||||
* Controls use ACTION_TYPES keys as identifiers to ensure uniqueness.
|
||||
* Controls use ACTION_TYPES keys as identifiers.
|
||||
* They are triggered by corresponding actions and handle external interactions.
|
||||
*
|
||||
* @file
|
||||
*/
|
||||
|
||||
import { select } from '@wordpress/data';
|
||||
import { apiFetch } from '@wordpress/api-fetch';
|
||||
import apiFetch from '@wordpress/api-fetch';
|
||||
|
||||
import { NAMESPACE, STORE_NAME } from '../constants';
|
||||
import { REST_PERSIST_PATH, REST_MANUAL_CONNECTION_PATH } from './constants';
|
||||
import {
|
||||
STORE_NAME,
|
||||
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;
|
||||
|
||||
async [ ACTION_TYPES.DO_PERSIST_DATA ]( { data } ) {
|
||||
console.log( 'Do PERSIST: ', data );
|
||||
try {
|
||||
const path = `${ NAMESPACE }/${ REST_PERSIST_PATH }`;
|
||||
const data = select( STORE_NAME ).onboardingPersistentData();
|
||||
|
||||
dispatch( setIsSaving( true ) );
|
||||
|
||||
await apiFetch( {
|
||||
path,
|
||||
method: 'post',
|
||||
path: REST_PERSIST_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 } ) => {
|
||||
async [ ACTION_TYPES.DO_MANUAL_CONNECTION ]( {
|
||||
clientId,
|
||||
clientSecret,
|
||||
useSandbox,
|
||||
} ) {
|
||||
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,
|
||||
path: REST_MANUAL_CONNECTION_PATH,
|
||||
method: 'POST',
|
||||
data: {
|
||||
clientId,
|
||||
|
@ -64,8 +53,6 @@ export const controls = {
|
|||
success: false,
|
||||
error: e,
|
||||
};
|
||||
} finally {
|
||||
dispatch( setManualConnectionIsBusy( false ) );
|
||||
}
|
||||
|
||||
return result;
|
||||
|
|
|
@ -5,8 +5,8 @@ import { STORE_NAME } from './constants';
|
|||
import reducer from './reducer';
|
||||
import * as selectors from './selectors';
|
||||
import * as actions from './actions';
|
||||
import * as resolvers from './resolvers';
|
||||
import * as hooks from './hooks';
|
||||
import { resolvers } from './resolvers';
|
||||
import { controls } from './controls';
|
||||
|
||||
export const initStore = () => {
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
* Resolvers: Handle asynchronous data fetching for the store.
|
||||
*
|
||||
* These functions update store state with data from external sources.
|
||||
* Each resolver corresponds to a specific selector but must have a unique name.
|
||||
* Each resolver corresponds to a specific selector (selector with same name must exist).
|
||||
* Resolvers are called automatically when selectors request unavailable data.
|
||||
*
|
||||
* @file
|
||||
|
@ -12,26 +12,25 @@ import { dispatch } from '@wordpress/data';
|
|||
import { __ } from '@wordpress/i18n';
|
||||
import { apiFetch } from '@wordpress/data-controls';
|
||||
|
||||
import { NAMESPACE } from '../constants';
|
||||
import { REST_HYDRATE_PATH } from './constants';
|
||||
import { setIsReady, hydrateOnboardingDetails } from './actions';
|
||||
import { STORE_NAME, REST_HYDRATE_PATH } from './constants';
|
||||
|
||||
/**
|
||||
* Retrieve settings from the site's REST API.
|
||||
*/
|
||||
export function* onboardingPersistentData() {
|
||||
const path = `${ NAMESPACE }/${ REST_HYDRATE_PATH }`;
|
||||
export const resolvers = {
|
||||
/**
|
||||
* Retrieve settings from the site's REST API.
|
||||
*/
|
||||
*persistentData() {
|
||||
try {
|
||||
const result = yield apiFetch( { path: REST_HYDRATE_PATH } );
|
||||
|
||||
try {
|
||||
const result = yield apiFetch( { path } );
|
||||
yield hydrateOnboardingDetails( result );
|
||||
yield setIsReady( true );
|
||||
} catch ( e ) {
|
||||
yield dispatch( 'core/notices' ).createErrorNotice(
|
||||
__(
|
||||
'Error retrieving onboarding details.',
|
||||
'woocommerce-paypal-payments'
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
yield dispatch( STORE_NAME ).hydrate( result );
|
||||
yield dispatch( STORE_NAME ).setIsReady( true );
|
||||
} catch ( e ) {
|
||||
yield dispatch( 'core/notices' ).createErrorNotice(
|
||||
__(
|
||||
'Error retrieving onboarding details.',
|
||||
'woocommerce-paypal-payments'
|
||||
)
|
||||
);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue