Open History to see the last commit
1import React from "react";
2import { Typography, Link } from "@material-ui/core";
3import { Link as RouterLink } from "react-router-dom";
4import { withRouter } from "react-router";
5import { useQuery } from "react-query";
6import fetch from "./fetch";
7β
8function Film(props) {
9 const filmId = props.match.params.filmId;
10 const { data, status, error } = useQuery(`film-${filmId}`, () =>
11 fetch(`https://swapi.dev/api/films/${filmId}/`)
12 );
13β
14 if (status === "loading") return <p>Loading...</p>;
15 // this will not be necessary when v1 is released.
16 if (data == null) {
17 console.info("this shouldn't happen but it does 2");
18 return <p>Loading...</p>;
19 }
20 if (status === "error") return <p>Error :(</p>;
21 return (
22 <div>
23 <Typography variant="h2">{data.title}</Typography>
24 <Typography variant="body1">{data.opening_crawl}</Typography>
25 <br />
26 <Typography variant="h4">Characters</Typography>
27 {data.characters.map((character) => {
28 const characterUrlParts = character.split("/").filter(Boolean);
29 const characterId = characterUrlParts[characterUrlParts.length - 1];
30 return <Character id={characterId} key={characterId} />;
31 })}
32 </div>
33 );
34}
35β
36function Character(props) {
37 const { id } = props;
38 const { data, status, error } = useQuery(`character-${id}`, () =>
39 fetch(`https://swapi.dev/api/people/${props.id}/`)
40 );
41β
42 if (status !== "success") {
43 return null;
44 }
45β
46 return (
47 <article key={id}>
48 <Link component={RouterLink} to={`/characters/${id}`}>
49 <Typography variant="h6">{data.name}</Typography>
50 </Link>
51 </article>
52 );
53}
54β
55export default withRouter(Film);