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 { withRouter } from 'react-router'
15import { useQuery } from '@tanstack/react-query'
16import fetch from './fetch'
17β
18function Character(props) {
19 const characterId = props.match.params.characterId
20 const { status, error, data } = useQuery({
21 queryKey: ['character', characterId],
22 queryFn: () => fetch(`https://swapi.dev/api/people/${characterId}/`),
23 })
24β
25 if (status === 'loading') return <p>Loading...</p>
26 if (status === 'error') return <p>Error :(</p>
27β
28 console.info({ data, status, error })
29 const homeworldUrlParts = data.homeworld.split('/').filter(Boolean)
30 const homeworldId = homeworldUrlParts[homeworldUrlParts.length - 1]
31β
32 if (status !== 'success') {
33 return null
34 }
35 return (
36 <div>
37 <Typography variant="h2">{data.name}</Typography>
38 <TableContainer component={Paper} style={{ maxWidth: '400px' }}>
39 <Table size="small" aria-label="simple table">
40 <TableHead>
41 <TableRow>
42 <TableCell>Feature</TableCell>
43 <TableCell>Value</TableCell>
44 </TableRow>
45 </TableHead>
46 <TableBody>
47 <TableRow>
48 <TableCell>Born</TableCell>
49 <TableCell>{data.birth_year}</TableCell>
50 </TableRow>
51 <TableRow>
52 <TableCell>Eyes</TableCell>
53 <TableCell>{data.eye_color}</TableCell>
54 </TableRow>
55 <TableRow>
56 <TableCell>Hair</TableCell>
57 <TableCell>{data.hair_color}</TableCell>
58 </TableRow>
59 <TableRow>
60 <TableCell>Height</TableCell>
61 <TableCell>{data.height}</TableCell>
62 </TableRow>
63 <TableRow>
64 <TableCell>Mass</TableCell>
65 <TableCell>{data.mass}</TableCell>
66 </TableRow>
67 <TableRow>
68 <TableCell>Homeworld</TableCell>
69 <TableCell>
70 <Homeworld id={homeworldId} />
71 </TableCell>
72 </TableRow>
73 </TableBody>
74 </Table>
75 </TableContainer>
76 <br />
77 <Typography variant="h4">Films</Typography>
78 {data.films.map((film) => {
79 const filmUrlParts = film.split('/').filter(Boolean)
80 const filmId = filmUrlParts[filmUrlParts.length - 1]
81 return <Film id={filmId} key={`Film-${filmId}`} />
82 })}
83 </div>
84 )
85}
86β
87function Film(props) {
88 const { id } = props
89 const { data, status } = useQuery({
90 queryKey: ['film', id],
91 queryFn: () => fetch(`https://swapi.dev/api/films/${id}/`),
92 })
93β
94 if (status !== 'success') {
95 return null
96 }
97 return (
98 <article key={id}>
99 <Link component={RouterLink} to={`/films/${id}`}>
100 <Typography variant="h6">
101 {data.episode_id}. {data.title}
102 </Typography>
103 </Link>
104 </article>
105 )
106}
107β
108function Homeworld(props) {
109 const { id } = props
110 const { data, status } = useQuery({
111 queryKey: ['homeworld', id],
112 queryFn: () => fetch(`https://swapi.dev/api/planets/${id}/`),
113 })
114β
115 if (status !== 'success') {
116 return null
117 }
118β
119 return data.name
120}
121β
122export default withRouter(Character)