Open History to see the last commit
1import React from 'react'
2import { Typography, Link } from '@mui/material'
3import { useParams, Link as RouterLink } from 'react-router-dom'
4import { useQuery } from '@tanstack/react-query'
5import fetch from './fetch'
6β
7function Episode() {
8 const { episodeId } = useParams()
9 const { data, status } = useQuery({
10 queryKey: ['episode', episodeId],
11 queryFn: () =>
12 fetch(`https://rickandmortyapi.com/api/episode/${episodeId}`),
13 })
14β
15 if (status === 'pending') return <p>Loading...</p>
16 if (status === 'error') return <p>Error :(</p>
17β
18 return (
19 <div>
20 <Typography variant="h2">{data.name}</Typography>
21 <Typography variant="body1">{data.air_date}</Typography>
22 <br />
23 <Typography variant="h4">Characters</Typography>
24 {data.characters.map((character) => {
25 const characterUrlParts = character.split('/').filter(Boolean)
26 const characterId = characterUrlParts[characterUrlParts.length - 1]
27 return <Character id={characterId} key={characterId} />
28 })}
29 </div>
30 )
31}
32β
33function Character({ id }) {
34 const { data, status } = useQuery({
35 queryKey: ['character', id],
36 queryFn: () => fetch(`https://rickandmortyapi.com/api/character/${id}`),
37 })
38β
39 if (status === 'pending') return <p>Loading...</p>
40 if (status === 'error') return <p>Error :(</p>
41β
42 return (
43 <article key={id}>
44 <Link component={RouterLink} to={`/characters/${id}`}>
45 <Typography variant="h6">{data.name}</Typography>
46 </Link>
47 </article>
48 )
49}
50β
51export default Episode