♻️ Minor code improvements

This commit is contained in:
Philipp Stracker 2024-12-03 19:00:04 +01:00
parent 782ba0fc61
commit cecb49cbf5
No known key found for this signature in database

View file

@ -15,6 +15,21 @@ import {
import ConnectionButton from './ConnectionButton'; import ConnectionButton from './ConnectionButton';
const FORM_ERRORS = {
noClientId: __(
'Please enter your Client ID',
'woocommerce-paypal-payments'
),
noClientSecret: __(
'Please enter your Secret Key',
'woocommerce-paypal-payments'
),
invalidClientId: __(
'Please enter a valid Client ID',
'woocommerce-paypal-payments'
),
};
const AdvancedOptionsForm = () => { const AdvancedOptionsForm = () => {
const [ clientValid, setClientValid ] = useState( false ); const [ clientValid, setClientValid ] = useState( false );
const [ secretValid, setSecretValid ] = useState( false ); const [ secretValid, setSecretValid ] = useState( false );
@ -36,37 +51,22 @@ const AdvancedOptionsForm = () => {
const refClientId = useRef( null ); const refClientId = useRef( null );
const refClientSecret = useRef( null ); const refClientSecret = useRef( null );
const errors = {
noClientId: __(
'Please enter a Client ID',
'woocommerce-paypal-payments'
),
noClientSecret: __(
'Please enter your Secret Key',
'woocommerce-paypal-payments'
),
invalidClientId: __(
'Please enter a valid Client ID',
'woocommerce-paypal-payments'
),
};
const validateManualConnectionForm = () => { const validateManualConnectionForm = () => {
const checks = [ const checks = [
{ {
ref: refClientId, ref: refClientId,
valid: () => clientId, valid: () => clientId,
errorMessage: errors.noClientId, errorMessage: FORM_ERRORS.noClientId,
}, },
{ {
ref: refClientId, ref: refClientId,
valid: () => clientValid, valid: () => clientValid,
errorMessage: errors.invalidClientId, errorMessage: FORM_ERRORS.invalidClientId,
}, },
{ {
ref: refClientSecret, ref: refClientSecret,
valid: () => clientSecret && secretValid, valid: () => clientSecret && secretValid,
errorMessage: errors.noClientSecret, errorMessage: FORM_ERRORS.noClientSecret,
}, },
]; ];
@ -80,11 +80,10 @@ const AdvancedOptionsForm = () => {
} }
}; };
const handleManualConnect = async () => { const handleManualConnect = () =>
await handleConnectViaIdAndSecret( { handleConnectViaIdAndSecret( {
validation: validateManualConnectionForm, validation: validateManualConnectionForm,
} ); } );
};
useEffect( () => { useEffect( () => {
setClientValid( ! clientId || /^A[\w-]{79}$/.test( clientId ) ); setClientValid( ! clientId || /^A[\w-]{79}$/.test( clientId ) );
@ -163,7 +162,7 @@ const AdvancedOptionsForm = () => {
/> />
{ clientValid || ( { clientValid || (
<p className="client-id-error"> <p className="client-id-error">
{ errors.invalidClientId } { FORM_ERRORS.invalidClientId }
</p> </p>
) } ) }
<DataStoreControl <DataStoreControl