mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-01 07:02:48 +08:00
✨ Integrate new Redux props to the welcome screen
The new fields are managed by a debounced field hook that instantly stores the field values in a local state and propagates the new value to the Redux store after a debouncing-delay
This commit is contained in:
parent
52d04c7347
commit
7b12e65ca8
2 changed files with 70 additions and 0 deletions
|
@ -5,6 +5,7 @@ import PaymentMethodIcons from '../../ReusableComponents/PaymentMethodIcons';
|
||||||
import SettingsToggleBlock from '../../ReusableComponents/SettingsToggleBlock';
|
import SettingsToggleBlock from '../../ReusableComponents/SettingsToggleBlock';
|
||||||
import Separator from '../../ReusableComponents/Separator';
|
import Separator from '../../ReusableComponents/Separator';
|
||||||
import { useOnboardingDetails } from '../../../data';
|
import { useOnboardingDetails } from '../../../data';
|
||||||
|
import { useDebounceField } from '../../../utils/hooks';
|
||||||
|
|
||||||
const StepWelcome = () => {
|
const StepWelcome = () => {
|
||||||
return (
|
return (
|
||||||
|
@ -78,8 +79,22 @@ const WelcomeForm = () => {
|
||||||
setSandboxMode,
|
setSandboxMode,
|
||||||
isManualConnectionMode,
|
isManualConnectionMode,
|
||||||
setManualConnectionMode,
|
setManualConnectionMode,
|
||||||
|
clientId,
|
||||||
|
setClientId,
|
||||||
|
clientSecret,
|
||||||
|
setClientSecret,
|
||||||
} = useOnboardingDetails();
|
} = useOnboardingDetails();
|
||||||
|
|
||||||
|
const [ currentClientId, updateClientId ] = useDebounceField(
|
||||||
|
setClientId,
|
||||||
|
clientId
|
||||||
|
);
|
||||||
|
|
||||||
|
const [ currentClientSecret, updateClientSecret ] = useDebounceField(
|
||||||
|
setClientSecret,
|
||||||
|
clientSecret
|
||||||
|
);
|
||||||
|
|
||||||
const advancedUsersDescription = sprintf(
|
const advancedUsersDescription = sprintf(
|
||||||
// translators: %s: Link to PayPal REST application guide
|
// translators: %s: Link to PayPal REST application guide
|
||||||
__(
|
__(
|
||||||
|
@ -122,12 +137,16 @@ const WelcomeForm = () => {
|
||||||
'Sandbox Client ID',
|
'Sandbox Client ID',
|
||||||
'woocommerce-paypal-payments'
|
'woocommerce-paypal-payments'
|
||||||
) }
|
) }
|
||||||
|
value={ currentClientId }
|
||||||
|
onChange={ updateClientId }
|
||||||
></TextControl>
|
></TextControl>
|
||||||
<TextControl
|
<TextControl
|
||||||
label={ __(
|
label={ __(
|
||||||
'Sandbox Secret Key',
|
'Sandbox Secret Key',
|
||||||
'woocommerce-paypal-payments'
|
'woocommerce-paypal-payments'
|
||||||
) }
|
) }
|
||||||
|
value={ currentClientSecret }
|
||||||
|
onChange={ updateClientSecret }
|
||||||
type="password"
|
type="password"
|
||||||
></TextControl>
|
></TextControl>
|
||||||
<Button variant="secondary">
|
<Button variant="secondary">
|
||||||
|
|
51
modules/ppcp-settings/resources/js/utils/hooks.js
Normal file
51
modules/ppcp-settings/resources/js/utils/hooks.js
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
import { useCallback, useEffect, useMemo, useState } from '@wordpress/element';
|
||||||
|
import { debounce } from '../../../../ppcp-blocks/resources/js/Helper/debounce';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hook to manage a form field in a local state, and sync it to the Redux store via a
|
||||||
|
* debounced setter. This ensures a good balance between fast/lightweight state management
|
||||||
|
* and a stable Redux integration, even when the value changes very frequently.
|
||||||
|
*
|
||||||
|
* @param {Function} syncToStore - Function to sync value to the Redux store.
|
||||||
|
* @param {string} storeValue - Initial value from the Redux store.
|
||||||
|
* @param {number} delay - Debounce delay, in milliseconds.
|
||||||
|
* @return {[string, Function]} Tuple of [fieldValue, updateField]
|
||||||
|
*/
|
||||||
|
export const useDebounceField = (
|
||||||
|
syncToStore,
|
||||||
|
storeValue = '',
|
||||||
|
delay = 300
|
||||||
|
) => {
|
||||||
|
const [ fieldValue, setFieldValue ] = useState( storeValue );
|
||||||
|
|
||||||
|
// Memoize the debounced store sync
|
||||||
|
const debouncedSync = useMemo(
|
||||||
|
() => debounce( syncToStore, delay ),
|
||||||
|
[ syncToStore, delay ]
|
||||||
|
);
|
||||||
|
|
||||||
|
// Sync field with store changes
|
||||||
|
useEffect( () => {
|
||||||
|
if ( storeValue !== '' && fieldValue !== storeValue ) {
|
||||||
|
setFieldValue( storeValue );
|
||||||
|
}
|
||||||
|
}, [ storeValue, fieldValue ] );
|
||||||
|
|
||||||
|
// Handle field updates and store sync
|
||||||
|
const updateField = useCallback(
|
||||||
|
( newValue ) => {
|
||||||
|
setFieldValue( newValue );
|
||||||
|
debouncedSync( newValue );
|
||||||
|
},
|
||||||
|
[ debouncedSync ]
|
||||||
|
);
|
||||||
|
|
||||||
|
// Cleanup on unmount
|
||||||
|
useEffect( () => {
|
||||||
|
return () => {
|
||||||
|
debouncedSync.cancel();
|
||||||
|
};
|
||||||
|
}, [ debouncedSync ] );
|
||||||
|
|
||||||
|
return [ fieldValue, updateField ];
|
||||||
|
};
|
Loading…
Add table
Add a link
Reference in a new issue