grafana/public/app/core/reducers/location.ts

37 lines
976 B
TypeScript
Raw Normal View History

2019-01-28 04:45:15 -06:00
import { Action, CoreActionTypes } from 'app/core/actions/location';
import { LocationState } from 'app/types';
import { renderUrl } from 'app/core/utils/url';
2018-10-10 02:26:17 -05:00
import _ from 'lodash';
export const initialState: LocationState = {
url: '',
path: '',
query: {},
routeParams: {},
2019-02-04 06:49:14 -06:00
replace: false,
};
2018-09-02 13:36:03 -05:00
export const locationReducer = (state = initialState, action: Action): LocationState => {
switch (action.type) {
2019-01-28 04:45:15 -06:00
case CoreActionTypes.UpdateLocation: {
2019-02-04 06:49:14 -06:00
const { path, routeParams, replace } = action.payload;
2018-10-10 02:26:17 -05:00
let query = action.payload.query || state.query;
if (action.payload.partial) {
query = _.defaults(query, state.query);
2018-10-27 08:09:36 -05:00
query = _.omitBy(query, _.isNull);
2018-10-10 02:26:17 -05:00
}
return {
url: renderUrl(path || state.path, query),
path: path || state.path,
2019-01-28 04:45:15 -06:00
query: { ...query },
routeParams: routeParams || state.routeParams,
2019-02-04 06:49:14 -06:00
replace: replace === true,
};
}
}
return state;
};