forked from TanStack/query
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEpisode.js
49 lines (42 loc) · 1.48 KB
/
Episode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import React from "react";
import { Typography, Link } from "@material-ui/core";
import { Link as RouterLink } from "react-router-dom";
import { useParams } from "react-router";
import { useQuery } from "react-query";
import fetch from "./fetch";
function Episode() {
const { episodeId } = useParams();
const { data, status } = useQuery(`episode-${episodeId}`, () =>
fetch(`https://rickandmortyapi.com/api/episode/${episodeId}`)
);
if (status === "loading") return <p>Loading...</p>;
if (status === "error") return <p>Error :(</p>;
return (
<div>
<Typography variant="h2">{data.name}</Typography>
<Typography variant="body1">{data.air_date}</Typography>
<br />
<Typography variant="h4">Characters</Typography>
{data.characters.map(character => {
const characterUrlParts = character.split("/").filter(Boolean);
const characterId = characterUrlParts[characterUrlParts.length - 1];
return <Character id={characterId} key={characterId} />;
})}
</div>
);
}
function Character({ id }) {
const { data, status } = useQuery(`character-${id}`, () =>
fetch(`https://rickandmortyapi.com/api/character/${id}`)
);
if (status === "loading") return <p>Loading...</p>;
if (status === "error") return <p>Error :(</p>;
return (
<article key={id}>
<Link component={RouterLink} to={`/characters/${id}`}>
<Typography variant="h6">{data.name}</Typography>
</Link>
</article>
);
}
export default Episode;