All files / app/payment/paymentMethod/PPSDKPaymentMethod usePropsToOnMount.ts

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37    25x 25x                     25x 40x   10x 10x             10x 10x   10x 4x   4x 1x        
 
import { CheckoutService, PaymentMethod } from '@bigcommerce/checkout-sdk';
import { noop } from 'lodash';
import { useCallback, useMemo } from 'react';
 
type CheckoutServiceInstance = InstanceType<typeof CheckoutService>;
 
interface Props {
    method: PaymentMethod;
    deinitializePayment: CheckoutServiceInstance['deinitializePayment'];
    initializePayment: CheckoutServiceInstance['initializePayment'];
    onUnhandledError?(error: Error): void;
}
 
export const usePropsToOnMount = (props: Props): () => () => void => {
    const { initializePayment, deinitializePayment, method, onUnhandledError = noop } = props;
 
    const options = useMemo(
        () => ({
            gatewayId: method.gateway,
            methodId: method.id,
        }),
        [method.gateway, method.id]
    );
 
    const onInit = useCallback(() => initializePayment(options), [initializePayment, options]);
    const onDeinit = useCallback(() => deinitializePayment(options), [deinitializePayment, options]);
 
    return useCallback(() => {
        onInit().catch(onUnhandledError);
 
        return () => {
            onDeinit().catch(onUnhandledError);
        };
    }, [onInit, onUnhandledError, onDeinit]);
};