location.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. replace: false,
  11. lastUpdated: 0,
  12. };
  13. export const locationReducer = (state = initialState, action: Action): LocationState => {
  14. switch (action.type) {
  15. case CoreActionTypes.UpdateLocation: {
  16. const { path, routeParams, replace } = action.payload;
  17. let query = action.payload.query || state.query;
  18. if (action.payload.partial) {
  19. query = _.defaults(query, state.query);
  20. query = _.omitBy(query, _.isNull);
  21. }
  22. return {
  23. url: renderUrl(path || state.path, query),
  24. path: path || state.path,
  25. query: { ...query },
  26. routeParams: routeParams || state.routeParams,
  27. replace: replace === true,
  28. lastUpdated: new Date().getTime(),
  29. };
  30. }
  31. }
  32. return state;
  33. };