File size: 3,320 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import debugFactory from 'debug';
import { useContext, useMemo } from 'react';
import { PaymentMethodProviderContext } from '../payment-method-provider-context';
import type { PaymentMethod, TogglePaymentMethod } from '../../types';

const debug = debugFactory( 'composite-checkout:payment-methods' );

export function usePaymentMethodId(): [ string | null | undefined, ( id: string ) => void ] {
	const { paymentMethodId, setPaymentMethodId } = useContext( PaymentMethodProviderContext );
	if ( ! setPaymentMethodId ) {
		throw new Error( 'usePaymentMethodId can only be used inside a CheckoutProvider' );
	}
	return [ paymentMethodId, setPaymentMethodId ];
}

export function usePaymentMethod(): PaymentMethod | null {
	const { paymentMethodId, setPaymentMethodId } = useContext( PaymentMethodProviderContext );
	const allPaymentMethods = useAllPaymentMethods();
	if ( ! setPaymentMethodId ) {
		throw new Error( 'usePaymentMethod can only be used inside a CheckoutProvider' );
	}
	if ( ! paymentMethodId ) {
		return null;
	}
	const paymentMethod = allPaymentMethods.find( ( { id } ) => id === paymentMethodId );
	if ( ! paymentMethod ) {
		debug( `No payment method found matching id '${ paymentMethodId }' in`, allPaymentMethods );
		return null;
	}
	return paymentMethod;
}

export function useAllPaymentMethods() {
	const { allPaymentMethods } = useContext( PaymentMethodProviderContext );
	if ( ! allPaymentMethods ) {
		throw new Error( 'useAllPaymentMethods cannot be used outside of CheckoutProvider' );
	}
	return allPaymentMethods;
}

export function useAvailablePaymentMethodIds(): string[] {
	const { allPaymentMethods, disabledPaymentMethodIds } = useContext(
		PaymentMethodProviderContext
	);
	if ( ! allPaymentMethods ) {
		throw new Error( 'useAvailablePaymentMethodIds cannot be used outside of CheckoutProvider' );
	}
	const paymentMethodIds = allPaymentMethods.map( ( method ) => method.id );
	const availablePaymentMethodIds = useMemo(
		() => paymentMethodIds.filter( ( id ) => ! disabledPaymentMethodIds.includes( id ) ),
		[ paymentMethodIds, disabledPaymentMethodIds ]
	);
	debug( 'Returning available payment methods', availablePaymentMethodIds );
	return availablePaymentMethodIds;
}

export function useTogglePaymentMethod(): TogglePaymentMethod {
	const { allPaymentMethods, disabledPaymentMethodIds, setDisabledPaymentMethodIds } = useContext(
		PaymentMethodProviderContext
	);
	if ( ! allPaymentMethods ) {
		throw new Error( 'useTogglePaymentMethod cannot be used outside of CheckoutProvider' );
	}
	return ( paymentMethodId: string, available: boolean ) => {
		const paymentMethod = allPaymentMethods.find( ( { id } ) => id === paymentMethodId );
		if ( ! paymentMethod ) {
			debug( `No payment method found matching id '${ paymentMethodId }' in`, allPaymentMethods );
			return;
		}

		if ( available && disabledPaymentMethodIds.includes( paymentMethodId ) ) {
			debug( 'Adding available payment method', paymentMethodId );
			setDisabledPaymentMethodIds(
				disabledPaymentMethodIds.filter( ( id ) => id !== paymentMethodId )
			);
		}

		if ( ! available && ! disabledPaymentMethodIds.includes( paymentMethodId ) ) {
			debug( 'Removing available payment method', paymentMethodId );
			setDisabledPaymentMethodIds( [ ...disabledPaymentMethodIds, paymentMethodId ] );
		}
	};
}