Skip to content

Commit

Permalink
chore: numerous changes were made and a new section in Book.tsx was a…
Browse files Browse the repository at this point in the history
…dded
  • Loading branch information
Franqsanz committed Nov 1, 2023
1 parent 641cef4 commit 80be1f0
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 51 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/links.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Links (Fail Fast)

on:
push:
pull_request:

jobs:
linkChecker:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Link Checker
uses: lycheeverse/[email protected]
with:
fail: true
45 changes: 0 additions & 45 deletions src/components/RelatedPost.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion src/components/cards/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function Card({
bg='green.50'
color='green.900'
icon={BsTag}
name={category[0]}
name={category && category[0]}
size='md'
isFocused={false}
tabIndex={-1}
Expand Down
14 changes: 14 additions & 0 deletions src/components/cards/ContainerRCard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react';
import { Flex, useColorModeValue } from '@chakra-ui/react';

export function ContainerRCard({ children }: { children: React.ReactNode }) {
const colorCard = useColorModeValue('gray.900', 'gray.100');

return (
<>
<Flex flexWrap={{ base: 'wrap', xl: 'nowrap' }} color={colorCard}>
{children}
</Flex>
</>
);
}
38 changes: 38 additions & 0 deletions src/components/cards/MoreBooks.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from 'react';

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

export default function MoreBooks({ currentBookId }: ReleatedBooksType) {
const { data, refetch } = useMoreBooks();
const moreBooks = useRefetchLocation(currentBookId, data, refetch);

// const moreBooks = data.filter((book: any) => {
// if (book.pathUrl === currentBookId) {
// refetch();
// }

// return book.pathUrl !== currentBookId;
// });

return (
<>
<ContainerRCard>
{moreBooks.map(({ id, title, authors, pathUrl }: CardType) => (
<React.Fragment key={id}>
<RelatedCard
id={id}
title={title}
authors={authors}
pathUrl={pathUrl}
refetchQueries={refetch}
/>
</React.Fragment>
))}
</ContainerRCard>
</>
);
}
30 changes: 30 additions & 0 deletions src/components/cards/RelatedBooks.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react';

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

export default function RelatedBooks({ currentBookId, id }: ReleatedBooksType) {
const { data, refetch } = useRelatedBooks(id);
const relatedBooks = useRefetchLocation(currentBookId, data, refetch);

return (
<>
<ContainerRCard>
{relatedBooks.map(({ id, title, authors, pathUrl }: CardType) => (
<React.Fragment key={id}>
<RelatedCard
id={id}
title={title}
authors={authors}
pathUrl={pathUrl}
refetchQueries={refetch}
/>
</React.Fragment>
))}
</ContainerRCard>
</>
);
}
5 changes: 3 additions & 2 deletions src/components/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface CardType {
title: string;
synopsis?: string;
authors: string[];
category: string[];
category?: string[];
year?: number;
language?: number;
sourceLink?: string;
Expand Down Expand Up @@ -92,7 +92,8 @@ interface ModalType extends DisclosureType {
}

interface ReleatedBooksType {
currentBookId: string | undefined;
id?: string;
currentBookId?: string | undefined;
}

interface SelectType extends Omit<SelectBooksType, 'label'> {
Expand Down
13 changes: 13 additions & 0 deletions src/hooks/querys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
getBook,
getBooksFilter,
getMoreBooks,
getRelatedBooks,
postBook,
} from '../services/api';
import { keys } from '../utils/utils';
Expand Down Expand Up @@ -113,6 +114,17 @@ function useMoreBooks() {
});
}

function useRelatedBooks(id: string | undefined) {
return useQuery({
queryKey: [keys.relatedBooks],
queryFn: () => getRelatedBooks(id),
suspense: true,
refetchOnWindowFocus: false,
cacheTime: 3000,
staleTime: 60000,
});
}

function useBook(pathUrl: string | undefined) {
return useQuery({
queryKey: [keys.one, pathUrl],
Expand All @@ -132,4 +144,5 @@ export {
useBook,
useFilter,
useMoreBooks,
useRelatedBooks,
};
23 changes: 23 additions & 0 deletions src/hooks/useRefetchLocation.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useState, useEffect } from 'react';
import { useLocation } from 'react-router-dom';

export function useRefetchLocation(currentBookId, data, refetch) {
const location = useLocation();
const [previousPathname, setPreviousPathname] = useState(location.pathname);
const [relatedBooks, setRelatedBooks] = useState([]);

useEffect(() => {
const filteredBooks = data.filter((book) => {
return book.pathUrl !== currentBookId;
});

setRelatedBooks(filteredBooks);

if (location.pathname !== previousPathname) {
refetch();
setPreviousPathname(location.pathname);
}
}, [data, currentBookId, location.pathname, refetch, previousPathname]);

return relatedBooks;
}
35 changes: 32 additions & 3 deletions src/pages/Book.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import { ModalShare } from '../components/ModalShare';
import { MyLink } from '../components/MyLink';

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

export default function Book() {
const shareUrl = window.location.href;
Expand Down Expand Up @@ -114,7 +115,7 @@ export default function Book() {
w='full'
maxW='1300px'
m='auto'
mb='25'
mb={{ base: 25, md: 32 }}
align='flex-start'
direction={{ base: 'column', lg: 'row-reverse' }}
>
Expand Down Expand Up @@ -314,6 +315,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 }}
>
Libros relacionados con{' '}
<Box as='span' fontWeight='semibold'>
{data.category[0]}
</Box>
</Box>
<Suspense
fallback={
<Box p='3'>
<Spinner size='lg' />
</Box>
}
>
<Box>
<RelatedBooks id={data.id} currentBookId={pathUrl} />
</Box>
</Suspense>
</Flex>
<Box my='10'>
<Divider borderColor='gray.400' />
</Box>
<Flex direction='column'>
<Box
p='2'
Expand All @@ -332,7 +361,7 @@ export default function Book() {
}
>
<Box>
<RelatedPost currentBookId={pathUrl} />
<MoreBooks currentBookId={pathUrl} />
</Box>
</Suspense>
</Flex>
Expand Down
7 changes: 7 additions & 0 deletions src/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ async function getMoreBooks() {
return data;
}

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

return data;
}

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

Expand All @@ -72,6 +78,7 @@ export {
getBooksFilter,
getAllFilterOptions,
getMoreBooks,
getRelatedBooks,
postBook,
deleteBook,
};
1 change: 1 addition & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const keys = {
paginate: 'BookPaginate',
filter: 'BooksFilter',
random: 'BooksRandom',
relatedBooks: 'RelatedBooks',
};

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

1 comment on commit 80be1f0

@vercel
Copy link

@vercel vercel bot commented on 80be1f0 Nov 1, 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-git-main-franqsanz.vercel.app
xbu-franqsanz.vercel.app
xbu.vercel.app

Please sign in to comment.