2019-01-28 04:45:15 -06:00
|
|
|
import { Action, CoreActionTypes } from 'app/core/actions/location';
|
2018-09-28 09:44:07 -05:00
|
|
|
import { LocationState } from 'app/types';
|
|
|
|
import { renderUrl } from 'app/core/utils/url';
|
2018-10-10 02:26:17 -05:00
|
|
|
import _ from 'lodash';
|
2018-08-31 15:16:20 -05:00
|
|
|
|
|
|
|
export const initialState: LocationState = {
|
|
|
|
url: '',
|
|
|
|
path: '',
|
|
|
|
query: {},
|
|
|
|
routeParams: {},
|
2019-02-04 06:49:14 -06:00
|
|
|
replace: false,
|
2018-08-31 15:16:20 -05:00
|
|
|
};
|
|
|
|
|
2018-09-02 13:36:03 -05:00
|
|
|
export const locationReducer = (state = initialState, action: Action): LocationState => {
|
2018-08-31 15:16:20 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-08-31 15:16:20 -05:00
|
|
|
return {
|
|
|
|
url: renderUrl(path || state.path, query),
|
|
|
|
path: path || state.path,
|
2019-01-28 04:45:15 -06:00
|
|
|
query: { ...query },
|
2018-08-31 15:16:20 -05:00
|
|
|
routeParams: routeParams || state.routeParams,
|
2019-02-04 06:49:14 -06:00
|
|
|
replace: replace === true,
|
2018-08-31 15:16:20 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
};
|