49 lines
1.1 KiB
JavaScript
Raw Normal View History

2016-09-30 18:25:04 +02:00
/*
*
* NotificationProvider reducer
*
*/
import { fromJS } from 'immutable';
import {
SHOW_NOTIFICATION,
HIDE_NOTIFICATION,
} from './constants';
const initialState = fromJS({
notifications: [],
});
function notificationProviderReducer(state = initialState, action) {
2016-10-05 11:32:31 +02:00
// Init variable
let index;
2016-09-30 18:25:04 +02:00
switch (action.type) {
case SHOW_NOTIFICATION:
return state.set('notifications', state.get('notifications').push({
message: action.message,
status: action.status,
id: action.id,
}));
case HIDE_NOTIFICATION:
// Check that the index exists
state.get('notifications').forEach((notification, i) => {
if (notification.id === action.id) {
index = i;
}
});
if (typeof index !== 'undefined') {
// Remove the notification
return state.set('notifications', state.get('notifications').splice(index, 1));
}
2016-10-05 11:32:31 +02:00
// Notification not found, return the current state
return state;
2016-09-30 18:25:04 +02:00
default:
return state;
}
}
export default notificationProviderReducer;