Skip to content

Commit

Permalink
Merge pull request #6293 from despatates/master
Browse files Browse the repository at this point in the history
Allow useRedirect to set location state
  • Loading branch information
fzaninotto authored May 31, 2021
2 parents b559604 + 3cad08c commit 53e8627
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
24 changes: 21 additions & 3 deletions packages/ra-core/src/sideEffect/useRedirect.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ import { Router } from 'react-router-dom';

import useRedirect from './useRedirect';

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

Expand All @@ -29,4 +35,16 @@ describe('useRedirect', () => {
state: { _scrollToTop: true },
});
});
it('should redirect to the path with state', () => {
const history = createMemoryHistory();
renderWithRedux(
<Router history={history}>
<Redirect redirectTo="/foo" state={{ bar: 'baz' }} />
</Router>
);
expect(history.location).toMatchObject({
pathname: '/foo',
state: { _scrollToTop: true, bar: 'baz' },
});
});
});
14 changes: 9 additions & 5 deletions packages/ra-core/src/sideEffect/useRedirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { refreshView } from '../actions/uiActions';
type RedirectToFunction = (
basePath?: string,
id?: Identifier,
data?: Record
data?: Record,
state?: object
) => string;

export type RedirectionSideEffect = string | boolean | RedirectToFunction;
Expand All @@ -25,10 +26,12 @@ export type RedirectionSideEffect = string | boolean | RedirectToFunction;
* redirect('list', '/posts');
* // redirect to edit view
* redirect('edit', '/posts', 123);
* // redirect to edit view with state data
* redirect('edit', '/comment', 123, {}, { record: { post_id: record.id } });
* // do not redirect (resets the record form)
* redirect(false);
* // redirect to the result of a function
* redirect((redirectTo, basePath, is, data) => ...)
* redirect((redirectTo, basePath, id, data) => ...)
*/
const useRedirect = () => {
const dispatch = useDispatch();
Expand All @@ -38,13 +41,14 @@ const useRedirect = () => {
redirectTo: RedirectionSideEffect,
basePath: string = '',
id?: Identifier,
data?: Partial<Record>
data?: Partial<Record>,
state: object = {}
) => {
if (!redirectTo) {
if (history.location.state || history.location.search) {
history.replace({
...history.location,
state: {},
state,
search: undefined,
});
} else {
Expand All @@ -55,7 +59,7 @@ const useRedirect = () => {

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

0 comments on commit 53e8627

Please sign in to comment.