Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Update snaps/fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley committed May 27, 2021
1 parent 6378390 commit 135ec39
Show file tree
Hide file tree
Showing 3 changed files with 1,918 additions and 1,849 deletions.
64 changes: 20 additions & 44 deletions assets/js/base/context/hooks/payment-methods/use-payment-methods.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/**
* External dependencies
*/
import { useEffect, useRef } from '@wordpress/element';
import isShallowEqual from '@wordpress/is-shallow-equal';
import { useShallowEqual } from '@woocommerce/base-hooks';

/**
* Internal dependencies
Expand All @@ -17,48 +16,25 @@ const usePaymentMethodState = ( express = false ) => {
expressPaymentMethodsInitialized,
} = usePaymentMethodDataContext();

const currentState = useRef( {
paymentMethods,
isInitialized: paymentMethodsInitialized,
} );

const currentExpressState = useRef( {
paymentMethods: expressPaymentMethods,
isInitialized: expressPaymentMethodsInitialized,
} );

useEffect( () => {
if (
paymentMethodsInitialized !== currentState.current.isInitialized ||
! isShallowEqual(
paymentMethods,
currentState.current.paymentMethods
)
) {
currentState.current = {
paymentMethods,
isInitialized: paymentMethodsInitialized,
};
}
}, [ paymentMethods, paymentMethodsInitialized ] );

useEffect( () => {
if (
expressPaymentMethodsInitialized !==
currentExpressState.current.isInitialized ||
! isShallowEqual(
expressPaymentMethods,
currentExpressState.current.paymentMethods
)
) {
currentExpressState.current = {
paymentMethods: expressPaymentMethods,
isInitialized: expressPaymentMethodsInitialized,
};
}
}, [ expressPaymentMethods, expressPaymentMethodsInitialized ] );

return express ? currentExpressState.current : currentState.current;
const currentPaymentMethods = useShallowEqual( paymentMethods );
const currentExpressPaymentMethods = useShallowEqual(
expressPaymentMethods
);
const currentPaymentMethodsInitialized = useShallowEqual(
paymentMethodsInitialized
);
const currentExpressPaymentMethodsInitialized = useShallowEqual(
expressPaymentMethodsInitialized
);

return {
paymentMethods: express
? currentExpressPaymentMethods
: currentPaymentMethods,
isInitialized: express
? currentExpressPaymentMethodsInitialized
: currentPaymentMethodsInitialized,
};
};

export const usePaymentMethods = () => usePaymentMethodState();
Expand Down
Loading

0 comments on commit 135ec39

Please sign in to comment.