Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2311 | Rev 3583 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3582 stevensc 1
import { v4 as uuid } from 'uuid';
2
import { notificationActionTypes } from './notification.types';
5 stevensc 3
 
4
const NotificationInitialState = {
5
  notifications: []
3582 stevensc 6
};
5 stevensc 7
 
3582 stevensc 8
const notificationReducer = (state = NotificationInitialState, { type, payload }) => {
5 stevensc 9
  switch (type) {
1668 stevensc 10
    case notificationActionTypes.ADD_NOTIFICATION: {
3582 stevensc 11
      const newNotification = { ...payload, id: uuid() };
2311 stevensc 12
 
13
      const alreadyExist = state.notifications.find(
14
        (notification) => notification.msg === newNotification.msg
3582 stevensc 15
      );
2311 stevensc 16
 
3582 stevensc 17
      if (alreadyExist) return state;
2311 stevensc 18
 
5 stevensc 19
      return {
20
        ...state,
21
        notifications: [newNotification, ...state.notifications]
3582 stevensc 22
      };
1668 stevensc 23
    }
24
    case notificationActionTypes.REMOVE_NOTIFICATION: {
3582 stevensc 25
      const idToFilter = payload;
5 stevensc 26
      return {
27
        ...state,
3582 stevensc 28
        notifications: state.notifications.filter((element) => element.id !== idToFilter)
29
      };
1668 stevensc 30
    }
5 stevensc 31
 
32
    default:
3582 stevensc 33
      return state;
5 stevensc 34
  }
3582 stevensc 35
};
5 stevensc 36
 
3582 stevensc 37
export default notificationReducer;