mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 13:44:42 +08:00
Move api requests to controls
This commit is contained in:
parent
f0a09ba83b
commit
da0a81036a
5 changed files with 44 additions and 28 deletions
|
@ -1,10 +1,9 @@
|
||||||
import { useState } from '@wordpress/element';
|
import { useState } from '@wordpress/element';
|
||||||
import apiFetch from '@wordpress/api-fetch';
|
|
||||||
import { REST_WEBHOOKS_SIMULATE } from '../../../../../../data/common/constants';
|
|
||||||
import { __ } from '@wordpress/i18n';
|
import { __ } from '@wordpress/i18n';
|
||||||
import { ButtonSettingsBlock } from '../../../../../ReusableComponents/SettingsBlocks';
|
import { ButtonSettingsBlock } from '../../../../../ReusableComponents/SettingsBlocks';
|
||||||
import { useDispatch } from '@wordpress/data';
|
import { useDispatch } from '@wordpress/data';
|
||||||
import { store as noticesStore } from '@wordpress/notices';
|
import { store as noticesStore } from '@wordpress/notices';
|
||||||
|
import { CommonHooks } from '../../../../../../data';
|
||||||
|
|
||||||
const SimulationBlock = () => {
|
const SimulationBlock = () => {
|
||||||
const {
|
const {
|
||||||
|
@ -13,26 +12,12 @@ const SimulationBlock = () => {
|
||||||
createErrorNotice,
|
createErrorNotice,
|
||||||
removeNotice,
|
removeNotice,
|
||||||
} = useDispatch( noticesStore );
|
} = useDispatch( noticesStore );
|
||||||
|
const { startWebhookSimulation, checkWebhookSimulationState } =
|
||||||
|
CommonHooks.useWebhooks();
|
||||||
const [ simulating, setSimulating ] = useState( false );
|
const [ simulating, setSimulating ] = useState( false );
|
||||||
|
|
||||||
const sleep = ( ms ) => {
|
const sleep = ( ms ) => {
|
||||||
return new Promise( ( resolve ) => setTimeout( resolve, ms ) );
|
return new Promise( ( resolve ) => setTimeout( resolve, ms ) );
|
||||||
};
|
};
|
||||||
|
|
||||||
const startWebhookSimulation = async () => {
|
|
||||||
return apiFetch( {
|
|
||||||
method: 'POST',
|
|
||||||
path: REST_WEBHOOKS_SIMULATE,
|
|
||||||
} );
|
|
||||||
};
|
|
||||||
|
|
||||||
const checkWebhookSimulationState = async () => {
|
|
||||||
return apiFetch( {
|
|
||||||
path: REST_WEBHOOKS_SIMULATE,
|
|
||||||
} );
|
|
||||||
};
|
|
||||||
|
|
||||||
const startSimulation = async ( maxRetries ) => {
|
const startSimulation = async ( maxRetries ) => {
|
||||||
const simulationStartNoticeId =
|
const simulationStartNoticeId =
|
||||||
'paypal-webhook-simulation-start-notice';
|
'paypal-webhook-simulation-start-notice';
|
||||||
|
@ -59,6 +44,7 @@ const SimulationBlock = () => {
|
||||||
try {
|
try {
|
||||||
await startWebhookSimulation();
|
await startWebhookSimulation();
|
||||||
} catch ( error ) {
|
} catch ( error ) {
|
||||||
|
console.error( error );
|
||||||
setSimulating( false );
|
setSimulating( false );
|
||||||
createErrorNotice(
|
createErrorNotice(
|
||||||
__(
|
__(
|
||||||
|
|
|
@ -25,4 +25,7 @@ export default {
|
||||||
DO_REFRESH_MERCHANT: 'COMMON:DO_REFRESH_MERCHANT',
|
DO_REFRESH_MERCHANT: 'COMMON:DO_REFRESH_MERCHANT',
|
||||||
DO_REFRESH_FEATURES: 'DO_REFRESH_FEATURES',
|
DO_REFRESH_FEATURES: 'DO_REFRESH_FEATURES',
|
||||||
DO_RESUBSCRIBE_WEBHOOKS: 'COMMON:DO_RESUBSCRIBE_WEBHOOKS',
|
DO_RESUBSCRIBE_WEBHOOKS: 'COMMON:DO_RESUBSCRIBE_WEBHOOKS',
|
||||||
|
DO_START_WEBHOOK_SIMULATION: 'COMMON:DO_START_WEBHOOK_SIMULATION',
|
||||||
|
DO_CHECK_WEBHOOK_SIMULATION_STATE:
|
||||||
|
'COMMON:DO_CHECK_WEBHOOK_SIMULATION_STATE',
|
||||||
};
|
};
|
||||||
|
|
|
@ -241,3 +241,21 @@ export const resubscribeWebhooks = function* () {
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Side effect. Starts webhook simulation.
|
||||||
|
*
|
||||||
|
* @return {Action} The action.
|
||||||
|
*/
|
||||||
|
export const startWebhookSimulation = function* () {
|
||||||
|
return yield { type: ACTION_TYPES.DO_START_WEBHOOK_SIMULATION };
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Side effect. Checks webhook simulation.
|
||||||
|
*
|
||||||
|
* @return {Action} The action.
|
||||||
|
*/
|
||||||
|
export const checkWebhookSimulationState = function* () {
|
||||||
|
return yield { type: ACTION_TYPES.DO_CHECK_WEBHOOK_SIMULATION_STATE };
|
||||||
|
};
|
||||||
|
|
|
@ -16,6 +16,7 @@ import {
|
||||||
REST_PERSIST_PATH,
|
REST_PERSIST_PATH,
|
||||||
REST_REFRESH_FEATURES_PATH,
|
REST_REFRESH_FEATURES_PATH,
|
||||||
REST_WEBHOOKS,
|
REST_WEBHOOKS,
|
||||||
|
REST_WEBHOOKS_SIMULATE,
|
||||||
} from './constants';
|
} from './constants';
|
||||||
import ACTION_TYPES from './action-types';
|
import ACTION_TYPES from './action-types';
|
||||||
|
|
||||||
|
@ -123,4 +124,17 @@ export const controls = {
|
||||||
path: REST_WEBHOOKS,
|
path: REST_WEBHOOKS,
|
||||||
} );
|
} );
|
||||||
},
|
},
|
||||||
|
|
||||||
|
async [ ACTION_TYPES.DO_START_WEBHOOK_SIMULATION ]() {
|
||||||
|
return await apiFetch( {
|
||||||
|
method: 'POST',
|
||||||
|
path: REST_WEBHOOKS_SIMULATE,
|
||||||
|
} );
|
||||||
|
},
|
||||||
|
|
||||||
|
async [ ACTION_TYPES.DO_CHECK_WEBHOOK_SIMULATION_STATE ]() {
|
||||||
|
return await apiFetch( {
|
||||||
|
path: REST_WEBHOOKS_SIMULATE,
|
||||||
|
} );
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -9,8 +9,7 @@
|
||||||
|
|
||||||
import { useDispatch, useSelect } from '@wordpress/data';
|
import { useDispatch, useSelect } from '@wordpress/data';
|
||||||
import { useCallback } from '@wordpress/element';
|
import { useCallback } from '@wordpress/element';
|
||||||
import { REST_WEBHOOKS, STORE_NAME } from './constants';
|
import { STORE_NAME } from './constants';
|
||||||
import apiFetch from '@wordpress/api-fetch';
|
|
||||||
|
|
||||||
const useTransient = ( key ) =>
|
const useTransient = ( key ) =>
|
||||||
useSelect(
|
useSelect(
|
||||||
|
@ -31,10 +30,11 @@ const useHooks = () => {
|
||||||
setManualConnectionMode,
|
setManualConnectionMode,
|
||||||
setClientId,
|
setClientId,
|
||||||
setClientSecret,
|
setClientSecret,
|
||||||
setWebhooks,
|
|
||||||
connectToSandbox,
|
connectToSandbox,
|
||||||
connectToProduction,
|
connectToProduction,
|
||||||
connectViaIdAndSecret,
|
connectViaIdAndSecret,
|
||||||
|
startWebhookSimulation,
|
||||||
|
checkWebhookSimulationState,
|
||||||
} = useDispatch( STORE_NAME );
|
} = useDispatch( STORE_NAME );
|
||||||
|
|
||||||
// Transient accessors.
|
// Transient accessors.
|
||||||
|
@ -78,19 +78,14 @@ const useHooks = () => {
|
||||||
setClientSecret: ( value ) => {
|
setClientSecret: ( value ) => {
|
||||||
return savePersistent( setClientSecret, value );
|
return savePersistent( setClientSecret, value );
|
||||||
},
|
},
|
||||||
setWebhooks: async () => {
|
|
||||||
const response = await apiFetch( {
|
|
||||||
method: 'GET',
|
|
||||||
path: REST_WEBHOOKS,
|
|
||||||
} );
|
|
||||||
setWebhooks( response?.data?.webhooks );
|
|
||||||
},
|
|
||||||
connectToSandbox,
|
connectToSandbox,
|
||||||
connectToProduction,
|
connectToProduction,
|
||||||
connectViaIdAndSecret,
|
connectViaIdAndSecret,
|
||||||
merchant,
|
merchant,
|
||||||
wooSettings,
|
wooSettings,
|
||||||
webhooks,
|
webhooks,
|
||||||
|
startWebhookSimulation,
|
||||||
|
checkWebhookSimulationState,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue