Skip to content

Commit

Permalink
feat: added to see books by the same author in related form
Browse files Browse the repository at this point in the history
  • Loading branch information
Franqsanz committed Nov 16, 2023
1 parent bc031e4 commit cd090c5
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/components/cards/MoreBooksAuthors.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React from 'react';
import { Box, Text } from '@chakra-ui/react';

import { RelatedCard } from './RelatedCard';
import { ContainerRCard } from './ContainerRCard';
import { CardType, RelatedBooksType } from '../types';
import { useMoreBooksAuthors } from '../../hooks/querys';
import { useRefetchLocation } from '../../hooks/useRefetchLocation';

export default function MoreBooksAuthors({
currentBookId,
id,
}: RelatedBooksType) {
const { data, refetch } = useMoreBooksAuthors(id);
const moreBooksAuthors = useRefetchLocation({ currentBookId, data, refetch });
let uiCard;

if (moreBooksAuthors.length > 0) {
uiCard = moreBooksAuthors.map(
({ id, title, authors, pathUrl }: CardType) => (
<React.Fragment key={id}>
<RelatedCard
title={title}
authors={authors}
pathUrl={pathUrl}
refetchQueries={refetch}
/>
</React.Fragment>
),
);
} else {
uiCard = (
<Text ml='4' fontStyle='italic' textAlign='center'>
<Box as='span' fontStyle='normal'>
🔍 📚
</Box>{' '}
No se encontraron más libros del mismo autor.
</Text>
);
}

return (
<>
<ContainerRCard>{uiCard}</ContainerRCard>
</>
);
}
12 changes: 12 additions & 0 deletions src/hooks/querys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
getBooksFilter,
getMoreBooks,
getRelatedBooks,
getMoreBooksAuthors,
postBook,
} from '../services/api';
import { keys } from '../utils/utils';
Expand Down Expand Up @@ -124,6 +125,16 @@ function useRelatedBooks(id: string | undefined) {
});
}

function useMoreBooksAuthors(id: string | undefined) {
return useSuspenseQuery({
queryKey: [keys.moreBooksAuthors, id],
queryFn: () => getMoreBooksAuthors(id),
refetchOnWindowFocus: false,
gcTime: 3000,
staleTime: 50000,
});
}

function useBook(pathUrl: string | undefined) {
return useSuspenseQuery({
queryKey: [keys.one, pathUrl],
Expand All @@ -143,4 +154,5 @@ export {
useFilter,
useMoreBooks,
useRelatedBooks,
useMoreBooksAuthors,
};
31 changes: 31 additions & 0 deletions src/pages/Book.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import { ModalShare } from '../components/ModalShare';
import { MyLink } from '../components/MyLink';

const Categories = lazy(() => import('../components/Categories'));
const MoreBooksAuthors = lazy(
() => import('../components/cards/MoreBooksAuthors'),
);
const RelatedBooks = lazy(() => import('../components/cards/RelatedBooks'));
const MoreBooks = lazy(() => import('../components/cards/MoreBooks'));

Expand Down Expand Up @@ -298,6 +301,34 @@ export default function Book() {
<Box my='10'>
<Divider borderColor='gray.400' />
</Box>
<Flex direction='column'>
<Box
p='2'
mb='3'
fontSize={{ base: 'xl', md: '2xl' }}
textAlign={{ base: 'center', lg: 'left' }}
ml={{ base: 0, lg: 2 }}
>
Más libros del autor{' '}
<Box as='span' fontWeight='semibold'>
{data.authors[0]}
</Box>
</Box>
<Suspense
fallback={
<Box p='3'>
<Spinner size='lg' />
</Box>
}
>
<Box>
<MoreBooksAuthors id={data.id} currentBookId={pathUrl} />
</Box>
</Suspense>
</Flex>
<Box my='10'>
<Divider borderColor='gray.400' />
</Box>
<Flex direction='column'>
<Box
p='2'
Expand Down
7 changes: 7 additions & 0 deletions src/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ async function getRelatedBooks(id: string | undefined) {
return data;
}

async function getMoreBooksAuthors(id: string | undefined) {
const data = await fetchData(`${API_URL}/books/more-books-authors/${id}`);

return data;
}

async function getAllFilterOptions() {
const data = await fetchData(`${API_URL}/books/options`);

Expand All @@ -79,6 +85,7 @@ export {
getAllFilterOptions,
getMoreBooks,
getRelatedBooks,
getMoreBooksAuthors,
postBook,
deleteBook,
};
1 change: 1 addition & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const keys = {
filter: 'BooksFilter',
random: 'BooksRandom',
relatedBooks: 'RelatedBooks',
moreBooksAuthors: 'MoreBooksAuthors',
};

function handleImageLoad(e: React.SyntheticEvent) {
Expand Down

1 comment on commit cd090c5

@vercel
Copy link

@vercel vercel bot commented on cd090c5 Nov 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

xbu – ./

xbu-franqsanz.vercel.app
xbu-git-main-franqsanz.vercel.app
xbu.vercel.app

Please sign in to comment.