From 7d6655c889f05804fc4cc4bf43bec87454f7ffbe Mon Sep 17 00:00:00 2001 From: franco sanchez Date: Thu, 29 Aug 2024 12:43:27 -0300 Subject: [PATCH] chore: some changes were made in the logic when displaying the book --- src/hooks/queries.ts | 7 +-- src/pages/Book.tsx | 111 +++++++++++++++++++++++++------------------ src/services/api.ts | 12 +++-- 3 files changed, 76 insertions(+), 54 deletions(-) diff --git a/src/hooks/queries.ts b/src/hooks/queries.ts index c3ff7fc..b9f6cd1 100644 --- a/src/hooks/queries.ts +++ b/src/hooks/queries.ts @@ -21,7 +21,7 @@ import { postRegister, getCheckUser, getUserAndBooks, - updateFavoriteStatus, + patchToggleFavorite, updateBook, deleteBook, deleteAccount, @@ -190,10 +190,11 @@ function useBook(pathUrl: string | undefined, token?: string | null) { }); } -function useFavoriteBook(body: boolean) { +function useFavoriteBook(body: any, isFavorite: boolean) { return useMutation({ mutationKey: ['favoriteBook'], - mutationFn: (id: string) => updateFavoriteStatus(id, body), + mutationFn: (userId: string | undefined) => + patchToggleFavorite(userId, body, isFavorite), onError: (error) => { console.error('Error updating favorite status'); }, diff --git a/src/pages/Book.tsx b/src/pages/Book.tsx index 9774f45..2965e08 100644 --- a/src/pages/Book.tsx +++ b/src/pages/Book.tsx @@ -83,17 +83,26 @@ export default function Book() { let btnFavorite; const { data } = useBook(pathUrl, getToken); + let bookObject = data; - const [isFavorite, setIsFavorite] = useState(data.isFavorite); + if (Array.isArray(bookObject)) { + const [obj] = data; - const { mutate: mutateFavorite } = useFavoriteBook(isFavorite); + bookObject = obj; + } else { + bookObject = data; + } + + const [isFavorite, setIsFavorite] = useState(bookObject.isFavorite); + + const { mutate: mutateFavorite } = useFavoriteBook(bookObject.id, isFavorite); const { mutate: mutateDelete, isSuccess, isPending } = useDeleteBook(); - const isCurrentUserAuthor = currentUser && currentUser.uid === data.userId; + const isCurrentUserAuthor = currentUser && currentUser.uid === bookObject.userId; useEffect(() => { - setIsFavorite(data.isFavorite); - }, [data.isFavorite]); + setIsFavorite(bookObject.isFavorite); + }, [bookObject.isFavorite]); if (currentUser && isCurrentUserAuthor) { btnMoreOptions = ( @@ -116,7 +125,7 @@ export default function Book() { const newFavoriteStatus = !isFavorite; setIsFavorite(newFavoriteStatus); - return await mutateFavorite(data.id); + return await mutateFavorite(currentUser?.uid); } if (currentUser) { @@ -200,9 +209,9 @@ export default function Book() { return ( <> @@ -320,12 +332,15 @@ export default function Book() { mb='2' > - + @@ -337,7 +352,7 @@ export default function Book() { mt='5' textTransform='uppercase' > - {data.title} + {bookObject.title} - {data.authors.map((author, index) => ( + {bookObject.authors.map((author, index) => ( ))} @@ -360,7 +375,7 @@ export default function Book() { Sinopsis - {data.synopsis} + {bookObject.synopsis} @@ -377,8 +392,8 @@ export default function Book() { @@ -388,7 +403,7 @@ export default function Book() { N° paginas: - {data.numberPages} + {bookObject.numberPages} @@ -399,8 +414,8 @@ export default function Book() { @@ -410,7 +425,7 @@ export default function Book() { Formato: - {data.format} + {bookObject.format} @@ -419,13 +434,13 @@ export default function Book() { - {data.category.map((category, index) => ( + {bookObject.category.map((category, index) => ( ))} @@ -458,19 +473,21 @@ export default function Book() { isOpen={isOpenShare} onClose={onCloseShare} shareUrl={shareUrl} - data={data.title} + data={bookObject.title} /> + } /> } + data={bookObject.category[0]} + booksComponent={ + + } />