Switched from own router to wouter

This commit is contained in:
Dmitriy Shishkov 2021-07-16 03:03:24 +05:00
parent 9603c00db2
commit 21448cf91d
No known key found for this signature in database
GPG Key ID: 14358F96FCDD8060
9 changed files with 24 additions and 62 deletions

View File

@ -16,6 +16,7 @@
"dependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2",
"serve": "^12.0.0"
"serve": "^12.0.0",
"wouter": "^2.7.4"
}
}

View File

@ -1,22 +1,19 @@
import { Route } from "~/types/router";
import React from "react";
import { Route, Switch } from "wouter";
import { Bookshelf } from "~/pages/Bookshelf";
import { BookView } from "~/pages/BookView";
import { UploadForm } from "~/pages/UploadForm";
import { Router } from "~/router";
import { Err404 } from "~/router/404";
import styles from "./App.module.css";
const routes: Route[] = [
{ Component: Bookshelf, path: "/list" },
{ Component: UploadForm, path: "/upload" },
];
export const App = () => (
<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>
);

View File

@ -0,0 +1,8 @@
import React from "react";
import { useRoute } from "wouter";
export const BookView = () => {
const [match, params] = useRoute("/:hash");
return <div></div>;
};

View File

@ -1,15 +1,17 @@
import React from "react";
import { useLocation } from "wouter";
import plusIcon from "~/assets/plus.svg";
import styles from "./AddBook.module.css";
import { BASE_URL } from "~/constants";
import { goTo } from "~/router/goTo";
export const AddBook = () => {
const [_, setLocation] = useLocation();
return (
<button
onClick={() => {
goTo(BASE_URL + "/upload");
setLocation("/upload");
}}
className={styles.container}
>

View File

@ -1,15 +1,15 @@
import React, { useState } from "react";
import { useLocation } from "wouter";
import plusIcon from "~/assets/plus.svg";
import styles from "./UploadForm.module.css";
import { submitFile, validateResponse, validState } from "~/api";
import { saveBook } from "~/utils/localStorage";
import { BASE_URL } from "~/constants";
import { goTo } from "~/router/goTo";
export const UploadForm = () => {
const [error, setError] = useState("");
const [loading, setLoading] = useState(false);
const [_, setLocation] = useLocation();
const processFile = async (file: File | undefined) => {
try {
@ -24,7 +24,7 @@ export const UploadForm = () => {
if (validateResponse(res)) {
saveBook(res, res.hash || Date.now().toString());
goTo(BASE_URL + "/list");
setLocation("/");
}
}
} catch (err) {

View File

@ -1,8 +0,0 @@
import React from "react";
export const Err404 = () => (
<>
<h1>Error 404</h1>
<h4>Path not found {window.location.pathname}</h4>
</>
);

View File

@ -1,4 +0,0 @@
export const goTo = (
href: string = window.location.href,
nextTitle: string = document.title
) => window.history.pushState({}, nextTitle, href);

View File

@ -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 />;
};

View File

@ -1,4 +0,0 @@
export interface Route {
path: string;
Component: React.FC;
}