All files / app/payment/storedInstrument CardInstrumentFieldset.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    41x   41x 41x 41x       41x 41x                               41x 28x 28x 28x 28x 28x 28x 28x   28x                                 28x                     28x                 28x                                       41x  
import { CardInstrument } from '@bigcommerce/checkout-sdk';
import { FieldProps } from 'formik';
import React, { memo, useCallback, FunctionComponent } from 'react';
 
import { TranslatedString } from '../../locale';
import { BasicFormField, Fieldset, Legend } from '../../ui/form';
import { ModalTrigger, ModalTriggerModalProps } from '../../ui/modal';
import { HostedCreditCardValidationValues } from '../hostedCreditCard';
 
import { CreditCardValidationValues } from './CreditCardValidation';
import InstrumentSelect from './InstrumentSelect';
import ManageInstrumentsModal from './ManageInstrumentsModal';
 
export interface CardInstrumentFieldsetProps {
    instruments: CardInstrument[];
    selectedInstrumentId?: string;
    shouldHideExpiryDate?: boolean;
    validateInstrument?: React.ReactNode;
    onDeleteInstrument?(instrumentId: string): void;
    onSelectInstrument(id: string): void;
    onUseNewInstrument(): void;
}
 
export type CardInstrumentFieldsetValues = {
    instrumentId: string;
} & CreditCardValidationValues | HostedCreditCardValidationValues;
 
const CardInstrumentFieldset: FunctionComponent<CardInstrumentFieldsetProps> = ({
    instruments,
    onDeleteInstrument,
    onSelectInstrument,
    onUseNewInstrument,
    selectedInstrumentId,
    shouldHideExpiryDate = false,
    validateInstrument = null,
}) => {
    const renderInput = useCallback((field: FieldProps<string>) => (
        <InstrumentSelect
            { ...field }
            instruments={ instruments }
            onSelectInstrument={ onSelectInstrument }
            onUseNewInstrument={ onUseNewInstrument }
            selectedInstrumentId={ selectedInstrumentId }
            shouldHideExpiryDate={ shouldHideExpiryDate }
        />
    ), [
        instruments,
        onSelectInstrument,
        onUseNewInstrument,
        selectedInstrumentId,
        shouldHideExpiryDate,
    ]);
 
    const renderModal = useCallback((props: ModalTriggerModalProps) => (
        <ManageInstrumentsModal
            instruments={ instruments }
            onDeleteInstrument={ onDeleteInstrument }
            { ...props }
        />
    ), [
        instruments,
        onDeleteInstrument,
    ]);
 
    return <Fieldset
        additionalClassName="instrumentFieldset"
        legend={
            <Legend hidden>
                <TranslatedString id="payment.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 }
        />
 
        <div style={ {display: selectedInstrumentId ? undefined : 'none'} }>
            { validateInstrument }
        </div>
    </Fieldset>;
};
 
export default memo(CardInstrumentFieldset);