All files / app/payment/paymentMethod MollieAPMCustomForm.tsx

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 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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125    26x   26x 26x 26x 26x                                                       26x 4x   4x 6x   4x 1x     3x 1x   1x       1x       3x         6x                   3x               26x 6x   6x 6x       6x     6x     26x                     26x 12x   4x                               26x  
import { PaymentMethod } from '@bigcommerce/checkout-sdk';
import { FieldProps } from 'formik';
import React, { useCallback, useEffect, useState, FunctionComponent, SyntheticEvent } from 'react';
 
import { preventDefault } from '../../common/dom';
import { withLanguage, WithLanguageProps } from '../../locale';
import { DropdownTrigger } from '../../ui/dropdown';
import { FormField } from '../../ui/form';
 
export interface MollieCustomCardFormProps {
    method: PaymentMethod;
}
 
export interface Issuer {
    name: string;
    image: {
        size1x: string;
    };
    id: string;
}
 
interface HiddenInputProps extends FieldProps {
    selectedIssuer?: Issuer;
}
 
interface SelecteIssuerProp {
    selectedIssuer: Issuer;
}
 
interface OptionButtonProps {
    issuer: Issuer;
    className?: string;
    onClick?(event: SyntheticEvent<EventTarget>): void;
}
 
const MollieAPMCustomForm: FunctionComponent<MollieCustomCardFormProps & WithLanguageProps> = ({ method, language }) => {
    const issuers: Issuer[] = method.initializationData?.paymentMethodsResponse;
 
    const [ selectedIssuer, setSelectedIssuer ] = useState<Issuer>({ name: language.translate('payment.select_your_bank') , id: '', image: { size1x: '' } });
    const render = useCallback((props: FieldProps) => <HiddenInput { ...props } selectedIssuer={ selectedIssuer } />, [ selectedIssuer ]);
 
    if (!issuers || issuers.length === 0) {
        return <></>;
    }
 
    const handleClick = ({ currentTarget }: SyntheticEvent<HTMLButtonElement>) => {
        const _selectedIssuer = issuers.find(({ id }) => id === currentTarget?.dataset.id);
 
        Iif (!_selectedIssuer) {
            return;
        }
 
        setSelectedIssuer(_selectedIssuer);
    };
 
    const issuersList = (
        <ul
            className="dropdown-menu instrumentSelect-dropdownMenu mollie-instrument-card"
            id="issuersDropdown"
        >
            { issuers.map(issuer =>
                <li
                    className="dropdown-menu-item dropdown-menu-item--select"
                    key={ issuer.id }
                >
                    <OptionButton issuer={ issuer } onClick={ handleClick } />
                </li>
            ) }
        </ul>
    );
 
    return (<>
        <DropdownTrigger dropdown={ issuersList }>
            <IssuerSelectButton selectedIssuer={ selectedIssuer } />
        </DropdownTrigger>
        <FormField input={ render } name="issuer" />
    </>);
};
 
export const HiddenInput: FunctionComponent<HiddenInputProps> = ({ field: { value, ...restField }, form, selectedIssuer}) => {
    const Input = useCallback(() => <input { ...restField } type="hidden" />, [restField]);
 
    useEffect(() => {
        Iif (value === selectedIssuer) {
            return;
        }
 
        form.setFieldValue(restField.name, selectedIssuer?.id);
    }, [value, form, selectedIssuer, restField.name]);
 
    return <Input />;
};
 
export const IssuerSelectButton: FunctionComponent<SelecteIssuerProp> = ({ selectedIssuer }) => (
    <a
        className="instrumentSelect instrumentSelect-card button dropdown-button dropdown-toogle--select"
        href="#"
        id="issuerToggle"
        onClick={ preventDefault() }
    >
        { selectedIssuer.name }
    </a>
);
 
export const OptionButton: FunctionComponent<OptionButtonProps> = ({ issuer, ...props }) => {
    const { name, image, id } = issuer;
 
    return (
        <a
            className="instrumentSelect-details mollie-instrument-list"
            { ...props }
            data-id={ id }
        >
            <label className="mollie-instrument-left">{ name }</label>
            <img
                alt={ name }
                data-test="cart-item-image"
                src={ image.size1x }
            />
        </a>
    );
};
 
export default withLanguage(MollieAPMCustomForm);