Home Reference Source

application/redux/reducers/workflow-section-reducers.js

  1. import _ from 'lodash';
  2. import Immutable from 'immutable';
  3. import {
  4. WORKFLOW_SECTIONS_REQUEST,
  5. WORKFLOW_SECTIONS_RECEIVE,
  6. WORKFLOW_SECTION_RECEIVE,
  7. WORKFLOW_SECTIONS_INVALIDATE,
  8. WORKFLOW_SECTION_SELECTED,
  9. WORKFLOW_SECTIONS_FILTER_WEBONLY,
  10. WORKFLOW_SECTIONS_FILTER_PRINTONLY
  11. } from './../actions/workflow-section-actions';
  12.  
  13. export function workflowSections(state = null, action) {
  14. if (state === null) {
  15. state = {
  16. isFetching: false,
  17. didInvalidate: false,
  18. items: Immutable.fromJS({}),
  19. selected: false,
  20. order: "name",
  21. dir: "asc",
  22. filters: {
  23. webOnly: Immutable.fromJS({}),
  24. printOnly: Immutable.fromJS({})
  25. }
  26. };
  27. }
  28.  
  29. let newState;
  30.  
  31. switch (action.type) {
  32. case WORKFLOW_SECTIONS_FILTER_WEBONLY:
  33. // little expanation, filter -> sort -> map:
  34. let _wo_filtered = state.items.filter((item) => {
  35. // we only want web workflowSections
  36. return parseInt(item.get('is_web')) === 1;
  37. }).sort((a, b) => {
  38. // and we sort by priority
  39. if (parseInt(a.get('priority')) > parseInt(b.get('priority'))) {
  40. return 1;
  41. } else if (parseInt(a.get('priority')) < parseInt(b.get('priority'))) {
  42. return -1;
  43. }
  44. return 0;
  45. }).map((item) => {
  46. /*
  47. then only return the UUID
  48. we'll use that to fetch the workflow
  49. from the workflows.items iterable
  50. check content-item.js:533 (line starts with {this.props.workflows.filters.webOnly.map((id, i) => {)
  51. for info on how this works in the real world
  52. */
  53. return item.get('uuid');
  54. });
  55.  
  56. return Object.assign({}, state, {
  57. filters: Object.assign({}, state.filters, {
  58. webOnly: _wo_filtered
  59. })
  60. });
  61. case WORKFLOW_SECTIONS_FILTER_PRINTONLY:
  62. let _po_filtered = state.items.filter((item) => {
  63. return parseInt(item.get('is_print')) === 1;
  64. }).sort((a, b) => {
  65. if (parseInt(a.get('priority')) > parseInt(b.get('priority'))) {
  66. return 1;
  67. } else if (parseInt(a.get('priority')) < parseInt(b.get('priority'))) {
  68. return -1;
  69. }
  70. return 0;
  71. }).map((item) => {
  72. return item.get('uuid');
  73. });
  74.  
  75. return Object.assign({}, state, {
  76. filters: Object.assign({}, state.filters, {
  77. printOnly: _po_filtered
  78. })
  79. });
  80. case WORKFLOW_SECTIONS_INVALIDATE:
  81. return Object.assign({}, state, {
  82. didInvalidate: true,
  83. });
  84. case WORKFLOW_SECTIONS_REQUEST:
  85. return Object.assign({}, state, {
  86. isFetching: true,
  87. didInvalidate: false
  88. });
  89. case WORKFLOW_SECTION_SELECTED: {
  90. return Object.assign({}, state, {
  91. selected: action.payload.selected
  92. });
  93. }
  94. case WORKFLOW_SECTION_RECEIVE:
  95. const workflowSection = action.payload.workflowSection;
  96. const workflowSections = state.items.map((item, i) => {
  97. if (item.get('uuid') == workflowSection.get('uuid')) {
  98. return workflowSection;
  99. }
  100. return item;
  101. });
  102.  
  103. action.payload.workflowSections = Immutable.fromJS(workflowSections);
  104. case WORKFLOW_SECTIONS_RECEIVE:
  105. return Object.assign({}, state, {
  106. isFetching: false,
  107. didInvalidate: false,
  108. items: action.payload.workflowSections
  109. });
  110. }
  111.  
  112. return state;
  113. };