Skip to content

Commit

Permalink
Include custom pages in AdminGuesser
Browse files Browse the repository at this point in the history
  • Loading branch information
fzaninotto committed Nov 5, 2024
1 parent d9ab7e7 commit 1c24516
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 20 deletions.
7 changes: 1 addition & 6 deletions packages/demo/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import { mergeTranslations } from 'react-admin';
import polyglotI18nProvider from 'ra-i18n-polyglot';
import englishMessages from 'ra-language-english';
import { BrowserRouter } from 'react-router-dom';
import {
AdminGuesser,
LoginPage,
raSupabaseEnglishMessages,
} from 'ra-supabase';
import { AdminGuesser, raSupabaseEnglishMessages } from 'ra-supabase';
import { authProvider } from './authProvider';
import { Dashboard } from './dashboard/Dashboard';
import { dataProvider } from './dataProvider';
Expand All @@ -23,7 +19,6 @@ const App = () => (
authProvider={authProvider}
i18nProvider={i18nProvider}
dashboard={Dashboard}
loginPage={LoginPage}
title="CRM Demo"
/>
</BrowserRouter>
Expand Down
65 changes: 51 additions & 14 deletions packages/ra-supabase-ui-materialui/src/guessers/AdminGuesser.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import * as React from 'react';

import { AdminContext, AdminUI, Resource, Loading } from 'react-admin';
import {
AdminContext,
AdminUI,
Resource,
Loading,
CustomRoutes,
} from 'react-admin';
import type { AdminProps, AdminUIProps } from 'react-admin';
import { Route } from 'react-router-dom';

import { useCrudGuesser } from './useCrudGuesser';
import { LoginPage } from '../LoginPage';
import { SetPasswordPage } from '../SetPasswordPage';
import { ForgotPasswordPage } from '../ForgotPasswordPage';

export const AdminGuesser = (props: AdminProps) => {
const {
Expand Down Expand Up @@ -40,15 +49,30 @@ export const AdminGuesser = (props: AdminProps) => {

const AdminUIGuesser = (props: AdminUIProps) => {
const resourceDefinitions = useCrudGuesser();
const hasLogged = React.useRef(false);
const { children, ...rest } = props;
if (!children) {
if (!children && resourceDefinitions.length > 0 && !hasLogged.current) {
console.log(
`Guessed Admin:
import { Admin, Resource } from 'react-admin';
import ( ListGuesser, EditGuesser, CreateGuesser, ShowGuesser ) from 'ra-supabase';
import { Admin, Resource, CustomRoutes } from 'react-admin';
import { Route } from 'react-router-dom';
import {
CreateGuesser,
EditGuesser,
ForgotPasswordPage,
ListGuesser,
LoginPage,
SetPasswordPage,
ShowGuesser,
} from 'ra-supabase';
export const App = () => (
<Admin dataProvider={dataProvider} authProvider={authProvider}>${resourceDefinitions
<Admin
dataProvider={dataProvider}
authProvider={authProvider}
loginPage={LoginPage}
>${resourceDefinitions
.map(
def => `
<Resource name="${def.name}"${def.list ? ' list={ListGuesser}' : ''}${
Expand All @@ -58,19 +82,32 @@ export const App = () => (
} />`
)
.join('')}
<CustomRoutes noLayout>
<Route path={SetPasswordPage.path} element={<SetPasswordPage />} />
<Route path={ForgotPasswordPage.path} element={<ForgotPasswordPage />} />
</CustomRoutes>
</Admin>
);`
);
hasLogged.current = true;
}

const resourceElements = resourceDefinitions.map(resourceDefinition => (
<Resource key={resourceDefinition.name} {...resourceDefinition} />
)) as any;
return (
<AdminUI ready={Loading} {...rest}>
{children ??
resourceDefinitions.map(resourceDefinition => (
<Resource
key={resourceDefinition.name}
{...resourceDefinition}
/>
))}
<AdminUI ready={Loading} loginPage={LoginPage} {...rest}>
<CustomRoutes noLayout>
<Route
path={SetPasswordPage.path}
element={<SetPasswordPage />}
/>
<Route
path={ForgotPasswordPage.path}
element={<ForgotPasswordPage />}
/>
</CustomRoutes>
{children ?? resourceElements}
</AdminUI>
);
};

0 comments on commit 1c24516

Please sign in to comment.