# All files / composer/stores NotificationStore.js

29.27% Statements 24/82
4.35% Branches 2/46
9.09% Functions 2/22
32.39% Lines 23/71

    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

    7x
    7x

    7x

    7x

    7x

    7x

    7x

    7x

    7x

    7x

    7x

    7x

    7x

    7x

    7x

    7x
    45x
    45x

    45x

    45x
    45x

    45x

    7x

     import { EventEmitter } from 'events';
    import cookies from 'js-cookie';
    import AppDispatcher from '../dispatcher';
    import { ActionTypes, NotificationTypes, NotificationScopes } from '../AppConstants';
    import { generateUniqueId } from '../utils/StringUtils';

    const CHANGE_EVENT = 'change';
    const CLOSED_NOTIFICATIONS_COOKIE_KEY = 'mc-closed-notifications';

    const getInitialState = () => ({
      visibleNotifications: [],
      // A pending queue might prove useful at some point to have finer control over
      // what notifications get displayed, and when (also update queueNotification()
      // and its unique id generation logic if enabling that pending set of notifications)
      // pendingNotifications: [],
    });

    let state = getInitialState();

    const getNewErrorNotification = (id, {
      scope,
      errorCode,
      message = '',
      onlyCloseOnce = false,
      showSoftAndHardCloseOptions = false,
      data = {},
    }) => ({
      type: NotificationTypes.ERROR,
      id,
      scope,
      errorCode,
      message,
      onlyCloseOnce,
      showSoftAndHardCloseOptions,
      data,
    });

    const getNewSuccessNotification = (id, {
      scope,
      message = '',
      onlyCloseOnce = false,
      showSoftAndHardCloseOptions = false,
      data = {},
    }) => ({
      type: NotificationTypes.SUCCESS,
      id,
      scope,
      message,
      onlyCloseOnce,
      showSoftAndHardCloseOptions,
      data,
    });

    const getNewInfoNotification = (id, {
      scope,
      message = '',
      onlyCloseOnce = false,
      showSoftAndHardCloseOptions = false,
      data = {},
      cta,
    }) => ({
      type: NotificationTypes.INFO,
      id,
      scope,
      message,
      onlyCloseOnce,
      showSoftAndHardCloseOptions,
      data,
      cta,
    });

    const NotificationStore = Object.assign({}, EventEmitter.prototype, {
      emitChange: () => NotificationStore.emit(CHANGE_EVENT),
      addChangeListener: (callback) => NotificationStore.on(CHANGE_EVENT, callback),
      removeChangeListener: (callback) => NotificationStore.removeListener(CHANGE_EVENT, callback),

      getVisibleNotifications: () => state.visibleNotifications,
    });

    const getClosedNotifications = () => cookies.getJSON(CLOSED_NOTIFICATIONS_COOKIE_KEY) || [];
    const setClosedNotifications =
      (notifs) => cookies.set(CLOSED_NOTIFICATIONS_COOKIE_KEY, notifs, { expires: 365 * 4 });

    const shouldShowNotification = (notification) => {
      if (!notification.onlyCloseOnce) return true;

      const closedNotifications = getClosedNotifications();
      const wasAlreadyClosed = closedNotifications.includes(notification.scope);

      return !wasAlreadyClosed;
    };

    const queueNotification = ({ isUnique, ...actionData }) => {
      // Remove prev notifications that share the same scope when a scope is identified as unique
      if (isUnique) removeAllNotificationsByScope(actionData.scope, false);

      const existingNotificationIds = state.visibleNotifications.map((notif) => notif.id);
      const id = generateUniqueId(existingNotificationIds);
      let notification;

      switch (actionData.type) {
        case NotificationTypes.ERROR:
          notification = getNewErrorNotification(id, actionData);
          break;

        case NotificationTypes.SUCCESS:
          notification = getNewSuccessNotification(id, actionData);
          break;

        case NotificationTypes.INFO:
          notification = getNewInfoNotification(id, actionData);
          break;

        default:
          return;
      }

      if (shouldShowNotification(notification)) state.visibleNotifications.push(notification);
    };

    const removeNotification = (id, {
      comesFromDirectUserAction = true,
      isHardCloseCheckboxChecked,
    } = {}) => {
      const removedNotificationIndex = state.visibleNotifications.findIndex((notif) => notif.id === id);
      const removedNotification = state.visibleNotifications[removedNotificationIndex];

      /**
       * Sometimes `removeNotification()` called right after we've reset the app state
       * (this happens in `AppActionCreators.saveDrafts`) and so `state.visibleNotifications`
       * is empty. This check ensures we don't try and remove a non-existant notification.
       */
      if (!removedNotification) {
        return;
      }

      state.visibleNotifications.splice(removedNotificationIndex, 1);

      const rememberClosedNotification = (
        removedNotification.onlyCloseOnce &&
        (!removedNotification.showSoftAndHardCloseOptions ||
          (removedNotification.showSoftAndHardCloseOptions && isHardCloseCheckboxChecked)) &&
        comesFromDirectUserAction
      );

      if (rememberClosedNotification) {
        const closedNotifications = getClosedNotifications();
        setClosedNotifications([...closedNotifications, removedNotification.scope]);
      }
    };

    const removeAllNotificationsByScope = (scope, comesFromDirectUserAction) => {
      const notifications = state.visibleNotifications.filter((notif) =>
        notif.scope === scope
      );
      notifications.forEach((notif) => removeNotification(notif.id, { comesFromDirectUserAction }));
    };

    const removeComposerOmniboxNotices = (draftId) => {
      const notifications = state.visibleNotifications.filter((notif) =>
        notif.data.id === draftId &&
        notif.scope === NotificationScopes.MC_OMNIBOX_EDIT_NOTICE
      );
      notifications.forEach((notif) => removeNotification(notif.id));
    };

    const onDispatchedPayload = (payload) => {
      const action = payload.action;
      let isPayloadInteresting = true;

      switch (action.actionType) {
        case ActionTypes.QUEUE_NOTIFICATION:
          queueNotification(action);
          break;

        case ActionTypes.REMOVE_NOTIFICATION:
          removeNotification(action.id, action.data);
          break;

        case ActionTypes.REMOVE_ALL_NOTIFICATIONS_BY_SCOPE:
          removeAllNotificationsByScope(action.scope, action.comesFromDirectUserAction);
          break;

        case ActionTypes.REMOVE_COMPOSER_NOTICES:
          removeComposerOmniboxNotices(action.draftId);
          break;

        case ActionTypes.APP_RESET:
        case ActionTypes.APP_SOFT_RESET:
          state = getInitialState();
          break;

        default:
          isPayloadInteresting = false;
          break;
      }

      Iif (isPayloadInteresting) NotificationStore.emitChange();
    };

    AppDispatcher.register(onDispatchedPayload);

    export default NotificationStore;