(null);
+ useEffect(() => {
+ if (params?.hash) {
+ (async () => {
+ try {
+ const res = await fetch(API_URL + "/book/" + params.hash);
+ if (!res.ok) throw new Error(res.status + " " + res.statusText);
+
+ const book: unknown = await res.json();
+
+ if (validateResponse(book)) setBook(book);
+ } catch (err) {
+ if (process.env.NODE_ENV === "development") console.error(err);
+ setHasErr(true);
+ setLoading(false);
+ }
+ })();
+ }
+ }, []);
+
const [pagesReady, goToPage, currentPage, pagesNumber] = usePagination(
contentRef,
pageContainerRef,
pageRef,
- books && loading ? params?.hash : undefined,
- params?.hash && books && loading ? books[params.hash]?.content : undefined
+ book ? params?.hash : undefined,
+ params?.hash ? book?.content : undefined
);
const currentPageRef = useRef(currentPage);
@@ -68,35 +91,33 @@ export const BookView = ({ setLoading, loading }: IPageProps) => {
}
};
- if (books) {
- if (params?.hash && params.hash in books)
- return (
- <>
-
-
-
-
-
-
-
- {currentPage + 1} / {pagesNumber}
-
-
-
- >
- );
- return ;
- } else return <>>;
+ if (hasErr) return ;
+
+ return (
+ <>
+
+
+
+
+
+
+
+ {currentPage + 1} / {pagesNumber}
+
+
+
+ >
+ );
};
diff --git a/src/pages/UploadForm/index.tsx b/src/pages/UploadForm/index.tsx
index 820af34..ab9c05b 100644
--- a/src/pages/UploadForm/index.tsx
+++ b/src/pages/UploadForm/index.tsx
@@ -1,18 +1,15 @@
-import React, { useContext, useEffect, useState } from "react";
+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 "~/utils/api";
-import { BookListContext } from "~/context";
import { IPageProps } from "~/types/page";
export const UploadForm = ({ setLoading }: IPageProps) => {
const [error, setError] = useState("");
const [_, setLocation] = useLocation();
- const [__, saveBook] = useContext(BookListContext);
-
const processFile = async (file: File | undefined) => {
try {
if (validState(file)) {
@@ -22,10 +19,7 @@ export const UploadForm = ({ setLoading }: IPageProps) => {
const res = await submitFile(file);
setLoading(false);
- if (validateResponse(res)) {
- saveBook(res);
- setLocation("/");
- }
+ if (validateResponse(res)) setLocation("/");
}
} catch (err) {
setLoading(false);