@riosst100/pwa-marketplace 3.0.5 → 3.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
module.exports = componentOverrideMapping = {
|
|
2
|
+
['@magento/peregrine/lib/talons/CheckoutPage/PaymentInformation/usePaymentMethods.js']: '@riosst100/pwa-marketplace/src/overwrites/peregrine/lib/talons/CheckoutPage/PaymentInformation/usePaymentMethods.js',
|
|
2
3
|
['@magento/venia-ui/lib/components/CartPage/PriceAdjustments/CouponCode/couponCode.js']: '@riosst100/pwa-marketplace/src/overwrites/venia-ui/lib/components/CartPage/PriceAdjustments/CouponCode/couponCode.js',
|
|
3
4
|
['@magento/peregrine/lib/talons/Header/useCartTrigger.js']: '@riosst100/pwa-marketplace/src/overwrites/peregrine/lib/talons/Header/useCartTrigger.js',
|
|
4
5
|
['@magento/peregrine/lib/talons/CartPage/useCartPage.js']: '@riosst100/pwa-marketplace/src/overwrites/peregrine/lib/talons/CartPage/useCartPage.js',
|
package/src/overwrites/peregrine/lib/talons/CheckoutPage/PaymentInformation/usePaymentMethods.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { useCallback } from 'react';
|
|
2
|
+
import { useMutation, useQuery } from '@apollo/client';
|
|
3
|
+
import useFieldState from '@magento/peregrine/lib/hooks/hook-wrappers/useInformedFieldStateWrapper';
|
|
4
|
+
import DEFAULT_OPERATIONS from '@magento/peregrine/lib/talons/CheckoutPage/PaymentInformation/paymentMethods.gql';
|
|
5
|
+
import mergeOperations from '@magento/peregrine/lib/util/shallowMerge';
|
|
6
|
+
|
|
7
|
+
import { useCartContext } from '@magento/peregrine/lib/context/cart';
|
|
8
|
+
|
|
9
|
+
export const usePaymentMethods = props => {
|
|
10
|
+
const operations = mergeOperations(DEFAULT_OPERATIONS, props.operations);
|
|
11
|
+
const {
|
|
12
|
+
getPaymentMethodsQuery,
|
|
13
|
+
setPaymentMethodOnCartMutation
|
|
14
|
+
} = operations;
|
|
15
|
+
|
|
16
|
+
const [setPaymentMethod] = useMutation(setPaymentMethodOnCartMutation);
|
|
17
|
+
|
|
18
|
+
const [{ cartId }] = useCartContext();
|
|
19
|
+
|
|
20
|
+
const { data, loading } = useQuery(getPaymentMethodsQuery, {
|
|
21
|
+
skip: !cartId,
|
|
22
|
+
variables: { cartId }
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const { value: currentSelectedPaymentMethod } = useFieldState(
|
|
26
|
+
'selectedPaymentMethod'
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
const availablePaymentMethods =
|
|
30
|
+
(data && data.cart.available_payment_methods) || [];
|
|
31
|
+
|
|
32
|
+
// If there is one payment method, select it by default.
|
|
33
|
+
// If more than one, none should be selected by default.
|
|
34
|
+
const defaultPaymentCode =
|
|
35
|
+
(availablePaymentMethods.length && availablePaymentMethods[0].code) ||
|
|
36
|
+
null;
|
|
37
|
+
const selectedPaymentCode =
|
|
38
|
+
(data && data.cart.selected_payment_method.code) || null;
|
|
39
|
+
|
|
40
|
+
const initialSelectedMethod =
|
|
41
|
+
availablePaymentMethods.length > 1
|
|
42
|
+
? selectedPaymentCode
|
|
43
|
+
: null;
|
|
44
|
+
|
|
45
|
+
console.log('availablePaymentMethods.length',availablePaymentMethods.length)
|
|
46
|
+
console.log('defaultPaymentCode',defaultPaymentCode)
|
|
47
|
+
console.log('selectedPaymentCode',selectedPaymentCode)
|
|
48
|
+
console.log('initialSelectedMethod',initialSelectedMethod)
|
|
49
|
+
|
|
50
|
+
const handlePaymentMethodSelection = useCallback(
|
|
51
|
+
element => {
|
|
52
|
+
const value = element.target.value;
|
|
53
|
+
|
|
54
|
+
const paymentMethodData =
|
|
55
|
+
value == 'braintree'
|
|
56
|
+
? {
|
|
57
|
+
code: value,
|
|
58
|
+
braintree: {
|
|
59
|
+
payment_method_nonce: value,
|
|
60
|
+
is_active_payment_token_enabler: false
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
: {
|
|
64
|
+
code: value
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
setPaymentMethod({
|
|
68
|
+
variables: {
|
|
69
|
+
cartId,
|
|
70
|
+
paymentMethod: paymentMethodData
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
},
|
|
74
|
+
[cartId, setPaymentMethod]
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
availablePaymentMethods,
|
|
79
|
+
currentSelectedPaymentMethod,
|
|
80
|
+
handlePaymentMethodSelection,
|
|
81
|
+
initialSelectedMethod,
|
|
82
|
+
isLoading: loading
|
|
83
|
+
};
|
|
84
|
+
};
|