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