2020-01-13 01:03:22 -06:00
|
|
|
import { Action } from 'redux';
|
|
|
|
import { DashboardInitPhase, DashboardState } from 'app/types';
|
2019-02-06 12:42:04 -06:00
|
|
|
import {
|
2020-01-13 01:03:22 -06:00
|
|
|
cleanUpDashboard,
|
|
|
|
dashboardInitCompleted,
|
|
|
|
dashboardInitFailed,
|
2019-02-06 12:42:04 -06:00
|
|
|
dashboardInitFetching,
|
|
|
|
dashboardInitServices,
|
2020-01-13 01:03:22 -06:00
|
|
|
dashboardInitSlow,
|
|
|
|
loadDashboardPermissions,
|
2019-02-06 12:42:04 -06:00
|
|
|
} from './actions';
|
2018-09-13 09:00:02 -05:00
|
|
|
import { processAclItems } from 'app/core/utils/acl';
|
2019-09-23 07:17:00 -05:00
|
|
|
import { panelEditorReducer } from '../panel_editor/state/reducers';
|
2020-01-13 01:03:22 -06:00
|
|
|
import { DashboardModel } from './DashboardModel';
|
2018-09-13 09:00:02 -05:00
|
|
|
|
2018-10-25 09:56:49 -05:00
|
|
|
export const initialState: DashboardState = {
|
2019-02-06 12:42:04 -06:00
|
|
|
initPhase: DashboardInitPhase.NotStarted,
|
|
|
|
isInitSlow: false,
|
2019-02-02 16:01:48 -06:00
|
|
|
model: null,
|
2018-09-13 09:00:02 -05:00
|
|
|
permissions: [],
|
|
|
|
};
|
|
|
|
|
2020-01-13 01:03:22 -06:00
|
|
|
// Redux Toolkit uses ImmerJs as part of their solution to ensure that state objects are not mutated.
|
|
|
|
// ImmerJs has an autoFreeze option that freezes objects from change which means this reducer can't be migrated to createSlice
|
|
|
|
// because the state would become frozen and during run time we would get errors because Angular would try to mutate
|
|
|
|
// the frozen state.
|
|
|
|
// https://github.com/reduxjs/redux-toolkit/issues/242
|
|
|
|
export const dashboardReducer = (state: DashboardState = initialState, action: Action<unknown>): DashboardState => {
|
|
|
|
if (loadDashboardPermissions.match(action)) {
|
|
|
|
return {
|
2019-02-02 15:43:19 -06:00
|
|
|
...state,
|
|
|
|
permissions: processAclItems(action.payload),
|
2020-01-13 01:03:22 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dashboardInitFetching.match(action)) {
|
|
|
|
return {
|
2019-02-06 12:42:04 -06:00
|
|
|
...state,
|
|
|
|
initPhase: DashboardInitPhase.Fetching,
|
2020-01-13 01:03:22 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dashboardInitServices.match(action)) {
|
|
|
|
return {
|
2019-02-06 12:42:04 -06:00
|
|
|
...state,
|
|
|
|
initPhase: DashboardInitPhase.Services,
|
2020-01-13 01:03:22 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dashboardInitSlow.match(action)) {
|
|
|
|
return {
|
2019-02-02 15:43:19 -06:00
|
|
|
...state,
|
2019-02-06 12:42:04 -06:00
|
|
|
isInitSlow: true,
|
2020-01-13 01:03:22 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dashboardInitFailed.match(action)) {
|
|
|
|
return {
|
2019-02-02 16:01:48 -06:00
|
|
|
...state,
|
2019-02-06 12:42:04 -06:00
|
|
|
initPhase: DashboardInitPhase.Failed,
|
|
|
|
isInitSlow: false,
|
|
|
|
initError: action.payload,
|
|
|
|
model: new DashboardModel({ title: 'Dashboard init failed' }, { canSave: false, canEdit: false }),
|
2020-01-13 01:03:22 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dashboardInitCompleted.match(action)) {
|
|
|
|
return {
|
2019-02-05 06:49:35 -06:00
|
|
|
...state,
|
2019-02-06 12:42:04 -06:00
|
|
|
initPhase: DashboardInitPhase.Completed,
|
|
|
|
model: action.payload,
|
|
|
|
isInitSlow: false,
|
2020-01-13 01:03:22 -06:00
|
|
|
};
|
|
|
|
}
|
2019-02-06 12:42:04 -06:00
|
|
|
|
2020-01-13 01:03:22 -06:00
|
|
|
if (cleanUpDashboard.match(action)) {
|
|
|
|
// Destroy current DashboardModel
|
|
|
|
// Very important as this removes all dashboard event listeners
|
|
|
|
state.model.destroy();
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
initPhase: DashboardInitPhase.NotStarted,
|
|
|
|
model: null,
|
|
|
|
isInitSlow: false,
|
|
|
|
initError: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
};
|
2018-09-13 09:00:02 -05:00
|
|
|
|
|
|
|
export default {
|
|
|
|
dashboard: dashboardReducer,
|
2019-09-23 07:17:00 -05:00
|
|
|
panelEditor: panelEditorReducer,
|
2018-09-13 09:00:02 -05:00
|
|
|
};
|