{"size": 1286, "ext": "js", "max_stars_repo_path": "app/redux/modules/notifications.js", "max_stars_repo_name": "MacKentoch/magic-markdown-to-html", "max_stars_repo_stars_event_min_datetime": "2017-02-26T08:35:50.000Z", "max_stars_repo_stars_event_max_datetime": "2017-02-26T08:35:50.000Z", "max_issues_repo_path": "app/redux/modules/notifications.js", "max_issues_repo_name": "MacKentoch/electron-react-file-management", "max_issues_repo_issues_event_min_datetime": "2019-09-18T07:00:19.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-10T17:23:20.000Z", "max_forks_repo_path": "app/redux/modules/notifications.js", "max_forks_repo_name": "MacKentoch/magic-markdown-to-html", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 4, "max_forks_count": null, "avg_line_length": 21.7966101695, "max_line_length": 95, "alphanum_fraction": 0.565318818}
import { Map, List } from 'immutable';

// /////////////////////
// constants
// /////////////////////
const ADD_NOTIFICATION = 'ADD_NOTIFICATION';
const REMOVE_NOTIFICATION = 'REMOVE_NOTIFICATION';

// /////////////////////
// reducer
// /////////////////////
const initialState = List();
// NOTE: a notification is an object like:
//   {
//     key: 2,
//     message: 'test message',
//     dismissAfter: 1000,
//     action: 'Dismiss',
//     onClick: () => this.key // returns its own key (needed to dismiss notification)
//   }

export default function (state = initialState, action) {
  switch (action.type) {

    case ADD_NOTIFICATION:
      return state.push(
        Map({
          key: state.size + 1,
          ...action.notification,
          onClick: () => removeNotification(this)
        })
      );

    case REMOVE_NOTIFICATION:
      return state.filter(notification => notification.get('key') !== action.notification.key);

    default:
      return state;
  }
}


// /////////////////////
// action creators
// /////////////////////
export function addNotification(notification) {
  return {
    type: ADD_NOTIFICATION,
    notification
  };
}

export function removeNotification(notification) {
  return {
    type: REMOVE_NOTIFICATION,
    notification
  };
}
