Skip to content

Commit

Permalink
feat: the option to add to favorites has been implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Franqsanz committed Aug 28, 2024
1 parent cd258bd commit d9c9a11
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 8 deletions.
5 changes: 4 additions & 1 deletion src/components/skeletons/SkeletonDBook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ export function SkeletonDetailsBook() {
justify='space-between'
>
<Skeleton w='100px' h='30px' rounded='lg'></Skeleton>
<Skeleton w='40px' h='30px' rounded='lg'></Skeleton>
<Flex gap='2'>
<Skeleton w='40px' h='30px' rounded='lg'></Skeleton>
<Skeleton w='40px' h='30px' rounded='lg'></Skeleton>
</Flex>
</Flex>
<Flex
w='full'
Expand Down
14 changes: 14 additions & 0 deletions src/hooks/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
postRegister,
getCheckUser,
getUserAndBooks,
updateFavoriteStatus,
updateBook,
deleteBook,
deleteAccount,
Expand Down Expand Up @@ -189,6 +190,16 @@ function useBook(pathUrl: string | undefined, token?: string | null) {
});
}

function useFavoriteBook(body: boolean) {
return useMutation({
mutationKey: ['favoriteBook'],
mutationFn: (id: string) => updateFavoriteStatus(id, body),
onError: (error) => {
console.error('Error updating favorite status');
},
});
}

// Usuarios

function useUserRegister(body: any) {
Expand Down Expand Up @@ -295,6 +306,9 @@ export {
useMostViewedBooks,
useRelatedBooks,
useMoreBooksAuthors,
useFavoriteBook,

// Usuarios
useUserRegister,
useCheckUser,
useUserData,
Expand Down
54 changes: 47 additions & 7 deletions src/pages/Book.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { lazy, Suspense } from 'react';
import React, { lazy, Suspense, useEffect, useState } from 'react';
import { useParams, useNavigate, NavLink } from 'react-router-dom';
import {
Box,
Expand All @@ -22,11 +22,12 @@ import {
} from 'react-icons/fi';
import { BsTag } from 'react-icons/bs';
import { FaCheckCircle } from 'react-icons/fa';
import { MdOutlineFavoriteBorder, MdOutlineFavorite } from 'react-icons/md';
import LazyLoad from 'react-lazy-load';
import Atropos from 'atropos/react';
import 'atropos/css';

import { useBook, useDeleteBook } from '@hooks/queries';
import { useBook, useFavoriteBook, useDeleteBook } from '@hooks/queries';
import { handleImageLoad } from '@utils/utils';
import { MainHead } from '@components/layout/Head';
import { MyTag } from '@components/ui/MyTag';
Expand Down Expand Up @@ -79,12 +80,21 @@ export default function Book() {
} = useDisclosure();
let uiLink;
let btnMoreOptions;
let btnFavorite;

const { data } = useBook(pathUrl, getToken);
const { mutate, isSuccess, isPending } = useDeleteBook();

const [isFavorite, setIsFavorite] = useState<boolean>(data.isFavorite);

const { mutate: mutateFavorite } = useFavoriteBook(isFavorite);
const { mutate: mutateDelete, isSuccess, isPending } = useDeleteBook();

const isCurrentUserAuthor = currentUser && currentUser.uid === data.userId;

useEffect(() => {
setIsFavorite(data.isFavorite);
}, [data.isFavorite]);

if (currentUser && isCurrentUserAuthor) {
btnMoreOptions = (
<Tooltip label='Más Opciones' fontSize='sm' bg='black' color='white'>
Expand All @@ -102,6 +112,33 @@ export default function Book() {
);
}

async function handleToggleFavorite() {
const newFavoriteStatus = !isFavorite;
setIsFavorite(newFavoriteStatus);

return await mutateFavorite(data.id);
}

if (currentUser) {
btnFavorite = (
<Tooltip
label={isFavorite ? 'Eliminar de Favoritos' : 'Agregar a Favoritos'}
fontSize='sm'
bg='black'
color='white'
>
<Button mt={{ base: 1, md: 5 }} size='sm' onClick={handleToggleFavorite}>
<Flex align='center' justify='center'>
<Icon
as={isFavorite ? MdOutlineFavorite : MdOutlineFavoriteBorder}
boxSize={5}
/>
</Flex>
</Button>
</Tooltip>
);
}

if (isSuccess) {
myToast({
title: 'Libro eliminado',
Expand All @@ -118,15 +155,15 @@ export default function Book() {
bxSize: 5,
});

navigate('/explore', { replace: true });
return navigate('/explore', { replace: true });
}

function handleDeleteBook() {
mutate(data.id);
return mutateDelete(data.id);
}

function handleGoBack() {
navigate(-1);
return navigate(-1);
}

if (data.sourceLink === '') {
Expand Down Expand Up @@ -189,7 +226,10 @@ export default function Book() {
Volver
</Flex>
</Button>
{btnMoreOptions}
<Flex gap='2'>
{btnFavorite}
{btnMoreOptions}
</Flex>
</Flex>
<ModalOptions
isOpen={isOpenOptions}
Expand Down
9 changes: 9 additions & 0 deletions src/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ async function getAllFilterOptions() {
return await fetchData(`${API_URL}/books/options`);
}

async function updateFavoriteStatus(id: string | undefined, body: any) {
return await fetchData(`${API_URL}/books/favorite/${id}`, {
method: 'PATCH',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ id, isFavorite: body }),
});
}

async function postBook(books: any) {
return await fetchData(`${API_URL}/books`, {
method: 'POST',
Expand Down Expand Up @@ -134,6 +142,7 @@ export {
getMostViewedBooks,
getRelatedBooks,
getMoreBooksAuthors,
updateFavoriteStatus,
postBook,
deleteBook,
updateBook,
Expand Down

0 comments on commit d9c9a11

Please sign in to comment.