Home Reference Source

application/redux/reducers/acl-reducers.js

  1. import _ from 'lodash';
  2. import Immutable from 'immutable';
  3. import {
  4. ACLS_REQUEST,
  5. ACLS_RECEIVE,
  6. ACL_RECEIVE,
  7. ACLS_RESOURCES_RECEIVE,
  8. ACLS_INVALIDATE,
  9. ACL_SELECTED,
  10. ACLS_SET_PAGE
  11. } from './../actions/acl-actions';
  12.  
  13. const PER_PAGE = 20;
  14.  
  15. export function acls(state = null, action) {
  16. if (state === null) {
  17. state = {
  18. isFetching: false,
  19. didInvalidate: false,
  20. items: Immutable.fromJS({}),
  21. selected: false,
  22. page: 0,
  23. order: "name",
  24. dir: "asc",
  25. filter: false,
  26. resources: {}
  27. };
  28. }
  29.  
  30. let newState;
  31.  
  32. switch (action.type) {
  33. case ACLS_INVALIDATE:
  34. return Object.assign({}, state, {
  35. didInvalidate: true,
  36. });
  37. case ACLS_REQUEST:
  38. return Object.assign({}, state, {
  39. isFetching: true,
  40. didInvalidate: false
  41. });
  42. case ACL_SELECTED: {
  43. return Object.assign({}, state, {
  44. selected: action.payload.selected
  45. });
  46. }
  47. case ACLS_RESOURCES_RECEIVE:
  48. return Object.assign({}, state, {
  49. resources: action.payload.resources
  50. });
  51. case ACL_RECEIVE:
  52. const acl = action.payload.acl;
  53. const acls = state.items.map((item, i) => {
  54. if (item.get('name') == acl.get('name')) {
  55. return acl;
  56. }
  57. return item;
  58. });
  59.  
  60. action.payload.acls = Immutable.fromJS(acls);
  61. case ACLS_RECEIVE:
  62. return Object.assign({}, state, {
  63. isFetching: false,
  64. didInvalidate: false,
  65. items: action.payload.acls
  66. });
  67. }
  68.  
  69. return state;
  70. };