Skip to content

Commit

Permalink
Merge pull request #6145 from marmelab/Fix-redirect-to-query-string
Browse files Browse the repository at this point in the history
Fix useRedirect does not handle query strings
  • Loading branch information
djhi authored Apr 10, 2021
2 parents bca55ce + c9dd50e commit e0fb207
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
32 changes: 32 additions & 0 deletions packages/ra-core/src/sideEffect/useRedirect.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as React from 'react';
import { useEffect } from 'react';
import expect from 'expect';
import { renderWithRedux } from 'ra-test';
import { createMemoryHistory } from 'history';
import { Router } from 'react-router-dom';

import useRedirect from './useRedirect';

const Redirect = ({ redirectTo, basePath = '', id = null, data = null }) => {
const redirect = useRedirect();
useEffect(() => {
redirect(redirectTo, basePath, id, data);
}, [basePath, data, id, redirect, redirectTo]);
return null;
};

describe('useRedirect', () => {
it('should redirect to the path with query string', () => {
const history = createMemoryHistory();
renderWithRedux(
<Router history={history}>
<Redirect redirectTo="/foo?bar=baz" />
</Router>
);
expect(history.location).toMatchObject({
pathname: '/foo',
search: '?bar=baz',
state: { _scrollToTop: true },
});
});
});
5 changes: 3 additions & 2 deletions packages/ra-core/src/sideEffect/useRedirect.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useCallback } from 'react';
import { useDispatch } from 'react-redux';
import { useHistory } from 'react-router-dom';
import { parsePath } from 'history';

import { Identifier, Record } from '../types';
import resolveRedirectTo from '../util/resolveRedirectTo';
import { refreshView } from '../actions/uiActions';
import { useHistory } from 'react-router-dom';

type RedirectToFunction = (
basePath?: string,
Expand Down Expand Up @@ -53,7 +54,7 @@ const useRedirect = () => {
}

history.push({
pathname: resolveRedirectTo(redirectTo, basePath, id, data),
...parsePath(resolveRedirectTo(redirectTo, basePath, id, data)),
state: { _scrollToTop: true },
});
},
Expand Down

0 comments on commit e0fb207

Please sign in to comment.