mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-01 07:02:48 +08:00
♻️ Restructure internal hooks
This commit is contained in:
parent
2302f6ce18
commit
5a81d7378f
1 changed files with 17 additions and 17 deletions
|
@ -25,34 +25,34 @@ const MESSAGES = {
|
|||
),
|
||||
};
|
||||
|
||||
const useCommonConnectionLogic = () => {
|
||||
const useConnectionBase = () => {
|
||||
const { setCompleted } = OnboardingHooks.useSteps();
|
||||
const { createSuccessNotice, createErrorNotice } =
|
||||
useDispatch( noticesStore );
|
||||
|
||||
const handleServerError = ( res, genericMessage ) => {
|
||||
console.error( 'Connection error', res );
|
||||
createErrorNotice( res?.message ?? genericMessage );
|
||||
return {
|
||||
handleError: ( res, genericMessage ) => {
|
||||
console.error( 'Connection error', res );
|
||||
createErrorNotice( res?.message ?? genericMessage );
|
||||
},
|
||||
handleSuccess: async () => {
|
||||
createSuccessNotice( MESSAGES.CONNECTED );
|
||||
return setCompleted( true );
|
||||
},
|
||||
createErrorNotice,
|
||||
};
|
||||
|
||||
const handleServerSuccess = () => {
|
||||
createSuccessNotice( MESSAGES.CONNECTED );
|
||||
return setCompleted( true );
|
||||
};
|
||||
|
||||
return { handleServerError, handleServerSuccess, createErrorNotice };
|
||||
};
|
||||
|
||||
export const useSandboxConnection = () => {
|
||||
const { connectToSandbox, isSandboxMode, setSandboxMode } =
|
||||
CommonHooks.useSandbox();
|
||||
const { handleServerError, createErrorNotice } = useCommonConnectionLogic();
|
||||
const { handleError, createErrorNotice } = useConnectionBase();
|
||||
|
||||
const handleSandboxConnect = async () => {
|
||||
const res = await connectToSandbox();
|
||||
|
||||
if ( ! res.success || ! res.data ) {
|
||||
handleServerError( res, MESSAGES.SANDBOX_ERROR );
|
||||
handleError( res, MESSAGES.SANDBOX_ERROR );
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -81,8 +81,8 @@ export const useManualConnection = () => {
|
|||
clientSecret,
|
||||
setClientSecret,
|
||||
} = CommonHooks.useManualConnection();
|
||||
const { handleServerError, handleServerSuccess, createErrorNotice } =
|
||||
useCommonConnectionLogic();
|
||||
const { handleError, handleSuccess, createErrorNotice } =
|
||||
useConnectionBase();
|
||||
|
||||
const handleConnectViaIdAndSecret = async ( { validation } = {} ) => {
|
||||
if ( 'function' === typeof validation ) {
|
||||
|
@ -96,9 +96,9 @@ export const useManualConnection = () => {
|
|||
const res = await connectViaIdAndSecret();
|
||||
|
||||
if ( res.success ) {
|
||||
await handleServerSuccess();
|
||||
await handleSuccess();
|
||||
} else {
|
||||
handleServerError( res, MESSAGES.MANUAL_ERROR );
|
||||
handleError( res, MESSAGES.MANUAL_ERROR );
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue