reducers.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { DashboardState, DashboardInitPhase } from 'app/types';
  2. import {
  3. loadDashboardPermissions,
  4. dashboardInitFetching,
  5. dashboardInitSlow,
  6. dashboardInitServices,
  7. dashboardInitFailed,
  8. dashboardInitCompleted,
  9. cleanUpDashboard,
  10. } from './actions';
  11. import { reducerFactory } from 'app/core/redux';
  12. import { processAclItems } from 'app/core/utils/acl';
  13. import { DashboardModel } from './DashboardModel';
  14. export const initialState: DashboardState = {
  15. initPhase: DashboardInitPhase.NotStarted,
  16. isInitSlow: false,
  17. model: null,
  18. permissions: [],
  19. };
  20. export const dashboardReducer = reducerFactory(initialState)
  21. .addMapper({
  22. filter: loadDashboardPermissions,
  23. mapper: (state, action) => ({
  24. ...state,
  25. permissions: processAclItems(action.payload),
  26. }),
  27. })
  28. .addMapper({
  29. filter: dashboardInitFetching,
  30. mapper: state => ({
  31. ...state,
  32. initPhase: DashboardInitPhase.Fetching,
  33. }),
  34. })
  35. .addMapper({
  36. filter: dashboardInitServices,
  37. mapper: state => ({
  38. ...state,
  39. initPhase: DashboardInitPhase.Services,
  40. }),
  41. })
  42. .addMapper({
  43. filter: dashboardInitSlow,
  44. mapper: state => ({
  45. ...state,
  46. isInitSlow: true,
  47. }),
  48. })
  49. .addMapper({
  50. filter: dashboardInitFailed,
  51. mapper: (state, action) => ({
  52. ...state,
  53. initPhase: DashboardInitPhase.Failed,
  54. isInitSlow: false,
  55. initError: action.payload,
  56. model: new DashboardModel({ title: 'Dashboard init failed' }, { canSave: false, canEdit: false }),
  57. }),
  58. })
  59. .addMapper({
  60. filter: dashboardInitCompleted,
  61. mapper: (state, action) => ({
  62. ...state,
  63. initPhase: DashboardInitPhase.Completed,
  64. model: action.payload,
  65. isInitSlow: false,
  66. }),
  67. })
  68. .addMapper({
  69. filter: cleanUpDashboard,
  70. mapper: (state, action) => {
  71. // Destroy current DashboardModel
  72. // Very important as this removes all dashboard event listeners
  73. state.model.destroy();
  74. return {
  75. ...state,
  76. initPhase: DashboardInitPhase.NotStarted,
  77. model: null,
  78. isInitSlow: false,
  79. initError: null,
  80. };
  81. },
  82. })
  83. .create();
  84. export default {
  85. dashboard: dashboardReducer,
  86. };