All files / app/payment/storedInstrument InstrumentSelect.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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312  42x 42x 42x   42x 42x   42x 42x 42x   42x                           42x 42x 29x         29x     42x 9x 9x   9x 4x       42x 3x   3x 2x       42x   38x 38x 38x 38x 38x 38x 38x   38x   38x                                                       42x   6x 6x 6x   6x   42x                   42x 4x 4x 4x 4x 4x   4x       8x                                                                 42x 38x 38x 38x 38x   38x 9x               29x                                   42x 8x 8x 8x   8x 1x           8x                                   42x 37x 37x 37x 37x 37x   37x 37x 37x         37x                                                                                                                 42x 13x 13x 13x 13x                                 42x  
import { CardInstrument } from '@bigcommerce/checkout-sdk';
import { expirationDate } from 'card-validator';
import classNames from 'classnames';
import creditCardType from 'credit-card-type';
import { FieldProps } from 'formik';
import { find, noop } from 'lodash';
import React, { useCallback, FunctionComponent, PureComponent, ReactNode } from 'react';
 
import { TranslatedString } from '../../locale';
import { DropdownTrigger } from '../../ui/dropdown';
import { CreditCardIcon } from '../creditCard';
 
import mapFromInstrumentCardType from './mapFromInstrumentCardType';
 
export interface InstrumentSelectProps extends FieldProps<string> {
    instruments: CardInstrument[];
    selectedInstrumentId?: string;
    shouldHideExpiryDate?: boolean;
    onSelectInstrument(id: string): void;
    onUseNewInstrument(): void;
}
 
export interface InstrumentSelectValues {
    instrumentId: string;
}
 
class InstrumentSelect extends PureComponent<InstrumentSelectProps> {
    componentDidMount() {
        const { selectedInstrumentId } = this.props;
 
        // FIXME: Used setTimeout here because setFieldValue call doesnot set value if called before formik is properly mounted.
        //        This ensures that update Field value is called after formik has mounted.
        // See GitHub issue: https://github.com/jaredpalmer/formik/issues/930
        setTimeout(() => this.updateFieldValue(selectedInstrumentId));
    }
 
    componentDidUpdate(prevProps: Readonly<InstrumentSelectProps>) {
        const { selectedInstrumentId: prevSelectedInstrumentId } = prevProps;
        const { selectedInstrumentId } = this.props;
 
        if (prevSelectedInstrumentId !== selectedInstrumentId) {
            this.updateFieldValue(selectedInstrumentId);
        }
    }
 
    componentWillUnmount() {
        const { selectedInstrumentId, field } = this.props;
 
        if (field.value === '' && selectedInstrumentId !== undefined) {
            this.updateFieldValue();
        }
    }
 
    render(): ReactNode {
        const {
            field,
            instruments,
            onSelectInstrument,
            onUseNewInstrument,
            selectedInstrumentId,
            shouldHideExpiryDate = false,
        } = this.props;
 
        const selectedInstrument = find(instruments, { bigpayToken: selectedInstrumentId });
 
        return (
            <div className="instrumentSelect">
                <DropdownTrigger
                    dropdown={
                        <InstrumentMenu
                            instruments={ instruments }
                            onSelectInstrument={ onSelectInstrument }
                            onUseNewInstrument={ onUseNewInstrument }
                            selectedInstrumentId={ selectedInstrumentId }
                            shouldHideExpiryDate={ shouldHideExpiryDate }
                        />
                    }
                >
                    <InstrumentSelectButton
                        instrument={ selectedInstrument }
                        shouldHideExpiryDate={ shouldHideExpiryDate }
                        testId="instrument-select"
                    />
 
                    <input
                        type="hidden"
                        { ...field }
                    />
                </DropdownTrigger>
            </div>
        );
    }
 
    private updateFieldValue(instrumentId: string = ''): void {
        const {
            form,
            field,
        } = this.props;
 
        form.setFieldValue(field.name, instrumentId);
    }
}
 
interface InstrumentMenuProps {
    instruments: CardInstrument[];
    selectedInstrumentId?: string;
    shouldHideExpiryDate?: boolean;
    onSelectInstrument(id: string): void;
    onUseNewInstrument(): void;
}
 
const InstrumentMenu: FunctionComponent<InstrumentMenuProps> = ({
    instruments,
    selectedInstrumentId,
    shouldHideExpiryDate = false,
    onSelectInstrument,
    onUseNewInstrument,
}) => {
    return <ul
        className="instrumentSelect-dropdownMenu instrumentSelect-dropdownMenuNext dropdown-menu"
        data-test="instrument-select-menu"
    >
        { instruments.map(instrument => (
            <li
                className={ classNames(
                    'instrumentSelect-option dropdown-menu-item',
                    { 'instrumentSelect-option--selected': instrument.bigpayToken === selectedInstrumentId }
                ) }
                key={ instrument.bigpayToken }
            >
                <InstrumentOption
                    instrument={ instrument }
                    onClick={ onSelectInstrument }
                    shouldHideExpiryDate={ shouldHideExpiryDate }
                    testId="instrument-select-option"
                />
            </li>
        )) }
 
        <li className="instrumentSelect-option instrumentSelect-option--addNew dropdown-menu-item">
            <InstrumentUseNewButton
                onClick={ onUseNewInstrument }
                testId="instrument-select-option-use-new"
            />
        </li>
    </ul>;
};
 
