Rev 1668 | Rev 3582 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
import { v4 as uuid } from 'uuid'
import { notificationActionTypes } from './notification.types'
const NotificationInitialState = {
notifications: []
}
const notificationReducer = (
state = NotificationInitialState,
{ type, payload }
) => {
switch (type) {
case notificationActionTypes.ADD_NOTIFICATION: {
const newNotification = { ...payload, id: uuid() }
const alreadyExist = state.notifications.find(
(notification) => notification.msg === newNotification.msg
)
if (alreadyExist) return state
return {
...state,
notifications: [newNotification, ...state.notifications]
}
}
case notificationActionTypes.REMOVE_NOTIFICATION: {
const idToFilter = payload
return {
...state,
notifications: state.notifications.filter(
(element) => element.id !== idToFilter
)
}
}
default:
return state
}
}
export default notificationReducer