location.ts 903 B

123456789101112131415161718192021222324252627282930313233343536
  1. import { Action } 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 'UPDATE_LOCATION': {
  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: {
  24. ...query,
  25. },
  26. routeParams: routeParams || state.routeParams,
  27. };
  28. }
  29. }
  30. return state;
  31. };