# All files / composer/components NotificationContainer.jsx

50% Statements 10/20
58.33% Branches 7/12
12.5% Functions 1/8
52.94% Lines 9/17

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

    4x

    4x

    4x
    2x

    4x
    2x

    4x

    4x

    4x

     /**
     * Component that displays visible notifications, with the ability to only
     * pay attention to a subset of notification scopes.
     *
     * Its styling can be customized at call site using either props.className or
     * props.classNames: the first to only extend the container's classname with another
     * classname, the second to extend both the container's and the notifications'
     * classnames with a map of classnames (see propTypes for shape).
     *
     * This component accepts children, which will then be used as children of the
     * individual notifications the component displays.
     */

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

    class NotificationContainer extends React.Component {
      static propTypes = {
        visibleNotifications: PropTypes.array.isRequired,
        scope: PropTypes.string,
        notScopes: PropTypes.array,
        type: PropTypes.string,
        notTypes: PropTypes.array,
        className: PropTypes.string,
        classNames: PropTypes.shape({
          container: PropTypes.string,
          notification: PropTypes.string,
          successNotification: PropTypes.string,
          errorNotification: PropTypes.string,
          notificationCloseButton: PropTypes.string,
        }),
        style: PropTypes.object,
        showCloseIcon: PropTypes.bool,
        children: PropTypes.node,
        onClick: PropTypes.func,
        onClose: PropTypes.func,
      };

      static defaultProps = {
        scope: null,
        notScopes: [],
        type: null,
        notTypes: [],
        className: null,
        classNames: {
          container: null,
          notification: null,
          successNotification: null,
          errorNotification: null,
          notificationCloseButton: null,
        },
        style: {},
        showCloseIcon: false,
        onClick: () => {},
        onClose: () => {},
      };

      render() {
        const {
          scope, notScopes, type, notTypes, className, classNames,
          showCloseIcon, children,
        } = this.props;

        let { visibleNotifications: notificationsToDisplay } = this.props;

        if (scope !== null) {
          notificationsToDisplay = notificationsToDisplay.filter((notif) => notif.scope === scope);
        }

        if (notScopes.length > 0) {
          notificationsToDisplay =
            notificationsToDisplay.filter((notif) => !notScopes.includes(notif.scope));
        }

        Iif (type !== null) {
          notificationsToDisplay = notificationsToDisplay.filter((notif) => notif.type === type);
        }

        Iif (notTypes.length > 0) {
          notificationsToDisplay =
            notificationsToDisplay.filter((notif) => !notTypes.includes(notif.type));
        }

        Eif (notificationsToDisplay.length === 0) return null;

        const containerStyles = [
          styles.notificationContainer,
          classNames.container || className,
        ].join(' ');

        const notificationClassNameMap = {
          [NotificationTypes.ERROR]: [
            classNames.notification,
            classNames.errorNotification,
          ].join(' '),

          [NotificationTypes.SUCCESS]: [
            classNames.notification,
            classNames.successNotification,
          ].join(' '),

          [NotificationTypes.INFO]: [
            classNames.notification,
            classNames.infoNotification,
          ].join(' '),
        };

        return (
          <div className={containerStyles} onClick={this.props.onClick} style={this.props.style}>
            {notificationsToDisplay.map((notif) => (
              <Notification
                id={notif.id}
                type={notif.type}
                message={notif.message}
                showCloseIcon={showCloseIcon}
                showSoftAndHardCloseOptions={notif.showSoftAndHardCloseOptions}
                onClose={this.props.onClose}
                classNames={{
                  notification: notificationClassNameMap[notif.type],
                  closeButton: classNames.notificationCloseButton,
                }}
                cta={notif.cta}
                key={notif.id}
              >
                {children}
              </Notification>
            ))}
          </div>
        );
      }
    }

    export default NotificationContainer;