From 73bd4bada9736df8e178069ef7aa304ad6a6ae90 Mon Sep 17 00:00:00 2001 From: k1arac Date: Mon, 29 Apr 2024 18:01:13 -0400 Subject: [PATCH] removing HTML tags from descriptions and read more option --- front-end/src/pages/Book.js | 35 +++++++++++++++++++++++++++++---- front-end/src/pages/LongText.js | 27 +++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 front-end/src/pages/LongText.js 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