All files / app/order OrderSummaryPrice.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 14311x 11x 11x   11x 11x                                       104x   104x 9x     95x 1x     94x       104x     104x 11x 104x           104x         11x   104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x   104x 104x   104x                                                                                                           104x                         11x   11x  
import classNames from 'classnames';
import React, { Component, ReactNode } from 'react';
import { CSSTransition } from 'react-transition-group';
 
import { preventDefault } from '../common/dom';
import { ShopperCurrency } from '../currency';
 
export interface OrderSummaryPriceProps {
    label: ReactNode;
    amount?: number | null;
    zeroLabel?: ReactNode;
    className?: string;
    testId?: string;
    currencyCode?: string;
    superscript?: string;
    actionLabel?: ReactNode;
    onActionTriggered?(): void;
}
 
export interface OrderSummaryPriceState {
    highlight: boolean;
    previousAmount?: number;
}
 
function getDisplayValue(amount?: number | null, zeroLabel?: ReactNode): ReactNode | number {
    const notYetSetSymbol = '--';
 
    if (typeof amount === 'undefined' || amount === null) {
        return notYetSetSymbol;
    }
 
    if (zeroLabel && amount === 0) {
        return zeroLabel;
    }
 
    return amount;
}
 
function isNumberValue(displayValue: number | ReactNode): displayValue is number {
    return typeof displayValue === 'number';
}
 
class OrderSummaryPrice extends Component<OrderSummaryPriceProps, OrderSummaryPriceState> {
    static getDerivedStateFromProps(props: OrderSummaryPriceProps, state: OrderSummaryPriceState) {
        return {
            highlight: props.amount !== state.previousAmount,
            previousAmount: props.amount,
        };
    }
 
    state = {
        highlight: false,
        previousAmount: 0,
    };
 
    render(): ReactNode {
        const {
            amount,
            actionLabel,
            onActionTriggered,
            children,
            className,
            currencyCode,
            label,
            superscript,
            testId,
            zeroLabel,
        } = this.props;
 
        const { highlight } = this.state;
        const displayValue = getDisplayValue(amount, zeroLabel);
 
        return (
            <div data-test={ testId }>
                <CSSTransition
                    addEndListener={ this.handleTransitionEnd }
                    classNames="changeHighlight"
                    in={ highlight }
                    timeout={ {} }
                >
                    <div
                        aria-live="polite"
                        className={ classNames(
                            'cart-priceItem',
                            'optimizedCheckout-contentPrimary',
                            className
                        ) }
                    >
                        <span className="cart-priceItem-label">
                            <span data-test="cart-price-label">
                                { label }
                                { '  ' }
                            </span>
                            { currencyCode && <span className="cart-priceItem-currencyCode">
                                { `(${currencyCode}) ` }
                            </span> }
                            { onActionTriggered && actionLabel && <span className="cart-priceItem-link">
                                <a
                                    data-test="cart-price-callback"
                                    href="#"
                                    onClick={ preventDefault(onActionTriggered) }
                                >
                                    { actionLabel }
                                </a>
                            </span> }
                        </span>
 
                        <span className="cart-priceItem-value">
                            <span data-test="cart-price-value">
                                { isNumberValue(displayValue) ?
                                    <ShopperCurrency amount={ displayValue } /> :
                                    displayValue }
                            </span>
 
                            { superscript && <sup data-test="cart-price-value-superscript">
                                { superscript }
                            </sup> }
                        </span>
 
                        { children }
                    </div>
                </CSSTransition>
            </div>
        );
    }
 
    private handleTransitionEnd: (node: HTMLElement, done: () => void) => void = (node, done) => {
        const { previousAmount } = this.state;
 
        node.addEventListener('animationend', ({ target }) => {
            if (target === node) {
                this.setState({
                    highlight: false,
                    previousAmount,
                });
                done();
            }
        });
    };
}
 
export default OrderSummaryPrice;