diff --git a/front-end/src/pages/Book.js b/front-end/src/pages/Book.js index 3354129..5c317d6 100644 --- a/front-end/src/pages/Book.js +++ b/front-end/src/pages/Book.js @@ -4,6 +4,7 @@ import Header from "../components/header"; import CurrentlyReading from "./CurrentlyReading"; import ReadingFinished from "./ReadingFinished"; import ReadingWishlist from "./ReadingWishlist"; +import LongText from "./LongText"; const BookPage = ({ loggedInUser }) => { @@ -12,6 +13,7 @@ const BookPage = ({ loggedInUser }) => { const { bookId } = useParams(); const [isFavorite, setIsFavorite] = useState(true); + const FavoriteIcon = ({ isFavorite }) => ( {isFavorite ? "⭐️" : "✩"} {/* Filled star if favorite, else outline */} @@ -42,6 +44,8 @@ const BookPage = ({ loggedInUser }) => { return userData[listType].includes(bookId); }; + + useEffect(() => { const token = localStorage.getItem("token"); if (bookId && token) { @@ -59,7 +63,25 @@ const BookPage = ({ loggedInUser }) => { return response.json(); }) .then((bookData) => { - setBook(bookData); + + setBook(bookData); // set the book info on the page + + /* altering the description here*/ + const regex = /(<([^>]+)>)/gi; + const updatedDescription = bookData.description.replace(regex, ""); + + setBook(() => ({ + id: bookData.id, + title: bookData.title, + author: bookData.author, + thumbnail: bookData.thumbnail, + description: updatedDescription, + pages: bookData.pages, + genres: bookData.genres, + publishedDate: bookData.publishedDate, + + })); + setIsFavorite(bookData.isFavorite); fetch(`http://localhost:3001/api/users/${loggedInUser._id}/books`, { @@ -80,11 +102,13 @@ const BookPage = ({ loggedInUser }) => { const isFinishedBook = checkIfBookInList(userData, bookId, 'finishedReading'); const isInWishList = checkIfBookInList(userData, bookId, 'wishlist'); + setBook((prevBook) => ({ ...prevBook, currentlyReading: isCurrentlyReading, finishedBooks: isFinishedBook, wishList: isInWishList, + })); }) .catch((error) => { @@ -131,10 +155,13 @@ const BookPage = ({ loggedInUser }) => {

{book.title}

by {book.author || "Unknown Author"}

-

+

Description:{" "} - {book.description || "No description available"} -

+
+ +
+ +

Pages:{" "} {book.pages || "Unknown"} diff --git a/front-end/src/pages/LongText.js b/front-end/src/pages/LongText.js new file mode 100644 index 0000000..c0ecf44 --- /dev/null +++ b/front-end/src/pages/LongText.js @@ -0,0 +1,27 @@ +import React, { useEffect, useState } from "react"; + +const LongText = ({ content, limit }) => { + const [showAll, setShowAll] = useState(false); + + const showMore = () => setShowAll(true); + const showLess = () => setShowAll(false); + + if (content.length <= limit) { + // there is nothing more to show + return

{content}
+ } + if (showAll) { + // We show the extended text and a link to reduce it + return
+ {content} + +
+ } + // In the final case, we show a text with ellipsis and a `Read more` button + const toShow = content.substring(0, limit) + "..."; + return
+ {toShow} + +
+} +export default LongText; \ No newline at end of file