All files / app/payment/storedInstrument AccountInstrumentFieldset.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    41x   41x 41x 41x   41x 41x                         41x 9x 9x 9x 9x   10x                             9x             9x                 9x                                       41x  
import { AccountInstrument } from '@bigcommerce/checkout-sdk';
import { FieldProps } from 'formik';
import React, { memo, useCallback, FunctionComponent } from 'react';
 
import { TranslatedHtml, TranslatedString } from '../../locale';
import { BasicFormField, Fieldset, Legend } from '../../ui/form';
import { ModalTrigger, ModalTriggerModalProps } from '../../ui/modal';
 
import AccountInstrumentSelect from './AccountInstrumentSelect';
import ManageInstrumentsModal from './ManageInstrumentsModal';
 
export interface AccountInstrumentFieldsetProps {
    instruments: AccountInstrument[];
    selectedInstrument?: AccountInstrument;
    onSelectInstrument(id: string): void;
    onUseNewInstrument(): void;
}
 
export interface AccountInstrumentFieldsetValues {
    instrumentId: string;
}
 
const AccountInstrumentFieldset: FunctionComponent<AccountInstrumentFieldsetProps> = ({
    instruments,
    onSelectInstrument,
    onUseNewInstrument,
    selectedInstrument,
}) => {
    const renderInput = useCallback((field: FieldProps<string>) => (
        <AccountInstrumentSelect
            { ...field }
            instruments={ instruments }
            onSelectInstrument={ onSelectInstrument }
            onUseNewInstrument={ onUseNewInstrument }
            selectedInstrumentId={ selectedInstrument && selectedInstrument.bigpayToken }
        />
    ), [
        instruments,
        onSelectInstrument,
        onUseNewInstrument,
        selectedInstrument,
    ]);
 
    const renderModal = useCallback((props: ModalTriggerModalProps) => (
        <ManageInstrumentsModal
            instruments={ instruments }
            { ...props }
        />
    ), [instruments]);
 
    return <Fieldset
        additionalClassName="instrumentFieldset"
        legend={
            <Legend hidden>
                <TranslatedString id="payment.account_instrument_text" />
            </Legend>
        }
    >
        <ModalTrigger modal={ renderModal }>
            { ({ onClick }) => <button
                className="instrumentModal-trigger"
                onClick={ onClick }
                type="button"
            >
                <TranslatedString id="payment.instrument_manage_button" />
            </button> }
        </ModalTrigger>
 
        <BasicFormField
            name="instrumentId"
            render={ renderInput }
        />
 
        { instruments.length === 0 && <div className="instrumentSelect-note">
            <TranslatedHtml id="payment.account_instrument_new_shipping_address" />
        </div> }
    </Fieldset>;
};
 
export default memo(AccountInstrumentFieldset);