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

[Doc] Explain how to change page with useNavigation #9840

Merged
merged 11 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions docs/useRedirect.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ redirect((resource, id, data) => {
}, 'posts', 1, { hasComments: true });
// redirect to edit view with state data
redirect('edit', 'posts', 1, {}, { record: { post_id: record.id } });
// redirect to a history Location object
redirect({ pathname: '/some/path', search: '?query=string', hash: '#hash', state: null, key: 'my_key' });
// do not redirect (resets the record form)
redirect(false);
```
Expand Down
2 changes: 2 additions & 0 deletions packages/ra-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@hookform/resolvers": "^3.2.0",
"@testing-library/react": "^14.1.2",
"@testing-library/react-hooks": "^8.0.1",
"@types/history": "^5.0.0",
"@types/jest": "^29.5.2",
"@types/jscodeshift": "^0.11.11",
"@types/node": "^20.10.7",
Expand All @@ -52,6 +53,7 @@
"zod": "^3.22.1"
},
"peerDependencies": {
"@types/history": "^5.0.0",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"react-hook-form": "^7.43.9",
Expand Down
107 changes: 107 additions & 0 deletions packages/ra-core/src/routing/useRedirect.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import * as React from 'react';
import { Link, Routes, Route, useLocation } from 'react-router-dom';

import { FakeBrowserDecorator } from '../storybook//FakeBrowser';
import { useRedirect as useRedirectRA } from './useRedirect';

export default {
title: 'ra-core/routing',
decorators: [FakeBrowserDecorator],
};

const Home = () => {
const redirect = useRedirectRA();
return (
<>
<h1>Home</h1>
<ul>
<li>
<button onClick={() => redirect('/dashboard')}>
Dashboard
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use types as labels for your buttons: Relative url, View name, View name with details, Location, Function

Copy link
Contributor Author

@erwanMarmelab erwanMarmelab May 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice idea, applied 👍

</button>
</li>
<li>
<button onClick={() => redirect('list', 'posts')}>
Post list
</button>
</li>
<li>
<button onClick={() => redirect('edit', 'posts', 123)}>
123th post detail
</button>
</li>
<li>
<button
onClick={() =>
redirect({
pathname: '/some/path',
search: '?query=string',
hash: '#hash',
state: null,
key: 'my_key',
})
}
>
My page
</button>
</li>
<li>
<button
onClick={() =>
redirect((resource, id, data) => {
return data?.hasComments ? 'comments' : 'posts';
})
}
>
Redirect function
</button>
</li>
</ul>
</>
);
};

const Dashboard = () => (
<div>
<h1>Admin dashboard</h1>
<Link to="/">Home</Link>
</div>
);

const PostList = () => (
<div>
<h1>Posts</h1>
<Link to="/">Home</Link>
</div>
);

const PostDetail = () => (
<div>
<h1>Post 123</h1>
<Link to="/">Home</Link>
</div>
);

const SomePage = () => {
const location = useLocation();
return (
<div>
<h1>My Page</h1>
<Link to="/">Home</Link>
<hr />
<p>Location: {location.pathname}</p>
<p>Location: {location.search}</p>
<p>Hash: {location.hash}</p>
</div>
);
};

export const useRedirect = () => (
<Routes>
<Route path="/" element={<Home />} />
<Route path="/dashboard" element={<Dashboard />} />
<Route path="/posts" element={<PostList />} />
<Route path="/posts/123" element={<PostDetail />} />
<Route path="/some/path" element={<SomePage />} />
</Routes>
);
35 changes: 26 additions & 9 deletions packages/ra-core/src/routing/useRedirect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useCallback } from 'react';
import { useNavigate, To } from 'react-router-dom';
import { Location } from 'history';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you import the To type imported from 'react-router-dom' above? It would avoid the added dependency

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because there is any state key in a To object. And in our first discussion on it your speak about this state

import { Identifier, RaRecord } from '../types';

import { useBasename } from './useBasename';
Expand All @@ -12,7 +13,11 @@ type RedirectToFunction = (
state?: object
) => To;

export type RedirectionSideEffect = CreatePathType | false | RedirectToFunction;
export type RedirectionSideEffect =
| CreatePathType
| false
| RedirectToFunction
| Location;

/**
* Hook for Redirection Side Effect
Expand All @@ -30,6 +35,8 @@ export type RedirectionSideEffect = CreatePathType | false | RedirectToFunction;
* redirect(false);
* // redirect to the result of a function
* redirect((resource, id, data) => ...)
* // redirect to a Location object
* redirect({ pathname: '/some/path', search: '?query=string', hash: '#hash', state: null, key: 'my_key' });
*/
export const useRedirect = () => {
const navigate = useNavigate();
Expand Down Expand Up @@ -59,6 +66,9 @@ export const useRedirect = () => {
state: { _scrollToTop: true, ...state },
});
return;
} else if (typeof redirectTo === 'object') {
navigate(redirectTo);
return;
} else if (
typeof redirectTo === 'string' &&
redirectTo.startsWith('http') &&
Expand All @@ -70,14 +80,21 @@ export const useRedirect = () => {
return;
} else {
// redirection to an internal link
navigate(createPath({ resource, id, type: redirectTo }), {
state:
// We force the scrollToTop except when navigating to a list
// where this is already done by <RestoreScrollPosition> in <Resource>
redirectTo === 'list'
? state
: { _scrollToTop: true, ...state },
});
navigate(
createPath({
resource,
id,
type: redirectTo as CreatePathType,
}),
{
state:
// We force the scrollToTop except when navigating to a list
// where this is already done by <RestoreScrollPosition> in <Resource>
redirectTo === 'list'
? state
: { _scrollToTop: true, ...state },
}
);
erwanMarmelab marked this conversation as resolved.
Show resolved Hide resolved
return;
}
},
Expand Down
20 changes: 20 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6547,6 +6547,15 @@ __metadata:
languageName: node
linkType: hard

"@types/history@npm:^5.0.0":
version: 5.0.0
resolution: "@types/history@npm:5.0.0"
dependencies:
history: "npm:*"
checksum: bce6d8da5217349dec97dfc4d9b65be03166351b86cb3e8764ed92b8b98eced1fc7ecb5bb17f1d7da894d065ffa3a12a8106444c174450a6d596241deef3f0a5
languageName: node
linkType: hard

"@types/hoist-non-react-statics@npm:^3.3.1":
version: 3.3.1
resolution: "@types/hoist-non-react-statics@npm:3.3.1"
Expand Down Expand Up @@ -13360,6 +13369,15 @@ __metadata:
languageName: node
linkType: hard

"history@npm:*":
version: 5.3.0
resolution: "history@npm:5.3.0"
dependencies:
"@babel/runtime": "npm:^7.7.6"
checksum: 812ec839386222d6437bd78d9f05db32e47d105ada0ad8834b32626919dd2fee7a10001bc489510f93a8069d02f118214bd8d42a82f7cf9daf8e84fbcbbb2016
languageName: node
linkType: hard

"hoist-non-react-statics@npm:^3.3.0, hoist-non-react-statics@npm:^3.3.1, hoist-non-react-statics@npm:^3.3.2":
version: 3.3.2
resolution: "hoist-non-react-statics@npm:3.3.2"
Expand Down Expand Up @@ -18686,6 +18704,7 @@ __metadata:
"@tanstack/react-query": "npm:^5.8.4"
"@testing-library/react": "npm:^14.1.2"
"@testing-library/react-hooks": "npm:^8.0.1"
"@types/history": "npm:^5.0.0"
"@types/jest": "npm:^29.5.2"
"@types/jscodeshift": "npm:^0.11.11"
"@types/node": "npm:^20.10.7"
Expand Down Expand Up @@ -18717,6 +18736,7 @@ __metadata:
yup: "npm:^0.32.11"
zod: "npm:^3.22.1"
peerDependencies:
"@types/history": ^5.0.0
react: ^18.0.0
react-dom: ^18.0.0
react-hook-form: ^7.43.9
Expand Down
Loading