Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request #38 from SELab-2/react-router
Browse files Browse the repository at this point in the history
added routing
  • Loading branch information
EmmaVandewalle authored Mar 3, 2024
2 parents a3d9c96 + 6254c48 commit ada1130
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 9 deletions.
41 changes: 40 additions & 1 deletion frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"lint": "tsc --noEmit && eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
"react-dom": "^18.2.0",
"react-router-dom": "^6.22.2"
},
"devDependencies": {
"@types/react": "^18.2.56",
Expand Down
33 changes: 31 additions & 2 deletions frontend/src/main.tsx
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>,
)
2 changes: 1 addition & 1 deletion frontend/src/pages/admin/HomeAdmin.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {JSX} from "react";

export function HomeAdmin(): JSX.Element {
export default function HomeAdmin(): JSX.Element {
return (
<>Homescreen for an admin</>
)
Expand Down
38 changes: 38 additions & 0 deletions frontend/src/pages/error.tsx
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'
}
}
2 changes: 1 addition & 1 deletion frontend/src/pages/login/LoginScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {JSX} from "react";

export function LoginScreen(): JSX.Element {
export default function LoginScreen(): JSX.Element {
return (
<>Login screen</>
)
Expand Down
12 changes: 12 additions & 0 deletions frontend/src/pages/root.tsx
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>
</>
)
}
2 changes: 1 addition & 1 deletion frontend/src/pages/student/HomeStudent.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {JSX} from "react";

export function HomeStudent(): JSX.Element {
export default function HomeStudent(): JSX.Element {
return (
<>Homescreen for a student</>
)
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/teacher/HomeTeacher.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {JSX} from "react";

export function HomeTeacher(): JSX.Element {
export default function HomeTeacher(): JSX.Element {
return (
<>Homescreen for a teacher</>
)
Expand Down

0 comments on commit ada1130

Please sign in to comment.