Skip to content

Commit

Permalink
chore: type correction in hook useRefetchLocation and other changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Franqsanz committed Nov 7, 2023
1 parent 31e42ac commit 2c4bbb4
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 26 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/type-check.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: type check
name: Type Check

on:
pull_request:
Expand All @@ -24,3 +24,5 @@ jobs:
run: npm ci
- name: Run TypeScript type check
run: npm run ts-lint
- name: Typecheck
uses: andoshin11/[email protected]
6 changes: 3 additions & 3 deletions src/components/cards/MoreBooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import React from 'react';

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

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

return (
<>
Expand Down
43 changes: 28 additions & 15 deletions src/components/cards/RelatedBooks.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,42 @@
import React from 'react';
import { Box, Text } from '@chakra-ui/react';

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

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

if (relatedBooks.length > 0) {
uiCard = relatedBooks.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 libros relacionados.
</Text>
);
}

return (
<>
<ContainerRCard>
{relatedBooks.map(({ id, title, authors, pathUrl }: CardType) => (
<React.Fragment key={id}>
<RelatedCard
title={title}
authors={authors}
pathUrl={pathUrl}
refetchQueries={refetch}
/>
</React.Fragment>
))}
</ContainerRCard>
<ContainerRCard>{uiCard}</ContainerRCard>
</>
);
}
8 changes: 5 additions & 3 deletions src/components/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ interface ModalType extends DisclosureType {
data?: string;
}

interface ReleatedBooksType {
id?: string;
interface RelatedBooksType
extends Omit<CardType, 'title' | 'authors' | 'pathUrl'> {
data?: any;
currentBookId?: string | undefined;
refetch?: () => void;
}

interface SelectType extends Omit<SelectBooksType, 'label'> {
Expand Down Expand Up @@ -149,7 +151,7 @@ export type {
ModalCroppType,
ModalCropperType,
ModalType,
ReleatedBooksType,
RelatedBooksType,
DrawerType,
LanguageAndYearType,
BookSearchResultsType,
Expand Down
27 changes: 23 additions & 4 deletions src/hooks/useRefetchLocation.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,42 @@
import { useState, useEffect } from 'react';
import { useLocation } from 'react-router-dom';

export function useRefetchLocation(currentBookId, data, refetch) {
import { RelatedBooksType } from '../components/types';

type Book = {
pathUrl: string;
};

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

useEffect(() => {
const filteredBooks = data.filter((book) => {
const filteredBooks = data.filter((book: Book) => {
if (book.pathUrl === currentBookId) {
if (refetch !== undefined) {
refetch();
}
}

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

setRelatedBooks(filteredBooks);

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

return relatedBooks;
}

1 comment on commit 2c4bbb4

@vercel
Copy link

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

Please sign in to comment.