Switched from own router to wouter
This commit is contained in:
parent
9603c00db2
commit
21448cf91d
@ -16,6 +16,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"react": "^17.0.2",
|
"react": "^17.0.2",
|
||||||
"react-dom": "^17.0.2",
|
"react-dom": "^17.0.2",
|
||||||
"serve": "^12.0.0"
|
"serve": "^12.0.0",
|
||||||
|
"wouter": "^2.7.4"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,22 +1,19 @@
|
|||||||
import { Route } from "~/types/router";
|
|
||||||
import React from "react";
|
import React from "react";
|
||||||
|
import { Route, Switch } from "wouter";
|
||||||
|
|
||||||
import { Bookshelf } from "~/pages/Bookshelf";
|
import { Bookshelf } from "~/pages/Bookshelf";
|
||||||
|
import { BookView } from "~/pages/BookView";
|
||||||
import { UploadForm } from "~/pages/UploadForm";
|
import { UploadForm } from "~/pages/UploadForm";
|
||||||
|
|
||||||
import { Router } from "~/router";
|
|
||||||
import { Err404 } from "~/router/404";
|
|
||||||
|
|
||||||
import styles from "./App.module.css";
|
import styles from "./App.module.css";
|
||||||
|
|
||||||
const routes: Route[] = [
|
|
||||||
{ Component: Bookshelf, path: "/list" },
|
|
||||||
{ Component: UploadForm, path: "/upload" },
|
|
||||||
];
|
|
||||||
|
|
||||||
export const App = () => (
|
export const App = () => (
|
||||||
<div className={styles.container}>
|
<div className={styles.container}>
|
||||||
<Router routes={routes} startPath="/list" DefaultComponent={Err404} />
|
<Switch>
|
||||||
|
<Route path="/upload" component={UploadForm} />
|
||||||
|
<Route path="/" component={Bookshelf} />
|
||||||
|
<Route path="/:hash" component={BookView} />
|
||||||
|
</Switch>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
8
src/pages/BookView/index.tsx
Normal file
8
src/pages/BookView/index.tsx
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import React from "react";
|
||||||
|
import { useRoute } from "wouter";
|
||||||
|
|
||||||
|
export const BookView = () => {
|
||||||
|
const [match, params] = useRoute("/:hash");
|
||||||
|
|
||||||
|
return <div></div>;
|
||||||
|
};
|
@ -1,15 +1,17 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
|
import { useLocation } from "wouter";
|
||||||
|
|
||||||
import plusIcon from "~/assets/plus.svg";
|
import plusIcon from "~/assets/plus.svg";
|
||||||
import styles from "./AddBook.module.css";
|
import styles from "./AddBook.module.css";
|
||||||
import { BASE_URL } from "~/constants";
|
import { BASE_URL } from "~/constants";
|
||||||
import { goTo } from "~/router/goTo";
|
|
||||||
|
|
||||||
export const AddBook = () => {
|
export const AddBook = () => {
|
||||||
|
const [_, setLocation] = useLocation();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<button
|
<button
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
goTo(BASE_URL + "/upload");
|
setLocation("/upload");
|
||||||
}}
|
}}
|
||||||
className={styles.container}
|
className={styles.container}
|
||||||
>
|
>
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
import React, { useState } from "react";
|
import React, { useState } from "react";
|
||||||
|
import { useLocation } from "wouter";
|
||||||
|
|
||||||
import plusIcon from "~/assets/plus.svg";
|
import plusIcon from "~/assets/plus.svg";
|
||||||
import styles from "./UploadForm.module.css";
|
import styles from "./UploadForm.module.css";
|
||||||
import { submitFile, validateResponse, validState } from "~/api";
|
import { submitFile, validateResponse, validState } from "~/api";
|
||||||
import { saveBook } from "~/utils/localStorage";
|
import { saveBook } from "~/utils/localStorage";
|
||||||
import { BASE_URL } from "~/constants";
|
|
||||||
import { goTo } from "~/router/goTo";
|
|
||||||
|
|
||||||
export const UploadForm = () => {
|
export const UploadForm = () => {
|
||||||
const [error, setError] = useState("");
|
const [error, setError] = useState("");
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
|
const [_, setLocation] = useLocation();
|
||||||
|
|
||||||
const processFile = async (file: File | undefined) => {
|
const processFile = async (file: File | undefined) => {
|
||||||
try {
|
try {
|
||||||
@ -24,7 +24,7 @@ export const UploadForm = () => {
|
|||||||
|
|
||||||
if (validateResponse(res)) {
|
if (validateResponse(res)) {
|
||||||
saveBook(res, res.hash || Date.now().toString());
|
saveBook(res, res.hash || Date.now().toString());
|
||||||
goTo(BASE_URL + "/list");
|
setLocation("/");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
@ -1,8 +0,0 @@
|
|||||||
import React from "react";
|
|
||||||
|
|
||||||
export const Err404 = () => (
|
|
||||||
<>
|
|
||||||
<h1>Error 404</h1>
|
|
||||||
<h4>Path not found {window.location.pathname}</h4>
|
|
||||||
</>
|
|
||||||
);
|
|
@ -1,4 +0,0 @@
|
|||||||
export const goTo = (
|
|
||||||
href: string = window.location.href,
|
|
||||||
nextTitle: string = document.title
|
|
||||||
) => window.history.pushState({}, nextTitle, href);
|
|
@ -1,30 +0,0 @@
|
|||||||
import React from "react";
|
|
||||||
|
|
||||||
import { BASE_URL } from "~/constants";
|
|
||||||
import { Route } from "~/types/router";
|
|
||||||
import { Err404 } from "./404";
|
|
||||||
import { goTo } from "./goTo";
|
|
||||||
|
|
||||||
interface IRouterProps {
|
|
||||||
routes: Route[];
|
|
||||||
startPath?: string;
|
|
||||||
DefaultComponent?: React.FC;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const Router = ({
|
|
||||||
routes,
|
|
||||||
startPath,
|
|
||||||
DefaultComponent,
|
|
||||||
}: IRouterProps) => {
|
|
||||||
const currPath = window.location.pathname;
|
|
||||||
if (startPath && currPath === "/") goTo(BASE_URL + startPath);
|
|
||||||
|
|
||||||
for (const route of routes) {
|
|
||||||
if (currPath === route.path || currPath === route.path + "/")
|
|
||||||
return <route.Component />;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (DefaultComponent) return <DefaultComponent />;
|
|
||||||
|
|
||||||
return <Err404 />;
|
|
||||||
};
|
|
@ -1,4 +0,0 @@
|
|||||||
export interface Route {
|
|
||||||
path: string;
|
|
||||||
Component: React.FC;
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user