You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 line
3.6KB

  1. import { PageID } from "codes/page";
  2. import { UserRole } from "codes/user";
  3. import LoadingScreen from "components/LoadingScreen";
  4. import { AppContextProvider } from "contexts/AppContext";
  5. import useAuth from "hooks/useAuth";
  6. import DashboardLayout from "layouts/dashbord";
  7. import SimpleLayout from "layouts/simple";
  8. import { ElementType, Suspense, lazy, useMemo } from "react";
  9. import { RouteObject, useRoutes } from "react-router-dom";
  10. import { getRoute } from "./path";
  11. const Loadable = (Component: ElementType) => (props: any) => {
  12. return (
  13. <Suspense fallback={<LoadingScreen />}>
  14. <Component {...props} />
  15. </Suspense>
  16. );
  17. };
  18. const AuthRoutes = (): RouteObject => ({
  19. element: <SimpleLayout />,
  20. children: [
  21. {
  22. path: getRoute(PageID.LOGIN),
  23. element: <Login />,
  24. },
  25. {
  26. path: getRoute(PageID.LOGOUT),
  27. element: <Logout />,
  28. },
  29. ],
  30. });
  31. const AppRoutes = (): RouteObject => ({
  32. element: (
  33. <AppContextProvider>
  34. <SimpleLayout />
  35. </AppContextProvider>
  36. ),
  37. children: [
  38. {
  39. path: getRoute(PageID.APP_RECEIPT_ISSUING_ORDER_INDEX),
  40. element: <ReceiptIssuingOrder />,
  41. },
  42. ],
  43. });
  44. const DashboardRoutes = (): RouteObject => {
  45. const { canAccess } = useAuth();
  46. const allChildren = [
  47. {
  48. pageId: PageID.DASHBOARD_OVERVIEW,
  49. element: <Dashboard />,
  50. target: UserRole.NORMAL_ADMIN,
  51. },
  52. {
  53. pageId: PageID.DASHBOARD_CONTRACT_LIST,
  54. element: <ContractList />,
  55. target: UserRole.SUPER_ADMIN,
  56. },
  57. {
  58. pageId: PageID.DASHBOARD_CONTRACT_DETAIL,
  59. element: <ContractDetail />,
  60. target: UserRole.SUPER_ADMIN,
  61. },
  62. {
  63. pageId: PageID.DASHBOARD_RECEIPT_ISSUING_ORDER_CREATE,
  64. element: <ReceiptIssuingOrderCreate />,
  65. target: UserRole.NORMAL_ADMIN,
  66. },
  67. {
  68. pageId: PageID.DASHBOARD_RECEIPT_ISSUING_ORDER_LIST,
  69. element: <ReceiptIssuingOrderList />,
  70. target: UserRole.NORMAL_ADMIN,
  71. },
  72. ];
  73. const children: RouteObject[] = useMemo(() => {
  74. return allChildren
  75. .filter(({ pageId }) => canAccess(pageId))
  76. .map(({ pageId, ...others }) => ({
  77. ...others,
  78. path: getRoute(pageId),
  79. }));
  80. }, [canAccess]);
  81. return {
  82. element: <DashboardLayout />,
  83. children,
  84. };
  85. };
  86. export function Routes() {
  87. const { initialized } = useAuth();
  88. return useRoutes([
  89. AuthRoutes(),
  90. AppRoutes(),
  91. DashboardRoutes(),
  92. {
  93. path: "403",
  94. element: <Page403 />,
  95. },
  96. {
  97. path: "*",
  98. element: initialized ? <Page404 /> : <LoadingScreen />,
  99. },
  100. ]);
  101. }
  102. // 認証関連 -------------------------------
  103. const Login = Loadable(lazy(() => import("pages/auth/login")));
  104. const Logout = Loadable(lazy(() => import("pages/auth/logout")));
  105. // App ---------------------------
  106. const ReceiptIssuingOrder = Loadable(
  107. lazy(() => import("pages/app/ReceiptIssuingOrder"))
  108. );
  109. //ダッシュボード ----------------------------
  110. const Dashboard = Loadable(lazy(() => import("pages/dashboard")));
  111. // 契約関連
  112. const ContractList = Loadable(
  113. lazy(() => import("pages/dashboard/contract/list"))
  114. );
  115. const ContractDetail = Loadable(
  116. lazy(() => import("pages/dashboard/contract/detail"))
  117. );
  118. // 領収証発行依頼
  119. const ReceiptIssuingOrderCreate = Loadable(
  120. lazy(() => import("pages/dashboard/receipt-issuing-order/create"))
  121. );
  122. const ReceiptIssuingOrderList = Loadable(
  123. lazy(() => import("pages/dashboard/receipt-issuing-order/list"))
  124. );
  125. // その他 ---------------------------------
  126. const Page403 = Loadable(lazy(() => import("pages/common/Page403")));
  127. const Page404 = Loadable(lazy(() => import("pages/common/Page404")));