# All files / composer/components Notification.jsx

0% Statements 0/11
0% Branches 0/18
0% Functions 0/4
0% Lines 0/11

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

     /* eslint-disable jsx-a11y/label-has-for */

    import React from 'react';
    import PropTypes from 'prop-types';
    import { NotificationTypes } from '../AppConstants';
    import CloseButton from '../components/CloseButton';
    import styles from './css/Notification.css';
    import NotificationActionCreators from '../action-creators/NotificationActionCreators';

    import Button from '../components/Button';

    class Notification extends React.Component {
      static propTypes = {
        id: PropTypes.string.isRequired,
        type: PropTypes.string.isRequired,
        message: PropTypes.string.isRequired,
        showCloseIcon: PropTypes.bool.isRequired,
        showSoftAndHardCloseOptions: PropTypes.bool.isRequired,
        onClose: PropTypes.func,
        className: PropTypes.string,
        classNames: PropTypes.shape({
          notification: PropTypes.string,
          closeButton: PropTypes.string,
        }),
        cta: PropTypes.shape({
          label: PropTypes.string,
          action: PropTypes.func,
        }),
        children: PropTypes.node,
      };

      static defaultProps = {
        onClose: () => {},
        className: null,
        classNames: {
          notification: null,
          closeButton: null,
        },
        cta: null,
      };

      onCloseButtonClick = () => {
        const { id, showSoftAndHardCloseOptions, onClose } = this.props;
        const data = showSoftAndHardCloseOptions ? {
          isHardCloseCheckboxChecked: this.hardCloseCheckbox.checked,
        } : {};

        NotificationActionCreators.removeNotification(id, data);
        onClose();
      };

      render() {
        const {
          type,
          message,
          showCloseIcon,
          showSoftAndHardCloseOptions,
          className,
          classNames,
          cta,
          children,
        } = this.props;

        const htmlMessage = { __html: message };

        const notificationClassNamesMap = {
          [NotificationTypes.ERROR]: styles.errorNotification,
          [NotificationTypes.SUCCESS]: styles.successNotification,
          [NotificationTypes.INFO]: styles.infoNotification,
        };

        const notificationClassName = [
          (showCloseIcon && showSoftAndHardCloseOptions ? styles.notificationWithCloseButtonAndCheckbox :
            showCloseIcon && !showSoftAndHardCloseOptions ? styles.notificationWithCloseButton :
              styles.notification),
          notificationClassNamesMap[type],
          classNames.notification || className,
        ].join(' ');

        const closeButtonClassName = [
          'bi bi-x',
          styles.closeButton,
          classNames.closeButton,
        ].join(' ');

        return (
          <div className={notificationClassName}>
            {cta &&
              <div className={styles.ctaContainer}>
                <Button onClick={cta.action} className={styles.cta}>
                  {cta.label}
                </Button>
              </div>
            }
            <div dangerouslySetInnerHTML={htmlMessage} />

            {showCloseIcon &&
              <CloseButton
                onClick={this.onCloseButtonClick}
                className={closeButtonClassName}
              />}

            {children}

            {showSoftAndHardCloseOptions &&
              <div className={styles.hardCloseCheckboxContainer}>
                <label>
                  <input
                    type="checkbox"
                    className={styles.hardCloseCheckbox}
                    ref={(el) => { this.hardCloseCheckbox = el; }}
                  />
                  Don't show this message again
                </label>
              </div>}
          </div>
        );
      }
    }

    export default Notification;