grafana/public/app/features/dashboard/state/reducers.ts

90 lines
2.1 KiB
TypeScript
Raw Normal View History

import { DashboardState, DashboardInitPhase } from 'app/types';
import {
loadDashboardPermissions,
dashboardInitFetching,
dashboardInitSlow,
dashboardInitServices,
dashboardInitFailed,
dashboardInitCompleted,
cleanUpDashboard,
} from './actions';
2019-02-02 15:43:19 -06:00
import { reducerFactory } from 'app/core/redux';
2018-09-13 09:00:02 -05:00
import { processAclItems } from 'app/core/utils/acl';
import { DashboardModel } from './DashboardModel';
2018-09-13 09:00:02 -05:00
2018-10-25 09:56:49 -05:00
export const initialState: DashboardState = {
initPhase: DashboardInitPhase.NotStarted,
isInitSlow: false,
model: null,
2018-09-13 09:00:02 -05:00
permissions: [],
};
2019-02-02 15:43:19 -06:00
export const dashboardReducer = reducerFactory(initialState)
.addMapper({
filter: loadDashboardPermissions,
mapper: (state, action) => ({
...state,
permissions: processAclItems(action.payload),
}),
})
.addMapper({
filter: dashboardInitFetching,
mapper: state => ({
...state,
initPhase: DashboardInitPhase.Fetching,
}),
})
.addMapper({
filter: dashboardInitServices,
mapper: state => ({
...state,
initPhase: DashboardInitPhase.Services,
}),
})
.addMapper({
filter: dashboardInitSlow,
mapper: state => ({
2019-02-02 15:43:19 -06:00
...state,
isInitSlow: true,
2019-02-02 15:43:19 -06:00
}),
})
.addMapper({
filter: dashboardInitFailed,
mapper: (state, action) => ({
...state,
initPhase: DashboardInitPhase.Failed,
isInitSlow: false,
initError: action.payload,
model: new DashboardModel({ title: 'Dashboard init failed' }, { canSave: false, canEdit: false }),
}),
})
.addMapper({
filter: dashboardInitCompleted,
mapper: (state, action) => ({
...state,
initPhase: DashboardInitPhase.Completed,
model: action.payload,
isInitSlow: false,
}),
})
.addMapper({
filter: cleanUpDashboard,
mapper: (state, action) => {
// tear down current dashboard
state.model.destroy();
return {
...state,
initPhase: DashboardInitPhase.NotStarted,
model: null,
isInitSlow: false,
initError: null,
};
},
})
2019-02-03 03:55:58 -06:00
.create();
2018-09-13 09:00:02 -05:00
export default {
dashboard: dashboardReducer,
};