/* eslint-disable react/display-name */
import React from "react";
import { RouteProps, Switch, Route, Redirect } from "react-router-dom";
import {
LoginPage as DefaultLoginPage,
ErrorComponent,
LayoutWrapper,
useAuthenticated,
useIsAuthenticated,
IResourceItem,
useResource,
useRefineContext,
useRouterContext,
CanAccess,
} from "@pankod/refine-core";
type IRoutesProps = RouteProps & { routes?: RouteProps[] };
type IRouteComponentProps = { match: { params: { id: string } } };
const RouteProviderBase: React.FC = () => {
const { resources } = useResource();
const { catchAll, DashboardPage, LoginPage } = useRefineContext();
const { routes: customRoutes }: { routes: RouteProps[] } =
useRouterContext();
const isAuthenticated = useIsAuthenticated();
const { isLoading } = useAuthenticated({ type: "routeProvider" });
if (isLoading) {
return (
);
}
const routes: IRoutesProps[] = [];
const RouteHandler = (val: IResourceItem): void => {
const { list, create, edit, show, canDelete, route, name, options } =
val;
const ListComponent = list;
const CreateComponent = create;
const EditComponent = edit;
const ShowComponent = show;
const canCreate = !!create;
const canEdit = !!edit;
const canShow = !!show;
if (CreateComponent) {
routes.push({
exact: true,
path: `/:resource(${route})/:action(create)`,
component: () => (
}
>
),
});
routes.push({
exact: true,
path: `/:resource(${route})/:action(clone)/:id`,
component: (props: IRouteComponentProps) => (
}
>
),
});
}
if (EditComponent) {
routes.push({
exact: true,
path: `/:resource(${route})/:action(edit)/:id`,
component: (props: IRouteComponentProps) => (
}
>
),
});
}
if (ShowComponent) {
routes.push({
exact: true,
path: `/:resource(${route})/:action(show)/:id`,
component: (props: IRouteComponentProps) => (
}
>
),
});
}
if (ListComponent) {
routes.push({
exact: true,
path: `/:resource(${route})`,
component: () => (
}
>
),
});
}
return;
};
resources.map((item) => {
RouteHandler(item);
});
const RouteWithSubRoutes = (route: any) => ;
const renderAuthorized = () => (
{[...(customRoutes || [])].map((route, i) => (
))}
DashboardPage ? (
}
>
) : (
)
}
/>
{[...routes].map((route, i) => (
))}
{catchAll ?? }
{catchAll ?? }
);
const renderUnauthorized = () => (
{[...(customRoutes || [])].map((route, i) => (
))}
LoginPage ? :
}
/>
{
if (isLoading) {
return null;
}
const { pathname, search } = location;
const toURL = `${pathname}${search}`;
return (
);
}}
/>
);
return isAuthenticated ? renderAuthorized() : renderUnauthorized();
};
/**
* @internal
*/
export const RouteProvider = React.memo(RouteProviderBase);