All files / app/payment/storedInstrument AccountInstrumentSelect.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 313 314 315 316 317 318 319 320 321 322  41x   41x 41x   41x 41x 41x   41x                         41x 41x 17x         17x     41x 6x 6x   6x 2x       41x 1x   1x 1x       41x   23x 23x 23x 23x 23x 23x   23x 23x   23x                                                     41x   3x 3x 3x   3x   41x                 41x 5x 5x 5x 5x   5x       10x                                                             41x 23x 23x 23x   23x 13x               10x                                             41x 10x 10x   10x 2x           10x                                           41x 15x 15x 15x 15x   15x                                                                     41x 5x 5x 5x 5x   5x 5x   5x                                                         41x 18x 18x 18x 18x                                       41x  
import { AccountInstrument, BankInstrument } from '@bigcommerce/checkout-sdk';
import classNames from 'classnames';
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 { IconNewAccount, IconPaypal, IconSize } from '../../ui/icon';
 
import isBankAccountInstrument from './isBankAccountInstrument';
 
export interface AccountInstrumentSelectProps extends FieldProps<string> {
    instruments: AccountInstrument[];
    selectedInstrumentId?: string;
    onSelectInstrument(id: string): void;
    onUseNewInstrument(): void;
}
 
export interface AccountInstrumentSelectValues {
    instrumentId: string;
}
 
class AccountInstrumentSelect extends PureComponent<AccountInstrumentSelectProps> {
    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<AccountInstrumentSelectProps>) {
        const { selectedInstrumentId: prevSelectedInstrumentId } = prevProps;
        const { selectedInstrumentId } = this.props;
 
        if (prevSelectedInstrumentId !== selectedInstrumentId) {
            this.updateFieldValue(selectedInstrumentId);
        }
    }
 
    componentWillUnmount() {
        const { selectedInstrumentId, field } = this.props;
 
        Eif (field.value === '' && selectedInstrumentId !== undefined) {
            this.updateFieldValue();
        }
    }
 
    render(): ReactNode {
        const {
            field,
            instruments,
            onSelectInstrument,
            onUseNewInstrument,
            selectedInstrumentId,
        } = this.props;
 
        const selectedInstrument = find(instruments, { bigpayToken: selectedInstrumentId });
        const { value, ...otherFieldProps } = field;
 
        return (
            <div className="instrumentSelect">
                <DropdownTrigger
                    dropdown={
                        <AccountInstrumentMenu
                            instruments={ instruments }
                            onSelectInstrument={ onSelectInstrument }
                            onUseNewInstrument={ onUseNewInstrument }
                            selectedInstrumentId={ selectedInstrumentId }
                        />
                    }
                >
                    <AccountInstrumentSelectButton
                        instrument={ selectedInstrument }
                        testId="instrument-select"
                    />
 
                    <input
                        type="hidden"
                        value={ value || '' }
                        { ...otherFieldProps }
                    />
                </DropdownTrigger>
            </div>
        );
    }
 
    private updateFieldValue(instrumentId: string = ''): void {
        const {
            form,
            field,
        } = this.props;
 
        form.setFieldValue(field.name, instrumentId);
    }
}
 
interface AccountInstrumentMenuProps {
    instruments: AccountInstrument[];
    selectedInstrumentId?: string;
    onSelectInstrument(id: string): void;
    onUseNewInstrument(): void;
}
 
const AccountInstrumentMenu: FunctionComponent<AccountInstrumentMenuProps> = ({
    instruments,
    selectedInstrumentId,
    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 }
            >
                <AccountInstrumentOption
                    instrument={ instrument }
                    onClick={ onSelectInstrument }
                    testId="instrument-select-option"
                />
            </li>
        )) }
 
        <li className="instrumentSelect-option instrumentSelect-option--addNew dropdown-menu-item">
            <AccountInstrumentUseNewButton
                onClick={ onUseNewInstrument }
                testId="instrument-select-option-use-new"
            />
        </li>
    </ul>;
};
 
interface AccountInstrumentSelectButtonProps {
    instrument?: AccountInstrument;
    testId?: string;
    onClick?(): void;
}
 
