location.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { LocationState } from 'app/types';
  2. import { renderUrl } from 'app/core/utils/url';
  3. import _ from 'lodash';
  4. import { reducerFactory } from 'app/core/redux';
  5. import { updateLocation } from 'app/core/actions';
  6. export const initialState: LocationState = {
  7. url: '',
  8. path: '',
  9. query: {},
  10. routeParams: {},
  11. replace: false,
  12. lastUpdated: 0,
  13. };
  14. export const locationReducer = reducerFactory<LocationState>(initialState)
  15. .addMapper({
  16. filter: updateLocation,
  17. mapper: (state, action): LocationState => {
  18. const { path, routeParams, replace } = action.payload;
  19. let query = action.payload.query || state.query;
  20. if (action.payload.partial) {
  21. query = _.defaults(query, state.query);
  22. query = _.omitBy(query, _.isNull);
  23. }
  24. return {
  25. url: renderUrl(path || state.path, query),
  26. path: path || state.path,
  27. query: { ...query },
  28. routeParams: routeParams || state.routeParams,
  29. replace: replace === true,
  30. lastUpdated: new Date().getTime(),
  31. };
  32. },
  33. })
  34. .create();