feat(DNBApi): add api from deutsche national bibliothek

This commit is contained in:
0ry5 2024-09-25 21:33:17 +02:00
parent 46198913b1
commit 65e9aa2e0b
9 changed files with 1942 additions and 422 deletions
frontend/src/shared/components/modals

View file

@ -6,6 +6,8 @@ import { useForm, useWatch } from "react-hook-form";
import { Book, bookShelfs } from "../../../types/Book";
import { useScanner } from "../../utils/useScanner";
import { BookModalProps } from "./types";
import { tryGoogleBooksApi } from "../../../pages/Main/utils/tryGoogleBooksApi";
import { tryDeutscheNationalBibliothekApi } from "../../../pages/Main/utils/tryDeutscheNationalBibliothekApi";
type BookForm = Pick<Book, "isbn" | "title" | "shelf" | "published">;
@ -27,24 +29,33 @@ export const BookModal = ({
reset(book);
}, [book, reset]);
const { scannerError, setScannerRef } = useScanner({
onDetected: async (result) => {
const googleBooks = await (
await fetch(
"https://www.googleapis.com/books/v1/volumes?q=isbn:" + result
)
).json();
if ("items" in googleBooks) {
console.log(googleBooks);
setValue(
"published",
googleBooks.items[0].volumeInfo.publishedDate.substring(0, 4)
);
setValue("title", googleBooks.items[0].volumeInfo.title);
const [processingDetection, setProcessingDetection] = useState(false);
const onDetected = useCallback(
async (result: string) => {
if (!processingDetection) {
setProcessingDetection(true);
const apiResponses = (
await Promise.all([
tryDeutscheNationalBibliothekApi(result),
tryGoogleBooksApi(result),
])
).filter((b) => !!b) as Pick<Book, "published" | "title">[];
if (apiResponses.length) {
Object.entries(apiResponses[0]).forEach(([key, value]) => {
setValue(key as keyof BookForm, value);
});
}
setValue("isbn", result);
setShowScanner(false);
setProcessingDetection(false);
}
setValue("isbn", result);
setShowScanner(false);
},
[processingDetection, setValue]
);
const { scannerError, setScannerRef } = useScanner({
onDetected,
});
const values = useWatch({ control });