|
| 1 | +import React, { useEffect, useContext } from 'react'; |
| 2 | +import { func, shape } from 'prop-types'; |
| 3 | +import { __ } from '@hyva/react-checkout/i18n'; |
| 4 | +import RadioInput from '@hyva/react-checkout/components/common/Form/RadioInput'; |
| 5 | +import Card from '@hyva/react-checkout/components/common/Card'; |
| 6 | +import SelectInput from '@hyva/react-checkout/components/common/Form/SelectInput'; |
| 7 | +import CheckoutFormContext from '@hyva/react-checkout/context/Form/CheckoutFormContext'; |
| 8 | +import { PAYMENT_METHOD_FORM } from '@hyva/react-checkout/config'; |
| 9 | +import useMultiSafepayPaymentMethodContext from '../../hooks/useMultiSafepayPaymentMethodContext'; |
| 10 | +import useMultiSafepayIdeal from './hooks/useMultiSafepayIdeal'; |
| 11 | +import idealConfig from './idealConfig'; |
| 12 | +import { paymentMethodShape } from '../../utility'; |
| 13 | + |
| 14 | +const issuersField = `${PAYMENT_METHOD_FORM}.multisafepay.ideal.issuersField`; |
| 15 | + |
| 16 | +function IdealComponent({ method, selected, actions }) { |
| 17 | + const { formikData, setFieldValue } = useMultiSafepayPaymentMethodContext(); |
| 18 | + const { registerPaymentAction } = useContext(CheckoutFormContext); |
| 19 | + const { placeOrderWithIdeal } = useMultiSafepayIdeal(method.code); |
| 20 | + const isSelected = method.code === selected.code; |
| 21 | + |
| 22 | + useEffect(() => { |
| 23 | + setFieldValue(issuersField, ''); |
| 24 | + }, [setFieldValue]); |
| 25 | + |
| 26 | + useEffect(() => { |
| 27 | + registerPaymentAction(method.code, placeOrderWithIdeal); |
| 28 | + }, [method, registerPaymentAction, placeOrderWithIdeal]); |
| 29 | + |
| 30 | + const idealRadioInput = ( |
| 31 | + <RadioInput |
| 32 | + value={method.code} |
| 33 | + label={method.title} |
| 34 | + name="paymentMethod" |
| 35 | + checked={isSelected} |
| 36 | + onChange={actions.change} |
| 37 | + /> |
| 38 | + ); |
| 39 | + |
| 40 | + if (!isSelected) { |
| 41 | + return idealRadioInput; |
| 42 | + } |
| 43 | + |
| 44 | + return ( |
| 45 | + <div> |
| 46 | + <div>{idealRadioInput}</div> |
| 47 | + <div className="mx-4 my-4"> |
| 48 | + <Card bg="darker"> |
| 49 | + <div className="container flex flex-col justify-center w-4/5"> |
| 50 | + <SelectInput |
| 51 | + formikData={formikData} |
| 52 | + name={issuersField} |
| 53 | + label={__('Choose your bank')} |
| 54 | + options={idealConfig.issuers} |
| 55 | + /> |
| 56 | + </div> |
| 57 | + </Card> |
| 58 | + </div> |
| 59 | + </div> |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +IdealComponent.propTypes = { |
| 64 | + method: paymentMethodShape.isRequired, |
| 65 | + selected: paymentMethodShape.isRequired, |
| 66 | + actions: shape({ change: func }).isRequired, |
| 67 | +}; |
| 68 | + |
| 69 | +export default IdealComponent; |
0 commit comments