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