Skip to content

Commit f5cd9d4

Browse files
authored
Merge pull request #13 from nejcrogelsek/feature/12-queryById
Feature/12 query by
2 parents 3c096df + c0c1cc6 commit f5cd9d4

File tree

6 files changed

+80
-13
lines changed

6 files changed

+80
-13
lines changed

README.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,19 @@ Let's say the data doesn't change that often. We can set the stale time to 30 se
3939

4040
0 seconds is possibly the safest value you can set to stale time, which is also the default value.
4141

42-
3. react-query refetchOnMount
42+
**3. react-query refetchOnMount**
4343

4444
If set to true, the query will refetch on mount if the data is stale. If set to false, will disable additional instances of a query to trigger background refetches. If set to 'always', the query will always refetch on mount. If set to a function, the function will be executed with the latest data and query to compute the value Defaults to true.
4545

4646
Default value is set to true.
4747

48-
4. react-query refetchOnWindowFocus
48+
**4. react-query refetchOnWindowFocus**
4949

5050
If set to true, the query will refetch on window focus if the data is stale. If set to false, the query will not refetch on window focus. If set to 'always', the query will always refetch on window focus. If set to a function, the function will be executed with the latest data and query to compute the value. Defaults to true.
5151

5252
Default value is set to true.
5353

54-
5. react-query refetchInterval - Polling
54+
**5. react-query refetchInterval - Polling**
5555

5656
Fetching data at regular intervals. For example: If you have component that shows real time price of different stocks, you might want to fetch the data every second so is in sync with UI.
5757

@@ -61,24 +61,33 @@ Default value is set to false.
6161

6262
Polling is paused when the window lose focus. To fix that issue we can set `refetchIntervalInBackground` property to true.
6363

64-
6. Homework (Solution on branch: feature/09-homework)
64+
**6. Homework (Solution on branch: feature/09-homework)**
6565

6666
Combine polling with callbacks. Use the `refetchInterval` option to pull the api data every 3 seconds. Behind the scenes add a fourth superhero of your choice to the superheroes array in `db.json`.
6767

68-
a.) Within the onSuccess callback check if the number of heroes is 4 and ifit is the case I want you to stop the polling.
69-
b.) Within the onError callback I want you to stop the polling.
68+
a.) Within the onSuccess callback check if the number of heroes is 4 and ifit is the case I want you to stop the polling.
69+
b.) Within the onError callback I want you to stop the polling.
7070

7171
Hint:
7272
Mantain state variable whose initial value is 3000. State variable will be assigned to `refetchInterval` configuration. In callbacks check for the response / errors and set the state variable to false.
7373

74-
7. react-query select - Data Transformation
74+
**7. react-query select - Data Transformation**
7575

7676
For data tranformation react-query provides us with `select` flag.
7777

7878
`select` is function that receives onSuccess response and transform out data in the way we want it.
7979

8080
In our example we transformed data to be an array of just heroes names instead of whole response.
8181

82+
**8. QueryById - 2 solutions**
83+
84+
We can query by id in two ways (look commits under branch name: `feature/12-queryById`).
85+
86+
a.) Solution 1 or first commit: **QueryById with manuali passed ID**
87+
- We can manually pass id into fetch function: file `useSuperHeroData`
88+
b.) Solution 2 or second commit: **QueryById with react-query automatic passed id**
89+
- React query automatically pass id into fetch function: file `useSuperHeroData`
90+
8291
## Available Scripts
8392

8493
In the project directory, you can run:

src/lib/hooks/useSuperHeroData.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { useQuery } from 'react-query'
2+
import axios from 'axios'
3+
import { SuperHero } from 'interfaces'
4+
5+
interface Props {
6+
onSuccess: (response: any) => void
7+
onError: (error: Error) => void
8+
heroId: string
9+
}
10+
11+
const fetchSuperHero = ({ queryKey }: { queryKey: any[] }) => {
12+
const id = queryKey[1]
13+
return axios.get(`http://localhost:4000/superheroes/${id}`)
14+
}
15+
16+
export const useSuperHeroData = ({ onSuccess, onError, heroId }: Props) => {
17+
return useQuery(['super-hero', heroId], fetchSuperHero, {
18+
onError,
19+
onSuccess,
20+
})
21+
}

src/lib/hooks/useSuperHeroesData.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,5 @@ export const useSuperHeroesData = ({ onSuccess, onError }: Props) => {
1515
return useQuery('super-heroes', fetchSuperHeroes, {
1616
onError,
1717
onSuccess,
18-
select: (response) => {
19-
const result = response.data.map((hero: SuperHero) => hero.name)
20-
return result
21-
},
2218
})
2319
}

src/pages/RQSuperHero/RQSuperHero.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { useSuperHeroData } from 'lib/hooks/useSuperHeroData'
2+
import { FC } from 'react'
3+
import { useParams } from 'react-router-dom'
4+
5+
const RQSuperHero: FC = () => {
6+
const onError = (error: Error) => {
7+
console.log('Perform side effect after encountering error', error)
8+
}
9+
10+
const onSuccess = (response: any) => {
11+
console.log('Perform side effect after encountering error', response)
12+
}
13+
14+
const { heroId } = useParams()
15+
const { data, isLoading, isError, error } = useSuperHeroData({ onSuccess, onError, heroId: heroId ?? '' })
16+
17+
if (isLoading) {
18+
return <h2>Loading...</h2>
19+
}
20+
21+
if (isError && error instanceof Error) {
22+
return <h2>{error.message}</h2>
23+
}
24+
return (
25+
<div className="rq-super-hero">
26+
{data?.data.name} - {data?.data.alterEgo}
27+
</div>
28+
)
29+
}
30+
31+
export default RQSuperHero

src/pages/RQSuperHeroes/RQSuperHeroes.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { FC } from 'react'
22
import { useSuperHeroesData } from 'lib/hooks/useSuperHeroesData'
3+
import { SuperHero } from 'interfaces'
4+
import { Link } from 'react-router-dom'
35

46
interface Props {}
57

@@ -28,8 +30,10 @@ const RQSuperHeroes: FC<Props> = (props: Props) => {
2830
<>
2931
<h2>RQ Super Heroes</h2>
3032
<ul>
31-
{data.map((heroName: string, index: number) => (
32-
<li key={index}>{heroName}</li>
33+
{data.data.map((hero: SuperHero, index: number) => (
34+
<li key={index}>
35+
<Link to={`/rq-superheroes/${hero.id}`}>{hero.name}</Link>
36+
</li>
3337
))}
3438
</ul>
3539
</>

src/routes/Routes.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Home from 'pages/Home/Home'
55
import Page404 from 'pages/Page404/Page404'
66
import SuperHeroes from 'pages/SuperHeroes/SuperHeroes'
77
import RQSuperHeroes from 'pages/RQSuperHeroes/RQSuperHeroes'
8+
import RQSuperHero from 'pages/RQSuperHero/RQSuperHero'
89

910
export enum RouteType {
1011
PUBLIC,
@@ -33,6 +34,11 @@ export const AppRoutes: AppRoute[] = [
3334
path: '/rq-superheroes',
3435
children: <RQSuperHeroes />,
3536
},
37+
{
38+
type: RouteType.PUBLIC,
39+
path: '/rq-superheroes/:heroId',
40+
children: <RQSuperHero />,
41+
},
3642
]
3743

3844
const Routes: FC = () => {

0 commit comments

Comments
 (0)