import { yupResolver } from '@hookform/resolvers/yup'; import { useEffect, useState } from 'react'; import { SubmitHandler, useForm } from 'react-hook-form'; import { useAppSelector } from '@akinon/next/redux/hooks'; import { RootState } from '@theme/redux/store'; import { useCompleteFundsTransferMutation, useSetFundsTransferOptionMutation } from '@akinon/next/data/client/checkout'; import { Button, Icon, Price, Radio } from '@theme/components'; import { partAttrs } from '@akinon/pz-theme/src/utils/part-styles'; import * as yup from 'yup'; import CheckoutAgreements from '../agreements'; import PaymentHeader from '../payment-header'; import { useLocalization } from '@akinon/next/hooks'; import { checkPaymentWillRedirect } from '@akinon/next/utils'; import { Image } from '@akinon/next/components/image'; import { Trans } from '@akinon/next/components/trans'; const fundsTransferFormSchema = () => yup.object().shape({ agreement: yup.boolean().oneOf([true], 'This field is required.') }); const CheckoutFundsTransfer = () => { const { t } = useLocalization(); const { handleSubmit, control, formState: { errors, isSubmitting } } = useForm({ resolver: yupResolver(fundsTransferFormSchema()) }); const [formError, setFormError] = useState(null); const [isProcessing, setIsProcessing] = useState(false); const { bankAccounts, selectedBankAccountPk, preOrder } = useAppSelector( (state: RootState) => state.checkout ); const [setFundsTransferOption] = useSetFundsTransferOptionMutation(); const [completeFundsTransfer, { isLoading: isCompletingPayment }] = useCompleteFundsTransferMutation(); const isButtonDisabled = isSubmitting || isCompletingPayment || isProcessing; const onSubmit: SubmitHandler = async () => { if (isButtonDisabled) return; setIsProcessing(true); try { const response = await completeFundsTransfer().unwrap(); setFormError(response?.errors?.non_field_errors); if ( response?.errors?.non_field_errors || !checkPaymentWillRedirect(response) ) { setIsProcessing(false); } } catch (error) { setIsProcessing(false); } }; useEffect(() => { if (!selectedBankAccountPk && bankAccounts?.length) { setFundsTransferOption(bankAccounts[0].pk); } }, [bankAccounts, selectedBankAccountPk, setFundsTransferOption]); return (

{t('checkout.payment.fund_transfer.description')}

{t('checkout.payment.fund_transfer.account_title')}{' '} {t('checkout.payment.fund_transfer.receiver')}: Project Zero Ltd. Sti

{bankAccounts.map((bankAccount) => (
))}

{t('checkout.payment.fund_transfer.attention')}

{t('checkout.payment.fund_transfer.transfer_amount_title')}

{t('checkout.payment.fund_transfer.bold_title')} ) }} />

{t('checkout.payment.fund_transfer.info_second')}

{formError && (
{formError}
)}
); }; export default CheckoutFundsTransfer;