The code powering m.abunchtell.com https://m.abunchtell.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

58 lines
1.7 KiB

  1. import {
  2. NOTIFICATIONS_UPDATE,
  3. NOTIFICATIONS_REFRESH_SUCCESS,
  4. NOTIFICATIONS_EXPAND_SUCCESS
  5. } from '../actions/notifications';
  6. import Immutable from 'immutable';
  7. const initialState = Immutable.Map({
  8. items: Immutable.List(),
  9. next: null,
  10. loaded: false
  11. });
  12. const notificationToMap = notification => Immutable.Map({
  13. id: notification.id,
  14. type: notification.type,
  15. account: notification.account.id,
  16. status: notification.status ? notification.status.id : null
  17. });
  18. const normalizeNotification = (state, notification) => {
  19. return state.update('items', list => list.unshift(notificationToMap(notification)));
  20. };
  21. const normalizeNotifications = (state, notifications, next) => {
  22. let items = Immutable.List();
  23. const loaded = state.get('loaded');
  24. notifications.forEach((n, i) => {
  25. items = items.set(i, notificationToMap(n));
  26. });
  27. return state.update('items', list => loaded ? list.unshift(...items) : list.push(...items)).set('next', next).set('loaded', true);
  28. };
  29. const appendNormalizedNotifications = (state, notifications, next) => {
  30. let items = Immutable.List();
  31. notifications.forEach((n, i) => {
  32. items = items.set(i, notificationToMap(n));
  33. });
  34. return state.update('items', list => list.push(...items)).set('next', next);
  35. };
  36. export default function notifications(state = initialState, action) {
  37. switch(action.type) {
  38. case NOTIFICATIONS_UPDATE:
  39. return normalizeNotification(state, action.notification);
  40. case NOTIFICATIONS_REFRESH_SUCCESS:
  41. return normalizeNotifications(state, action.notifications, action.next);
  42. case NOTIFICATIONS_EXPAND_SUCCESS:
  43. return appendNormalizedNotifications(state, action.notifications, action.next);
  44. default:
  45. return state;
  46. }
  47. };