Home Reference Source

application/redux/reducers/container-reducers.js

  1. import _ from 'lodash';
  2. import Immutable from 'immutable';
  3. import {
  4. CONTAINERS_REQUEST,
  5. CONTAINERS_RECEIVE,
  6. CONTAINER_RECEIVE,
  7. CONTAINERS_INVALIDATE,
  8. CONTAINER_SELECTED,
  9. CONTAINERS_RECEIVE_PAGINATION
  10. } from './../actions/container-actions';
  11.  
  12. const PER_PAGE = 20;
  13.  
  14. export function containers(state = null, action) {
  15. if (state === null) {
  16. state = {
  17. isFetching: false,
  18. didInvalidate: false,
  19. items: Immutable.fromJS({}),
  20. open: Immutable.fromJS({}),
  21. selected: false,
  22. order: "title",
  23. dir: "asc",
  24. page: 0,
  25. pagination: {
  26. before: 0,
  27. current: 0,
  28. first: 0,
  29. last: 0,
  30. limit: 0,
  31. next: 0,
  32. total_items: 0,
  33. total_pages: 0
  34. },
  35. };
  36. }
  37.  
  38. switch (action.type) {
  39. case CONTAINERS_RECEIVE_PAGINATION:
  40. return Object.assign({}, state, {
  41. pagination: {
  42. 'before': action.payload.before,
  43. 'current': action.payload.current,
  44. 'first': action.payload.first,
  45. 'last': action.payload.last,
  46. 'limit': action.payload.limit,
  47. 'next': action.payload.next,
  48. 'total_items': action.payload.total_items,
  49. 'total_pages': action.payload.total_pages,
  50. }
  51. });
  52. case CONTAINERS_INVALIDATE:
  53. return Object.assign({}, state, {
  54. didInvalidate: true,
  55. });
  56. case CONTAINERS_REQUEST:
  57. return Object.assign({}, state, {
  58. isFetching: true,
  59. didInvalidate: false
  60. });
  61. case CONTAINER_SELECTED: {
  62. return Object.assign({}, state, {
  63. selected: action.payload.selected
  64. });
  65. }
  66. case CONTAINER_RECEIVE:
  67. const container = action.payload.container;
  68. let found = false;
  69. let containers = state.items.map((item, i) => {
  70. if (item.get('uuid') == container.get('uuid')) {
  71. found = true;
  72. return container;
  73. }
  74. return item;
  75. });
  76.  
  77. if (!found) {
  78. let col = containers.toJS();
  79. if (col && col.length) {
  80. col.push(container);
  81. } else {
  82. col = [container];
  83. }
  84. containers = Immutable.fromJS(col);
  85. }
  86.  
  87. action.payload.containers = Immutable.fromJS(containers);
  88. case CONTAINERS_RECEIVE:
  89. return Object.assign({}, state, {
  90. isFetching: false,
  91. didInvalidate: false,
  92. items: action.payload.containers
  93. });
  94. }
  95.  
  96. return state;
  97. };