const AccountInstrumentSelectButton: FunctionComponent<AccountInstrumentSelectButtonProps> = ({
    instrument,
    testId,
    onClick,
}) => {
    if (!instrument) {
        return (
            <AccountInstrumentUseNewButton
                className="instrumentSelect-button optimizedCheckout-form-select dropdown-button form-input"
                testId={ testId }
            />
        );
    }
 
    return (
        !isBankAccountInstrument(instrument) ?
            (<AccountInstrumentMenuItem
                className="instrumentSelect-button optimizedCheckout-form-select dropdown-button form-input"
                instrument={ instrument }
                onClick={ onClick }
                testId={ testId }
            />) :
            (<BankInstrumentMenuItem
                className="instrumentSelect-button optimizedCheckout-form-select dropdown-button form-input"
                instrument={ instrument }
                onClick={ onClick }
                testId={ testId }
            />)
    );
};
 
interface AccountInstrumentOptionProps {
    instrument: AccountInstrument;
    testId?: string;
    onClick?(token: string): void;
}
 
const AccountInstrumentOption: FunctionComponent<AccountInstrumentOptionProps> = ({
    instrument,
    onClick = noop,
}) => {
    const handleClick = useCallback(() => {
        onClick(instrument.bigpayToken);
    }, [
        onClick,
        instrument,
    ]);
 
    return (
        !isBankAccountInstrument(instrument) ?
            (<AccountInstrumentMenuItem
                instrument={ instrument }
                onClick={ handleClick }
                testId="instrument-select-option"
            />) :
            (<BankInstrumentMenuItem
                instrument={ instrument }
                onClick={ handleClick }
                testId="instrument-select-option"
            />)
    );
};
 
interface AccountInstrumentMenuItemProps {
    className?: string;
    instrument: AccountInstrument;
    testId?: string;
    onClick?(): void;
}
 
const AccountInstrumentMenuItem: FunctionComponent<AccountInstrumentMenuItemProps> = ({
    className,
    instrument: { externalId },
    testId,
    onClick,
}) => {
    return (
        <button
            className={ className }
            data-test={ testId }
            onClick={ onClick }
            type="button"
        >
            <div className="instrumentSelect-details">
                {
                    // TODO: When we include new account instrument types we can
                    // abstract these icons in a similar way we did for credit cards.
                }
                <IconPaypal
                    additionalClassName="accountIcon-icon"
                    size={ IconSize.Medium }
                />
 
                <div
                    className="instrumentSelect-account"
                    data-test={ `${testId}-externalId` }
                >
                    { externalId }
                </div>
            </div>
        </button>
    );
};
 
interface BankInstrumentMenuItemProps {
    className?: string;
    instrument: BankInstrument;
    testId?: string;
    onClick?(): void;
}
 
const BankInstrumentMenuItem: FunctionComponent<BankInstrumentMenuItemProps> = ({
      className,
      instrument,
      testId,
      onClick,
}) => {
    const issuerName = `Issuer: ${instrument.issuer}`;
    const accountNumber = `Account number ending in: ${instrument.accountNumber}`;
 
    return (
        <button
            className={ className }
            data-test={ testId }
            onClick={ onClick }
            type="button"
        >
            <div className="instrumentSelect-details">
                {
                    // TODO: When we include new account instrument types we can
                    // abstract these icons in a similar way we did for credit cards.
                }
                <div className="instrumentSelect-card">
                    { accountNumber }
                </div>
                <div className="instrumentSelect-issuer">
                    { issuerName }
                </div>
            </div>
        </button>
    );
};
 
interface AccountInstrumentUseNewButtonProps {
    className?: string;
    testId?: string;
    onClick?(): void;
}
 
const AccountInstrumentUseNewButton: FunctionComponent<AccountInstrumentUseNewButtonProps> = ({
    className,
    testId,
    onClick = noop,
}) => (
    <button
        className={ className }
        data-test={ testId }
        onClick={ onClick }
        type="button"
    >
        <div className="instrumentSelect-details instrumentSelect-details--addNew">
            <IconNewAccount
                additionalClassName="accountIcon-icon"
                size={ IconSize.Medium }
            />
 
            <div className="instrumentSelect-account">
                <TranslatedString id="payment.account_instrument_add_action" />
            </div>
        </div>
    </button>
);
 
export default AccountInstrumentSelect;