location.ts 914 B

12345678910111213141516171819202122232425262728293031323334
  1. import { Action, CoreActionTypes } from 'app/core/actions/location';
  2. import { LocationState } from 'app/types';
  3. import { renderUrl } from 'app/core/utils/url';
  4. import _ from 'lodash';
  5. export const initialState: LocationState = {
  6. url: '',
  7. path: '',
  8. query: {},
  9. routeParams: {},
  10. };
  11. export const locationReducer = (state = initialState, action: Action): LocationState => {
  12. switch (action.type) {
  13. case CoreActionTypes.UpdateLocation: {
  14. const { path, routeParams } = action.payload;
  15. let query = action.payload.query || state.query;
  16. if (action.payload.partial) {
  17. query = _.defaults(query, state.query);
  18. query = _.omitBy(query, _.isNull);
  19. }
  20. return {
  21. url: renderUrl(path || state.path, query),
  22. path: path || state.path,
  23. query: { ...query },
  24. routeParams: routeParams || state.routeParams,
  25. };
  26. }
  27. }
  28. return state;
  29. };