interface InstrumentSelectButtonProps {
    instrument?: CardInstrument;
    shouldHideExpiryDate?: boolean;
    testId?: string;
    onClick?(): void;
}
 
const InstrumentSelectButton: FunctionComponent<InstrumentSelectButtonProps> = ({
    instrument,
    shouldHideExpiryDate = false,
    testId,
    onClick,
}) => {
    if (!instrument) {
        return (
            <InstrumentUseNewButton
                className="instrumentSelect-button optimizedCheckout-form-select dropdown-button form-input"
                testId={ testId }
            />
        );
    }
 
    return (
        <InstrumentMenuItem
            className="instrumentSelect-button optimizedCheckout-form-select dropdown-button form-input"
            instrument={ instrument }
            onClick={ onClick }
            shouldHideExpiryDate={ shouldHideExpiryDate }
            testId={ testId }
        />
    );
};
 
interface InstrumentOptionProps {
    instrument: CardInstrument;
    testId?: string;
    shouldHideExpiryDate?: boolean;
    onClick?(token: string): void;
}
 
const InstrumentOption: FunctionComponent<InstrumentOptionProps> = ({
    instrument,
    shouldHideExpiryDate = false,
    onClick = noop,
}) => {
    const handleClick = useCallback(() => {
        onClick(instrument.bigpayToken);
    }, [
        onClick,
        instrument,
    ]);
 
    return (
        <InstrumentMenuItem
            instrument={ instrument }
            onClick={ handleClick }
            shouldHideExpiryDate={ shouldHideExpiryDate }
            testId="instrument-select-option"
        />
    );
};
 
interface InstrumentMenuItemProps {
    className?: string;
    instrument: CardInstrument;
    testId?: string;
    shouldHideExpiryDate?: boolean;
    onClick?(): void;
}
 
const InstrumentMenuItem: FunctionComponent<InstrumentMenuItemProps> = ({
    className,
    instrument,
    testId,
    shouldHideExpiryDate = false,
    onClick,
}) => {
    const cardType = mapFromInstrumentCardType(instrument.brand);
    const cardInfo = creditCardType.getTypeInfo(cardType);
    const isExpired = !expirationDate({
        month: instrument.expiryMonth,
        year: instrument.expiryYear,
    }).isValid;
 
    return (
        <button
            className={ className }
            data-test={ testId }
            onClick={ onClick }
            type="button"
        >
            <div className={ classNames(
                'instrumentSelect-details',
                { 'instrumentSelect-details--expired': isExpired }
            ) }
            >
                <CreditCardIcon cardType={ cardType } />
 
                <div
                    className="instrumentSelect-card"
                    data-test={ `${testId}-last4` }
                >
                    { cardInfo ?
                        <TranslatedString
                            data={ { cardTitle: cardInfo.niceType, endingIn: instrument.last4 } }
                            id="payment.instrument_ending_in_text"
                        /> :
                        <TranslatedString
                            data={ { endingIn: instrument.last4 } }
                            id="payment.instrument_default_ending_in_text"
                        /> }
                </div>
 
                { !shouldHideExpiryDate && <div
                    className={ classNames(
                        'instrumentSelect-expiry',
                        { 'instrumentSelect-expiry--expired': isExpired }
                    ) }
                    data-test={ `${testId}-expiry` }
                >
                    { isExpired ?
                        <TranslatedString
                            data={ { expiryDate: `${instrument.expiryMonth}/${instrument.expiryYear}` } }
                            id="payment.instrument_expired_text"
                        /> :
                        <TranslatedString
                            data={ { expiryDate: `${instrument.expiryMonth}/${instrument.expiryYear}` } }
                            id="payment.instrument_expires_text"
                        /> }
                </div> }
            </div>
        </button>
    );
};
 
interface InstrumentUseNewButtonProps {
    className?: string;
    testId?: string;
    onClick?(): void;
}
 
const InstrumentUseNewButton: FunctionComponent<InstrumentUseNewButtonProps> = ({
    className,
    testId,
    onClick = noop,
}) => (
    <button
        className={ className }
        data-test={ testId }
        onClick={ onClick }
        type="button"
    >
        <div className="instrumentSelect-details instrumentSelect-details--addNew">
            <CreditCardIcon />
 
            <div className="instrumentSelect-card">
                <TranslatedString id="payment.instrument_add_card_action" />
            </div>
        </div>
    </button>
);
 
export default InstrumentSelect;