Skip to content

Commit

Permalink
Merge pull request #192 from chrishiguto/feature/add-default-route-an…
Browse files Browse the repository at this point in the history
…d-unprotected-routes

feat: add default route and unprotected routes
  • Loading branch information
andreneto97 authored Aug 28, 2024
2 parents 5fad2a9 + 8d3fb77 commit a1f900a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
14 changes: 14 additions & 0 deletions packages/react-navigation/src/components/DefaultRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ModuleProps } from '@concepta/react-material-ui/dist/modules/crud';
type DefaultRouteProps = {
resource: string;
name: string;
isUnprotected?: boolean;
module?: ModuleProps;
page?: ReactNode;
items: DrawerItemProps[];
Expand All @@ -27,6 +28,7 @@ type DefaultRouteProps = {
const DefaultRoute = ({
resource,
name,
isUnprotected = false,
module,
page,
items,
Expand Down Expand Up @@ -64,13 +66,25 @@ const DefaultRoute = ({
}

if (renderAppBar) {
if (!isUnprotected) {
return <>{renderAppBar(menuItems, renderedChildren)}</>;
}

return (
<ProtectedRoute>
{renderAppBar(menuItems, renderedChildren)}
</ProtectedRoute>
);
}

if (!isUnprotected) {
return (
<AppBarContainer menuItems={menuItems}>
{renderedChildren}
</AppBarContainer>
);
}

return (
<ProtectedRoute>
<AppBarContainer
Expand Down
1 change: 1 addition & 0 deletions packages/react-navigation/src/components/Resource.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type ResourceProps = {
id: string;
name: string;
icon: ReactNode;
isUnprotected?: boolean;
module?: Partial<ModuleProps>;
page?: ReactNode;
};
Expand Down
7 changes: 6 additions & 1 deletion packages/react-navigation/src/components/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const router = (
AdminProvider: ComponentType<PropsWithChildren<{ home: string }>>,
routes: ReactElement[],
items: DrawerItemProps[],
defaultRoute?: string,
authModuleProps?: AuthModule,
drawerProps?: DrawerProps,
navbarProps?: NavbarProps,
Expand All @@ -55,10 +56,11 @@ const router = (
<Route
path="/*"
element={
<AdminProvider home={firstRoute?.props.id}>
<AdminProvider home={defaultRoute ?? firstRoute?.props.id}>
<RoutesRoot
routes={routes}
items={items}
defaultRoute={defaultRoute}
authModuleProps={authModuleProps}
drawerProps={drawerProps}
navbarProps={navbarProps}
Expand All @@ -78,6 +80,7 @@ const router = (
type RouterProps = {
children: ReactElement[];
AdminProvider: ComponentType<PropsWithChildren<{ home: string }>>;
defaultRoute?: string;
useMemoryRouter?: boolean;
authModuleProps?: AuthModule;
drawerProps?: DrawerProps;
Expand All @@ -95,6 +98,7 @@ type RouterProps = {
const Router = ({
children,
AdminProvider,
defaultRoute,
useMemoryRouter = false,
authModuleProps,
drawerProps,
Expand All @@ -119,6 +123,7 @@ const Router = ({
AdminProvider,
children,
items,
defaultRoute,
authModuleProps,
drawerProps,
navbarProps,
Expand Down
5 changes: 4 additions & 1 deletion packages/react-navigation/src/components/RoutesRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { AuthModule } from './Router';
type RoutesRootProps = {
items: DrawerItemProps[];
routes: ReactElement[];
defaultRoute?: string;
authModuleProps?: AuthModule;
drawerProps?: DrawerProps;
navbarProps?: NavbarProps;
Expand All @@ -31,6 +32,7 @@ type RoutesRootProps = {
const RoutesRoot = ({
routes,
items,
defaultRoute,
authModuleProps,
drawerProps,
navbarProps,
Expand All @@ -40,7 +42,7 @@ const RoutesRoot = ({
renderForgotPassword,
renderResetPassword,
}: RoutesRootProps) => {
const home = routes[0].props.id;
const home = defaultRoute ?? routes[0].props.id;

return (
<Routes>
Expand Down Expand Up @@ -99,6 +101,7 @@ const RoutesRoot = ({
element={
<DefaultRoute
renderAppBar={renderAppBar}
isUnprotected={child.props.isUnprotected}
resource={child.props.id}
name={child.props.name}
module={child.props.module}
Expand Down

0 comments on commit a1f900a

Please sign in to comment.