From 2fa808df33f1a621f59d828ab4891e3bd903b6e0 Mon Sep 17 00:00:00 2001 From: asher-lab Date: Fri, 20 Jan 2023 21:46:51 +0800 Subject: [PATCH] fix routing by using length and conditional checks --- src/layouts/CheckoutPage/BookCheckoutPage.tsx | 84 ++++++++++--------- 1 file changed, 44 insertions(+), 40 deletions(-) diff --git a/src/layouts/CheckoutPage/BookCheckoutPage.tsx b/src/layouts/CheckoutPage/BookCheckoutPage.tsx index d05d61b..a017327 100644 --- a/src/layouts/CheckoutPage/BookCheckoutPage.tsx +++ b/src/layouts/CheckoutPage/BookCheckoutPage.tsx @@ -16,40 +16,40 @@ export default function BookCheckoutPage({ book_id }: Props) { const [book, setBook] = useState(); const [isLoading, setIsLoading] = useState(true); const [httpError, setHttpError] = useState(null); - useEffect(() => { - const fetchBooks = async () => { - const baseUrl: string = `http://localhost:8080/api/books/${book_id}/hello`; - - const response = await fetch(baseUrl); + if (book_id !== undefined && book_id.length > 0) { + console.log(`The value of bookid: ${book_id}`); + const fetchBooks = async () => { + const baseUrl: string = `http://localhost:8080/api/books/${book_id}`; + const response = await fetch(baseUrl); + if (!response.ok) { + throw new Error('Something went wrong!'); + } + const responseJson = await response.json(); - if (!response.ok) { - throw new Error('Something went wrong!'); - } - const responseJson = await response.json(); + const loadedBooks: BookModel = { + id: responseJson.id, + title: responseJson.title, + author: responseJson.author, + description: responseJson.description, + copies: responseJson.copies, + copiesAvailable: responseJson.copiesAvailable, + category: responseJson.category, + img: responseJson.img, + }; - const loadedBooks: BookModel = { - id: responseJson.id, - title: responseJson.title, - author: responseJson.author, - description: responseJson.description, - copies: responseJson.copies, - copiesAvailable: responseJson.copiesAvailable, - category: responseJson.category, - img: responseJson.img, + setBook(loadedBooks); + setIsLoading(false); }; - setBook(loadedBooks); - setIsLoading(false); - }; - - // catch if there are any errors in async - fetchBooks().catch((error: any) => { - setIsLoading(false); - setHttpError(error.message); - }); - window.scrollTo(0, 0); - }, []); // each time the current page changes, we wanted to recall this hook. + // catch if there are any errors in async + fetchBooks().catch((error: any) => { + setIsLoading(false); + setHttpError(error.message); + }); + window.scrollTo(0, 0); + } + }, [book_id, httpError]); // each time the current page changes, we wanted to recall this hook. if (isLoading) { return ( @@ -58,18 +58,15 @@ export default function BookCheckoutPage({ book_id }: Props) { ); } - - if (httpError) { - return ( -
-

{httpError}

-
- ); - } - + // if (httpError) { + // return ( + //
+ //

{httpError}

+ //
+ // ); + // } return (
-

Value of data is {book_id}

1 2 @@ -78,7 +75,14 @@ export default function BookCheckoutPage({ book_id }: Props) { - ImageTable + {book?.img !== undefined ? ( + ImageTable + ) : null} {book?.title}