mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Clean up the usePaymentDependencyState.js hook
This commit is contained in:
parent
85c227aabd
commit
68785683e3
1 changed files with 26 additions and 49 deletions
|
@ -1,5 +1,5 @@
|
||||||
/**
|
/**
|
||||||
* Custom hook to handle payment-method-based payment method dependencies
|
* Custom hook to handle payment-method-based dependencies
|
||||||
*/
|
*/
|
||||||
import { useSelect } from '@wordpress/data';
|
import { useSelect } from '@wordpress/data';
|
||||||
|
|
||||||
|
@ -25,77 +25,54 @@ const getParentMethodName = ( parentId, methodsMap ) => {
|
||||||
* @return {Array} List of disabled parent IDs, empty if none
|
* @return {Array} List of disabled parent IDs, empty if none
|
||||||
*/
|
*/
|
||||||
const findDisabledParents = ( method, methodsMap ) => {
|
const findDisabledParents = ( method, methodsMap ) => {
|
||||||
const dependencies = method.depends_on_payment_methods || [];
|
const dependencies = method.depends_on_payment_methods;
|
||||||
|
|
||||||
if ( ! dependencies.length ) {
|
if ( ! dependencies || ! Array.isArray( dependencies ) ) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
const disabledParents = dependencies.filter( ( parentId ) => {
|
return dependencies.filter( ( parentId ) => {
|
||||||
const parent = methodsMap[ parentId ];
|
const parent = methodsMap[ parentId ];
|
||||||
return parent && ! parent.enabled;
|
return parent && ! parent.enabled;
|
||||||
} );
|
} );
|
||||||
|
|
||||||
return disabledParents;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hook to evaluate payment method dependencies for a set of methods
|
* Hook to evaluate payment method dependencies
|
||||||
*
|
*
|
||||||
* @param {Array} methods - List of payment methods
|
* @param {Array} methods - List of payment methods
|
||||||
* @param {Object} methodsMap - Map of payment methods by ID
|
* @param {Object} methodsMap - Map of payment methods by ID
|
||||||
* @return {Object|null} Dependency state object keyed by method ID, or null if not ready
|
* @return {Object} Dependency state object keyed by method ID
|
||||||
*/
|
*/
|
||||||
const usePaymentDependencyState = ( methods, methodsMap ) => {
|
const usePaymentDependencyState = ( methods, methodsMap ) => {
|
||||||
const dependencyState = useSelect(
|
return useSelect( () => {
|
||||||
( select ) => {
|
const result = {};
|
||||||
const paymentStore = select( 'wc/paypal/payment' );
|
|
||||||
if ( ! paymentStore ) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if payment methods data is available
|
|
||||||
if (
|
|
||||||
! methods ||
|
|
||||||
! methodsMap ||
|
|
||||||
Object.keys( methodsMap ).length === 0
|
|
||||||
) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = {};
|
|
||||||
|
|
||||||
|
if ( methods && methodsMap && Object.keys( methodsMap ).length > 0 ) {
|
||||||
methods.forEach( ( method ) => {
|
methods.forEach( ( method ) => {
|
||||||
if ( ! method || ! method.id ) {
|
if ( method && method.id ) {
|
||||||
return;
|
const disabledParents = findDisabledParents(
|
||||||
}
|
method,
|
||||||
|
|
||||||
const disabledParents = findDisabledParents(
|
|
||||||
method,
|
|
||||||
methodsMap
|
|
||||||
);
|
|
||||||
|
|
||||||
if ( disabledParents.length > 0 ) {
|
|
||||||
const parentId = disabledParents[ 0 ];
|
|
||||||
const parentName = getParentMethodName(
|
|
||||||
parentId,
|
|
||||||
methodsMap
|
methodsMap
|
||||||
);
|
);
|
||||||
|
|
||||||
result[ method.id ] = {
|
if ( disabledParents.length > 0 ) {
|
||||||
isDisabled: true,
|
const parentId = disabledParents[ 0 ];
|
||||||
parentId,
|
result[ method.id ] = {
|
||||||
parentName,
|
isDisabled: true,
|
||||||
};
|
parentId,
|
||||||
|
parentName: getParentMethodName(
|
||||||
|
parentId,
|
||||||
|
methodsMap
|
||||||
|
),
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} );
|
} );
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
},
|
}, [ methods, methodsMap ] );
|
||||||
[ methods, methodsMap ]
|
|
||||||
);
|
|
||||||
|
|
||||||
return dependencyState;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default usePaymentDependencyState;
|
export default usePaymentDependencyState;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue