From 5daac7df1d24148d157c8587970cb3dca3a23274 Mon Sep 17 00:00:00 2001 From: Kot Anton <707myemail@gmail.com> Date: Wed, 23 Oct 2024 21:44:31 -0400 Subject: [PATCH 1/2] added toggle favorite functionality and covered with unit tests --- package-lock.json | 9 +++++ package.json | 1 + src/components/bookList/BookList.js | 29 ++++++++++++++-- src/components/bookList/BookList.test.js | 42 ++++++++++++++++++++++++ src/redux/slices/BooksSlice.js | 9 ++++- src/utils/createBook.js | 1 + 6 files changed, 87 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index d388125..64f25ea 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,7 @@ "@testing-library/user-event": "^13.5.0", "react": "^18.3.1", "react-dom": "^18.3.1", + "react-icons": "^5.3.0", "react-redux": "^9.1.2", "react-scripts": "5.0.1", "uuid": "^10.0.0", @@ -14866,6 +14867,14 @@ "resolved": "https://registry.npmjs.org/react-error-overlay/-/react-error-overlay-6.0.11.tgz", "integrity": "sha512-/6UZ2qgEyH2aqzYZgQPxEnz33NJ2gNsnHA2o5+o4wW9bLM/JYQitNP9xPhsXwC08hMMovfGe/8retsdDsczPRg==" }, + "node_modules/react-icons": { + "version": "5.3.0", + "resolved": "https://registry.npmjs.org/react-icons/-/react-icons-5.3.0.tgz", + "integrity": "sha512-DnUk8aFbTyQPSkCfF8dbX6kQjXA9DktMeJqfjrg6cK9vwQVMxmcA3BfP4QoiztVmEHtwlTgLFsPuH2NskKT6eg==", + "peerDependencies": { + "react": "*" + } + }, "node_modules/react-is": { "version": "17.0.2", "resolved": "https://registry.npmjs.org/react-is/-/react-is-17.0.2.tgz", diff --git a/package.json b/package.json index 60a88b1..834e960 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "@testing-library/user-event": "^13.5.0", "react": "^18.3.1", "react-dom": "^18.3.1", + "react-icons": "^5.3.0", "react-redux": "^9.1.2", "react-scripts": "5.0.1", "uuid": "^10.0.0", diff --git a/src/components/bookList/BookList.js b/src/components/bookList/BookList.js index 86f4a4a..6e85a31 100644 --- a/src/components/bookList/BookList.js +++ b/src/components/bookList/BookList.js @@ -1,14 +1,27 @@ import { useDispatch, useSelector } from 'react-redux' +import { TbStar } from 'react-icons/tb' +import { TbStarFilled } from 'react-icons/tb' +import { + selectBook, + deleteBook, + toggleFavorite, +} from '../../redux/slices/BooksSlice' import './BookList.css' -import { selectBook } from '../../redux/slices/BooksSlice' -import { deleteBook } from '../../redux/slices/BooksSlice' const BookList = () => { const books = useSelector(selectBook) const dispatch = useDispatch() const handleDeleteBook = (id) => { - dispatch(deleteBook(id)) + books.forEach((book) => { + if (book.id === id && !book.isFavorite) { + dispatch(deleteBook(id)) + } + }) + } + + const toggleFavoriteBook = (id) => { + dispatch(toggleFavorite(id)) } return ( @@ -30,6 +43,16 @@ const BookList = () => {
+ toggleFavoriteBook(book.id)} + data-testid={`isFavorite_${book.isFavorite}`} + > + {book.isFavorite ? ( + + ) : ( + + )} +