This repository has been archived by the owner on Sep 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from SELab-2/react-router
added routing
- Loading branch information
Showing
9 changed files
with
128 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,39 @@ | ||
import React from 'react' | ||
import ReactDOM from 'react-dom/client' | ||
import App from './App.tsx' | ||
import {createBrowserRouter, RouterProvider,} from "react-router-dom"; | ||
import './index.css' | ||
import Root from './pages/root.tsx' | ||
import ErrorPage from './pages/error.tsx' | ||
import LoginScreen from "./pages/login/LoginScreen.tsx"; | ||
import HomeAdmin from "./pages/admin/HomeAdmin.tsx"; | ||
import HomeStudent from "./pages/student/HomeStudent.tsx"; | ||
import HomeTeacher from "./pages/teacher/HomeTeacher.tsx"; | ||
|
||
const router = createBrowserRouter([ | ||
{ | ||
path: "/", | ||
element: <Root/>, | ||
errorElement: <ErrorPage/> | ||
}, | ||
{ | ||
path: "/login", | ||
element: <LoginScreen/> | ||
}, | ||
{ | ||
path: "/admin", | ||
element: <HomeAdmin/> | ||
}, | ||
{ | ||
path: "/student", | ||
element: <HomeStudent/> | ||
}, | ||
{ | ||
path: "/teacher", | ||
element: <HomeTeacher/> | ||
} | ||
]); | ||
ReactDOM.createRoot(document.getElementById('root')!).render( | ||
<React.StrictMode> | ||
<App /> | ||
<RouterProvider router={router}/> | ||
</React.StrictMode>, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import {isRouteErrorResponse, useRouteError} from "react-router-dom"; | ||
import {JSX} from 'react'; | ||
|
||
export default function ErrorPage(): JSX.Element { | ||
const error = useRouteError(); | ||
console.error(error); | ||
|
||
return ( | ||
<div id="error-page"> | ||
<h1>Oops!</h1> | ||
<p>Sorry, an unexpected error has occurred.</p> | ||
<p> | ||
<i>{errorMessage(error)}</i> | ||
</p> | ||
</div> | ||
); | ||
} | ||
|
||
interface RouterError extends Error {} | ||
|
||
function isRouterError(object: unknown): object is RouterError { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
return 'message' in object; | ||
} | ||
|
||
function errorMessage(error: unknown): string { | ||
if (isRouteErrorResponse(error)) { | ||
return `${error.status} ${error.statusText}` | ||
} else if (error != undefined && isRouterError(error)) { | ||
return error.message; | ||
} else if (typeof error === 'string') { | ||
return error | ||
} else { | ||
console.error(error) | ||
return 'Unknown error' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {JSX} from "react"; | ||
import {Link} from "react-router-dom"; | ||
|
||
export default function Root(): JSX.Element { | ||
// TODO: logic to send user to /login or | ||
return ( | ||
<> | ||
<>Redirecting ...</> | ||
<Link to={`/login`}>goto login page</Link> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters