explorer.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import _ from 'lodash';
  2. const defaultState = {
  3. isVisible: false,
  4. isFetching: false,
  5. isResolved: false,
  6. path: [],
  7. currentPage: 1,
  8. defaultPage: 1,
  9. // TODO Change to include less fields (just 'descendants'?) in the next version of the admin API.
  10. // Specificies which fields are to be fetched in the API calls.
  11. fields: ['title', 'latest_revision_created_at', 'status', 'descendants', 'children'],
  12. // Coming from the API in order to get translated / pluralised labels.
  13. pageTypes: {},
  14. };
  15. export default function explorer(state = defaultState, action = {}) {
  16. let newNodes = state.path;
  17. switch (action.type) {
  18. case 'SET_DEFAULT_PAGE':
  19. return _.assign({}, state, {
  20. defaultPage: action.payload
  21. });
  22. case 'RESET_TREE':
  23. return _.assign({}, state, {
  24. isFetching: true,
  25. isResolved: false,
  26. currentPage: action.payload,
  27. path: [],
  28. });
  29. case 'TREE_RESOLVED':
  30. return _.assign({}, state, {
  31. isFetching: false,
  32. isResolved: true
  33. });
  34. case 'TOGGLE_EXPLORER':
  35. return _.assign({}, state, {
  36. isVisible: !state.isVisible,
  37. currentPage: action.payload ? action.payload : state.defaultPage,
  38. });
  39. case 'FETCH_START':
  40. return _.assign({}, state, {
  41. isFetching: true
  42. });
  43. case 'FETCH_BRANCH_SUCCESS':
  44. if (state.path.indexOf(action.payload.id) < 0) {
  45. newNodes = [action.payload.id].concat(state.path);
  46. }
  47. return _.assign({}, state, {
  48. path: newNodes,
  49. currentPage: state.currentPage ? state.currentPage : action.payload.id
  50. });
  51. // called on fetch page...
  52. case 'FETCH_SUCCESS':
  53. if (state.path.indexOf(action.payload.id) < 0) {
  54. newNodes = state.path.concat([action.payload.id]);
  55. }
  56. return _.assign({}, state, {
  57. isFetching: false,
  58. path: newNodes,
  59. });
  60. case 'PUSH_PAGE':
  61. return _.assign({}, state, {
  62. path: state.path.concat([action.payload])
  63. });
  64. case 'POP_PAGE':
  65. return _.assign({}, state, {
  66. path: state.path.length > 1 ? state.path.slice(0, -1) : state.path,
  67. });
  68. case 'FETCH_CHILDREN_SUCCESS':
  69. return _.assign({}, state, {
  70. isFetching: false,
  71. // eslint-disable-next-line no-underscore-dangle
  72. pageTypes: _.assign({}, state.pageTypes, action.payload.json.__types),
  73. });
  74. default:
  75. return state;
  76. }
  77. }