All files / app/common/error ErrorModal.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  79x 79x   79x 79x 79x 79x   79x 79x 79x 79x                           79x 79x 55x   55x                         79x   55x 55x 55x   55x               79x   55x 55x 55x   55x                     79x 55x                   79x   55x 55x 55x   55x 31x     24x 1x           23x   23x       23x     45x   6x 6x 6x   6x 6x     79x  
import { RequestError } from '@bigcommerce/checkout-sdk';
import { noop } from 'lodash';
import React, { Fragment, PureComponent, ReactNode, SyntheticEvent } from 'react';
 
import { TranslatedString } from '../../locale';
import { Button, ButtonSize } from '../../ui/button';
import { IconError, IconSize } from '../../ui/icon';
import { Modal, ModalHeader } from '../../ui/modal';
 
import computeErrorCode from './computeErrorCode';
import isCustomError from './isCustomError';
import isRequestError from './isRequestError';
import ErrorCode from './ErrorCode';
 
export interface ErrorModalProps {
    error?: Error | RequestError;
    message?: ReactNode;
    title?: ReactNode;
    shouldShowErrorCode?: boolean;
    onClose?(event: Event, props: ErrorModalOnCloseProps): void;
}
 
export interface ErrorModalOnCloseProps {
    error: Error;
}
 
export default class ErrorModal extends PureComponent<ErrorModalProps> {
    render(): ReactNode {
        const { error } = this.props;
 
        return (
            <Modal
                additionalModalClassName="modal--error"
                footer={ this.renderFooter() }
                header={ this.renderHeader() }
                isOpen={ !!error }
                onRequestClose={ this.handleOnRequestClose }
            >
                { this.renderBody() }
            </Modal>
        );
    }
 
    private renderHeader(): ReactNode {
        const {
            error,
            title = error && isCustomError(error) && error.title,
        } = this.props;
 
        return (
            <ModalHeader>
                <IconError additionalClassName="icon--error modal-header-icon" size={ IconSize.Small } />
                { title || <TranslatedString id="common.error_heading" /> }
            </ModalHeader>
        );
    }
 
    private renderBody(): ReactNode {
        const {
            error,
            message = error && error.message,
        } = this.props;
 
        return (
            <Fragment>
                { message && <p>{ message }</p> }
 
                <div className="optimizedCheckout-contentSecondary">
                    { this.renderErrorCode() }
                </div>
            </Fragment>
        );
    }
 
    private renderFooter(): ReactNode {
        return (
            <Button
                onClick={ this.handleOnRequestClose }
                size={ ButtonSize.Small }
            >
                <TranslatedString id="common.ok_action" />
            </Button>
        );
    }
 
    private renderErrorCode(): ReactNode {
        const {
            error,
            shouldShowErrorCode = true,
        } = this.props;
 
        if (!error || !shouldShowErrorCode) {
            return;
        }
 
        if (isRequestError(error) && error?.headers?.['x-request-id']) {
            return <ErrorCode
                code={ error.headers['x-request-id'] }
                label={ <TranslatedString id="common.request_id" /> }
            />;
        }
 
        const errorCode = computeErrorCode(error);
 
        Iif (!errorCode) {
            return;
        }
 
        return <ErrorCode code={ errorCode } />;
    }
 
    private handleOnRequestClose: (event: SyntheticEvent) => void = event => {
        const {
            error,
            onClose = noop,
        } = this.props;
 
        Eif (error) {
            onClose(event.nativeEvent, { error });
        }
    };
}