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