-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: type correction in hook useRefetchLocation and other changes
- Loading branch information
Showing
5 changed files
with
62 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name: type check | ||
name: Type Check | ||
|
||
on: | ||
pull_request: | ||
|
@@ -24,3 +24,5 @@ jobs: | |
run: npm ci | ||
- name: Run TypeScript type check | ||
run: npm run ts-lint | ||
- name: Typecheck | ||
uses: andoshin11/[email protected] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
2c4bbb4
There was a problem hiding this comment.
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