Skip to content

Commit

Permalink
Fix Code Break
Browse files Browse the repository at this point in the history
  • Loading branch information
shaurya35 committed Oct 30, 2024
1 parent f511360 commit fa2abe8
Showing 1 changed file with 40 additions and 43 deletions.
83 changes: 40 additions & 43 deletions client/src/Pages/Newarrivals.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,56 +63,53 @@ export const books = [
];

function Newarrivals({ onBookClick, highlightedBookId }) {
const bookRefs = useRef({});
// const bookRefs = useRef({});


return () => clearInterval(intervalId); // Cleanup interval on component unmount
}, []);
// const fetchRandomBooks = async () => {
// setIsLoading(true);
// try {
// const randomStartIndex = Math.floor(Math.random() * 100);
// const response = await fetch(`https://www.googleapis.com/books/v1/volumes?q=subject:fiction&maxResults=7&startIndex=${randomStartIndex}`);

const fetchRandomBooks = async () => {
setIsLoading(true);
try {
const randomStartIndex = Math.floor(Math.random() * 100);
const response = await fetch(`https://www.googleapis.com/books/v1/volumes?q=subject:fiction&maxResults=7&startIndex=${randomStartIndex}`);
// if (!response.ok) {
// throw new Error('HTTP Error! Status: ' + response.status);
// }

if (!response.ok) {
throw new Error('HTTP Error! Status: ' + response.status);
}

const data = await response.json();
console.log(data);
// const data = await response.json();
// console.log(data);


if (data.items) {
// if (data.items) {

const formattedBooks = data.items.map(item => ({
id: item.id,
bookTitle: item.volumeInfo.title,
authorName: item.volumeInfo.authors ? item.volumeInfo.authors[0] : 'Unknown Author',
imageURL: item.volumeInfo.imageLinks ? item.volumeInfo.imageLinks.thumbnail : 'https://via.placeholder.com/128x193.png',
category: item.volumeInfo.categories ? item.volumeInfo.categories[0] : 'Fiction'
}));
setBooks(formattedBooks);
}
else {
setError('No books found');
}
}
catch (error) {
console.error('Error fetching books: ', error);
setError('Falied to fetch books');
}
finally {
setIsLoading(false);
}
};

if (isLoading) {
return <div>Loading...</div>;
}
if (error) {
return <div>Error: {error}</div>;
}
// const formattedBooks = data.items.map(item => ({
// id: item.id,
// bookTitle: item.volumeInfo.title,
// authorName: item.volumeInfo.authors ? item.volumeInfo.authors[0] : 'Unknown Author',
// imageURL: item.volumeInfo.imageLinks ? item.volumeInfo.imageLinks.thumbnail : 'https://via.placeholder.com/128x193.png',
// category: item.volumeInfo.categories ? item.volumeInfo.categories[0] : 'Fiction'
// }));
// setBooks(formattedBooks);
// }
// else {
// setError('No books found');
// }
// }
// catch (error) {
// console.error('Error fetching books: ', error);
// setError('Falied to fetch books');
// }
// finally {
// setIsLoading(false);
// }
// };

// if (isLoading) {
// return <div>Loading...</div>;
// }
// if (error) {
// return <div>Error: {error}</div>;
// }

return (
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4 p-4">
Expand Down

0 comments on commit fa2abe8

Please sign in to comment.