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.

64 lines
2.0 KiB

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