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 | 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 32x 43x 46x 46x 46x 46x 46x 43x 46x 46x 46x 46x 46x 4x 42x 42x 42x 42x 42x 16x 26x 43x 46x 46x 46x 4x 42x 32x 9x 9x 32x 1x 1x 1x 1x 1x 32x 1x 1x 1x 2x 1x 1x 32x 4x 43x 43x 40x 40x 80x 40x 43x | import { CheckoutSelectors, PaymentInstrument } from '@bigcommerce/checkout-sdk'; import { noop } from 'lodash'; import React, { Component, Fragment, ReactNode } from 'react'; import { withCheckout, CheckoutContextProps } from '../../checkout'; import { TranslatedString } from '../../locale'; import { Button, ButtonSize, ButtonVariant } from '../../ui/button'; import { Modal, ModalHeader } from '../../ui/modal'; import isAccountInstrument from './isAccountInstrument'; import isBankAccountInstrument from './isBankAccountInstrument'; import isCardInstrument from './isCardInstrument'; import ManageAccountInstrumentsTable from './ManageAccountInstrumentsTable'; import ManageCardInstrumentsTable from './ManageCardInstrumentsTable'; import ManageInstrumentsAlert from './ManageInstrumentsAlert'; export interface ManageInstrumentsModalProps { isOpen: boolean; instruments: PaymentInstrument[]; onAfterOpen?(): void; onDeleteInstrument?(instrumentId: string): void; onDeleteInstrumentError?(error: Error): void; onRequestClose?(): void; } export interface ManageInstrumentsModalState { isConfirmingDelete: boolean; selectedInstrumentId?: string; } interface WithCheckoutProps { deleteInstrumentError?: Error; isDeletingInstrument: boolean; isLoadingInstruments: boolean; clearError(error: Error): Promise<CheckoutSelectors>; deleteInstrument(id: string): Promise<CheckoutSelectors>; } class ManageInstrumentsModal extends Component<ManageInstrumentsModalProps & WithCheckoutProps, ManageInstrumentsModalState> { state: ManageInstrumentsModalState = { isConfirmingDelete: false, }; render(): ReactNode { const { deleteInstrumentError, isOpen, onRequestClose, } = this.props; return ( <Modal closeButtonLabel={ <TranslatedString id="common.close_action" /> } footer={ this.renderFooter() } header={ <ModalHeader> <TranslatedString id="payment.instrument_manage_modal_title_text" /> </ModalHeader> } isOpen={ isOpen } onAfterOpen={ this.handleAfterOpen } onRequestClose={ onRequestClose } > { deleteInstrumentError && <ManageInstrumentsAlert error={ deleteInstrumentError } /> } { this.renderContent() } </Modal> ); } private renderContent(): ReactNode { const { instruments, isDeletingInstrument, } = this.props; const { isConfirmingDelete } = this.state; if (isConfirmingDelete) { return ( <p><TranslatedString id="payment.instrument_manage_modal_confirmation_label" /></p> ); } const cardInstruments = instruments.filter(isCardInstrument); const bankInstruments = instruments.filter(isBankAccountInstrument); const accountInstruments = instruments.filter(isAccountInstrument); const bankAndAccountInstruments = [...bankInstruments, ...accountInstruments]; if (bankAndAccountInstruments.length) { return ( <ManageAccountInstrumentsTable instruments={ bankAndAccountInstruments } isDeletingInstrument={ isDeletingInstrument } onDeleteInstrument={ this.handleDeleteInstrument } /> ); } return ( <ManageCardInstrumentsTable instruments={ cardInstruments } isDeletingInstrument={ isDeletingInstrument } onDeleteInstrument={ this.handleDeleteInstrument } /> ); } private renderFooter(): ReactNode { const { isDeletingInstrument, isLoadingInstruments, onRequestClose } = this.props; const { isConfirmingDelete } = this.state; if (isConfirmingDelete) { return ( <Fragment> <Button data-test="manage-instrument-cancel-button" onClick={ this.handleCancel } size={ ButtonSize.Small } > <TranslatedString id="common.cancel_action" /> </Button> <Button data-test="manage-instrument-confirm-button" disabled={ isDeletingInstrument || isLoadingInstruments } onClick={ this.handleConfirmDelete } size={ ButtonSize.Small } variant={ ButtonVariant.Primary } > <TranslatedString id="payment.instrument_manage_modal_confirmation_action" /> </Button> </Fragment> ); } return ( <Button data-test="manage-instrument-close-button" onClick={ onRequestClose } size={ ButtonSize.Small } > <TranslatedString id="common.close_action" /> </Button> ); } private handleAfterOpen: () => void = () => { const { onAfterOpen } = this.props; this.setState({ isConfirmingDelete: false, }, onAfterOpen); }; private handleCancel: () => void = () => { const { clearError, deleteInstrumentError, } = this.props; Iif (deleteInstrumentError) { clearError(deleteInstrumentError); } this.setState({ isConfirmingDelete: false, }); }; private handleConfirmDelete: () => void = async () => { const { deleteInstrument, onDeleteInstrument = noop, onDeleteInstrumentError = noop, onRequestClose = noop } = this.props; const { selectedInstrumentId } = this.state; Iif (!selectedInstrumentId) { return; } try { await deleteInstrument(selectedInstrumentId); onDeleteInstrument(selectedInstrumentId); onRequestClose(); } catch (error) { onDeleteInstrumentError(error); } }; private handleDeleteInstrument: (id: string) => void = id => { this.setState({ isConfirmingDelete: true, selectedInstrumentId: id, }); }; } export function mapFromCheckoutProps( { checkoutService, checkoutState }: CheckoutContextProps ): WithCheckoutProps | null { const { errors: { getDeleteInstrumentError }, statuses: { isDeletingInstrument, isLoadingInstruments }, } = checkoutState; return { clearError: checkoutService.clearError, deleteInstrument: checkoutService.deleteInstrument, deleteInstrumentError: getDeleteInstrumentError(), isDeletingInstrument: isDeletingInstrument(), isLoadingInstruments: isLoadingInstruments(), }; } export default withCheckout(mapFromCheckoutProps)(ManageInstrumentsModal); |