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

Fix incorrect <Ready> page when dynamically load routes with no resources #8490

Merged
merged 8 commits into from
Dec 15, 2022
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
2 changes: 1 addition & 1 deletion docs/Admin.md
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ See [Using React-Admin In A Sub Path](./Routing.md#using-react-admin-in-a-sub-pa

## `ready`

When you run an `<Admin>` with no child `<Resource>`, react-admin displays a "ready" screen:
When you run an `<Admin>` with no child `<Resource>` nor `<CustomRoutes>`, react-admin displays a "ready" screen:

![Empty Admin](./img/tutorial_empty.png)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import * as React from 'react';
import { render, screen, waitFor } from '@testing-library/react';
import expect from 'expect';
import { Route } from 'react-router-dom';
import { createMemoryHistory } from 'history';

import { useResourceDefinitions } from './useResourceDefinitions';
import { CoreAdminContext } from './CoreAdminContext';
import { CoreAdminRoutes } from './CoreAdminRoutes';
import { Resource } from './Resource';
import { CustomRoutes } from './CustomRoutes';
import { CoreLayoutProps } from '../types';
import { AuthProvider, ResourceProps } from '..';

Expand All @@ -31,6 +34,7 @@ const MyLayout = ({ children }: CoreLayoutProps) => (
);
const CatchAll = () => <div />;
const Loading = () => <>Loading</>;
const Ready = () => <>Ready</>;

const TestedComponent = ({ role }) => {
const history = createMemoryHistory();
Expand Down Expand Up @@ -185,6 +189,36 @@ const TestedComponentWithPermissions = () => {
);
};

const TestedComponentWithOnlyLazyCustomRoutes = ({ history }) => {
const [lazyRoutes, setLazyRoutes] = React.useState(null);

React.useEffect(() => {
const timer = setTimeout(
() =>
setLazyRoutes(
<CustomRoutes>
<Route path="/foo" element={<div>Foo</div>} />
</CustomRoutes>
),
500
);
return () => clearTimeout(timer);
}, [setLazyRoutes]);

return (
<CoreAdminContext history={history}>
<CoreAdminRoutes
layout={MyLayout}
catchAll={CatchAll}
loading={Loading}
ready={Ready}
>
{lazyRoutes}
</CoreAdminRoutes>
</CoreAdminContext>
);
};

const expectResource = (resource: string) =>
expect(screen.queryByText(`"name":"${resource}"`, { exact: false }));

Expand Down Expand Up @@ -280,4 +314,14 @@ describe('useConfigureAdminRouterFromChildren', () => {
expectResource('user').not.toBeNull();
expectResource('admin').toBeNull();
});
it('should allow dynamically loaded custom routes without any resources', async () => {
const history = createMemoryHistory();
render(<TestedComponentWithOnlyLazyCustomRoutes history={history} />);
expect(screen.queryByText('Ready')).not.toBeNull();

await new Promise(resolve => setTimeout(resolve, 1010));
expect(screen.queryByText('Ready')).toBeNull();
history.push('/foo');
expect(screen.queryByText('Foo')).not.toBeNull();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ const useRoutesAndResourcesFromChildren = (
setStatus(
!!functionChild
? 'loading'
: newRoutesAndResources.resources.length > 0
: newRoutesAndResources.resources.length > 0 ||
newRoutesAndResources.customRoutesWithLayout.length > 0 ||
newRoutesAndResources.customRoutesWithoutLayout.length > 0
? 'ready'
: 'empty'
);
Expand Down
4 changes: 2 additions & 2 deletions packages/ra-no-code/src/Admin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
ResourceConfigurationPage,
ResourceConfigurationProvider,
} from './ResourceConfiguration';
import { Layout, Ready } from './ui';
import { Layout } from './ui';
import { Route } from 'react-router';
import { useApplication } from './ApplicationContext';

Expand Down Expand Up @@ -43,7 +43,7 @@ const InnerAdmin = (props: AdminProps) => {
const hasResources = !!resources && Object.keys(resources).length > 0;

return (
<RaAdmin layout={Layout} ready={Ready} {...props}>
<RaAdmin layout={Layout} {...props}>
<CustomRoutes>
<Route
path="/configure/:resource"
Expand Down
15 changes: 12 additions & 3 deletions packages/ra-no-code/src/ui/Layout.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import React from 'react';
import { Layout as RaLayout, LayoutProps } from 'react-admin';
import { useResourcesConfiguration } from '../ResourceConfiguration';
import { Menu } from './Menu';
import { AppBar } from './Appbar';
import { Ready } from './Ready';

export const Layout = (props: LayoutProps) => (
<RaLayout {...props} appBar={AppBar} menu={Menu} />
);
export const Layout = (props: LayoutProps) => {
const [resources] = useResourcesConfiguration();
const hasResources = !!resources && Object.keys(resources).length > 0;

if (!hasResources) {
return <Ready />;
}

return <RaLayout {...props} appBar={AppBar} menu={Menu} />;
};