Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

Character.jsx

117 lines3.1 KB

1import React from 'react'
2import {
3 Typography,
4 Link,
5 Table,
6 TableBody,
7 TableCell,
8 TableContainer,
9 TableHead,
10 TableRow,
11 Paper,
12} from '@mui/material'
13import { useParams, Link as RouterLink } from 'react-router-dom'
14import { useQuery } from '@tanstack/react-query'
15import fetch from './fetch'
16​
17function Character() {
18 const { characterId } = useParams()
19 const { status, data } = useQuery({
20 queryKey: ['character', characterId],
21 queryFn: () =>
22 fetch(`https://rickandmortyapi.com/api/character/${characterId}`),
23 })
24​
25 if (status === 'pending') return <p>Loading...</p>
26 if (status === 'error') return <p>Error :(</p>
27​
28 const locationUrlPars = data.location.url.split('/').filter(Boolean)
29 const locationId = locationUrlPars[locationUrlPars.length - 1]
30​
31 return (
32 <div>
33 <Typography variant="h2">{data.name}</Typography>
34 <TableContainer component={Paper} style={{ maxWidth: '400px' }}>
35 <Table size="small" aria-label="simple table">
36 <TableHead>
37 <TableRow>
38 <TableCell>Feature</TableCell>
39 <TableCell>Value</TableCell>
40 </TableRow>
41 </TableHead>
42 <TableBody>
43 <TableRow>
44 <TableCell>Gender</TableCell>
45 <TableCell>{data.gender}</TableCell>
46 </TableRow>
47 <TableRow>
48 <TableCell>Status</TableCell>
49 <TableCell>{data.status}</TableCell>
50 </TableRow>
51 <TableRow>
52 <TableCell>Species</TableCell>
53 <TableCell>{data.species}</TableCell>
54 </TableRow>
55 <TableRow>
56 <TableCell>Origin</TableCell>
57 <TableCell>{data.origin.name}</TableCell>
58 </TableRow>
59 <TableRow>
60 <TableCell>Location</TableCell>
61 <TableCell>
62 <Location id={locationId} />
63 </TableCell>
64 </TableRow>
65 </TableBody>
66 </Table>
67 </TableContainer>
68 <br />
69 <Typography variant="h4">Episodes</Typography>
70 {data.episode.map((episode) => {
71 const episodeUrlParts = episode.split('/').filter(Boolean)
72 const episodeId = episodeUrlParts[episodeUrlParts.length - 1]
73​
74 return <Episode id={episodeId} key={`episode-${episodeId}`} />
75 })}
76 </div>
77 )
79​
80function Episode({ id }) {
81 const { data, status } = useQuery({
82 queryKey: ['episode', id],
83 queryFn: () => fetch(`https://rickandmortyapi.com/api/episode/${id}`),
84 })
85​
86 if (status !== 'success') {
87 return null
88 }
89​
90 return (
91 <article key={id}>
92 <Link component={RouterLink} to={`/episodes/${id}`}>
93 <Typography variant="h6">
94 {data.episode}. {data.name} - {data.air_date}
95 </Typography>
96 </Link>
97 </article>
98 )
100​
101function Location({ id }) {
102 const { data, status } = useQuery({
103 queryKey: ['location', id],
104 queryFn: () => fetch(`https://rickandmortyapi.com/api/location/${id}`),
105 })
106​
107 if (status === 'pending') return <p>Loading...</p>
108 if (status === 'error') return <p>Error :(</p>
109​
110 return (
111 <>
112 {data.name} - {data.type}
113 </>
114 )
116​
117export default Character