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.

84 lines
2.5 KiB

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