File size: 1,044 Bytes
1e92f2d |
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 |
import React from 'react'
import { Typography, Link } from '@mui/material'
import { Link as RouterLink } from 'react-router-dom'
import { useQuery } from '@tanstack/react-query'
import fetch from './fetch'
export default function Characters(props) {
const { status, error, data } = useQuery({
queryKey: ['characters'],
queryFn: () => fetch(`https://swapi.dev/api/people/`),
})
if (status === 'pending') return <p>Loading...</p>
if (status === 'error') return <p>Error :(</p>
return (
<div>
<Typography variant="h2">Characters</Typography>
{data.results.map((person) => {
const personUrlParts = person.url.split('/').filter(Boolean)
const personId = personUrlParts[personUrlParts.length - 1]
return (
<article key={personId} style={{ margin: '16px 0 0' }}>
<Link component={RouterLink} to={`/characters/${personId}`}>
<Typography variant="h6">{person.name}</Typography>
</Link>
</article>
)
})}
</div>
)
}
|