From f35c4c473d5040f1aa7d5ebf34b2af6fa226c623 Mon Sep 17 00:00:00 2001 From: 0ry5 Date: Fri, 13 Sep 2024 19:38:46 +0200 Subject: [PATCH] feat(GoogleApi): add google api to BookModal --- .../shared/components/modals/BookModal.tsx | 60 ++++++++++++------- 1 file changed, 37 insertions(+), 23 deletions(-) diff --git a/frontend/src/shared/components/modals/BookModal.tsx b/frontend/src/shared/components/modals/BookModal.tsx index fb852c1..5c086b6 100644 --- a/frontend/src/shared/components/modals/BookModal.tsx +++ b/frontend/src/shared/components/modals/BookModal.tsx @@ -29,7 +29,20 @@ export const BookModal = ({ }, [book, reset]); const { scannerError, setScannerRef } = useScanner({ - onDetected: (result) => { + 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); + } setValue("isbn", result); setShowScanner(false); }, @@ -38,6 +51,7 @@ export const BookModal = ({ const values = useWatch({ control }); const [submitError, setSubmitError] = useState(); + const onSubmit = useCallback( async (data: Partial) => { setSubmitError(undefined); @@ -89,7 +103,7 @@ export const BookModal = ({ } + icon={} /> {!showScanner && (
{ ev.preventDefault(); onSubmit(values); }} > - - + + ISBN - -
+ +
setShowScanner(true)} > @@ -135,7 +149,7 @@ export const BookModal = ({
{!formState.errors.isbn ? "ISBN is required" @@ -143,8 +157,8 @@ export const BookModal = ({ - - + + Title @@ -153,15 +167,15 @@ export const BookModal = ({ isInvalid={!!formState.errors.title} /> - + {!values.title ? "Title is required" : formState.errors.title?.message} - - + + Year published @@ -187,15 +201,15 @@ export const BookModal = ({ isInvalid={!!formState.errors.published} /> - + {!values.published ? "Year published is required" : formState.errors.published?.message} - - + + Shelf @@ -205,21 +219,21 @@ export const BookModal = ({ > {Object.keys(bookShelfs).map((key) => ( ))} - + {!values.shelf ? "Shelf is required" : formState.errors.shelf?.message} -
+