location.ts 881 B

123456789101112131415161718192021222324252627282930313233
  1. import { Action } from 'app/core/actions/location';
  2. import { LocationState, UrlQueryMap } from 'app/types';
  3. import { toUrlParams } from 'app/core/utils/url';
  4. export const initialState: LocationState = {
  5. url: '',
  6. path: '',
  7. query: {},
  8. routeParams: {},
  9. };
  10. function renderUrl(path: string, query: UrlQueryMap | undefined): string {
  11. if (query && Object.keys(query).length > 0) {
  12. path += '?' + toUrlParams(query);
  13. }
  14. return path;
  15. }
  16. export const locationReducer = (state = initialState, action: Action): LocationState => {
  17. switch (action.type) {
  18. case 'UPDATE_LOCATION': {
  19. const { path, query, routeParams } = action.payload;
  20. return {
  21. url: renderUrl(path || state.path, query),
  22. path: path || state.path,
  23. query: query || state.query,
  24. routeParams: routeParams || state.routeParams,
  25. };
  26. }
  27. }
  28. return state;
  29. };