Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow useRedirect to set location state #6293

Merged
merged 1 commit into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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