diff --git a/src/App/index.tsx b/src/App/index.tsx index 66e42af..533f1ea 100644 --- a/src/App/index.tsx +++ b/src/App/index.tsx @@ -1,12 +1,24 @@ +import { Route } from "@type/router"; import React from "react"; -import Bookshelf from "../Bookshelf"; + +import { Bookshelf } from "../Bookshelf"; +import { Router } from "../router"; +import { Err404 } from "../router/404"; import { UploadForm } from "../UploadForm"; + import styles from "./App.module.css"; -export const App = () => ( -
- -
-); +const routes: Route[] = [ + { Component: Bookshelf, path: "/list" }, + { Component: UploadForm, path: "/upload" }, +]; + +export const App = () => { + return ( +
+ +
+ ); +}; export default App; diff --git a/src/router/404.tsx b/src/router/404.tsx new file mode 100644 index 0000000..94d3e07 --- /dev/null +++ b/src/router/404.tsx @@ -0,0 +1,8 @@ +import React from "react"; + +export const Err404 = () => ( + <> +

Error 404

+

Path not found {window.location.pathname}

+ +); diff --git a/src/router/index.tsx b/src/router/index.tsx new file mode 100644 index 0000000..0c0f914 --- /dev/null +++ b/src/router/index.tsx @@ -0,0 +1,30 @@ +import React from "react"; + +import { BASE_URL } from "../constants"; +import { Route } from "@type/router"; +import { Err404 } from "./404"; + +interface IRouterProps { + routes: Route[]; + startPath?: string; + DefaultComponent?: React.FC; +} + +export const Router = ({ + routes, + startPath, + DefaultComponent, +}: IRouterProps) => { + const currPath = window.location.pathname; + if (startPath && currPath === "/") + window.location.href = BASE_URL + startPath; + + for (const route of routes) { + if (currPath === route.path || currPath === route.path + "/") + return ; + } + + if (DefaultComponent) return ; + + return ; +}; diff --git a/src/type/router.ts b/src/type/router.ts new file mode 100644 index 0000000..99a492f --- /dev/null +++ b/src/type/router.ts @@ -0,0 +1,4 @@ +export interface Route { + path: string; + Component: React.FC; +}