Open History to see the last commit
1import { useQuery } from '@tanstack/react-query'
2import { Link as RouterLink, useParams } from 'react-router'
3import { charactersQueries } from './services/characters/queries'
4import { episodesQueries } from './services/episodes/queries'
5import { locationsQueries } from './services/locations/queries'
6β
7function Character() {
8 const params = useParams()
9 const characterId = params.characterId!
10β
11 const { status, data } = useQuery(charactersQueries.detail(characterId))
12β
13 if (status === 'pending') return <p>Loading...</p>
14 if (status === 'error') return <p>Error :(</p>
15β
16 const locationUrlParts = data.location.url.split('/').filter(Boolean)
17 const locationId = locationUrlParts[locationUrlParts.length - 1]
18β
19 return (
20 <div>
21 <h2 className="text-4xl">{data.name}</h2>
22 <p>
23 <strong>Gender</strong>: {data.gender}
24 </p>
25 <p>
26 <strong>Status</strong>: {data.status}
27 </p>
28 <p>
29 <strong>Species</strong>: {data.species}
30 </p>
31 <p>
32 <strong>Origin</strong>: {data.origin.name}
33 </p>
34 <p>
35 <strong>Location</strong>:{' '}
36 {locationId ? <Location locationId={locationId} /> : data.location.name}
37 </p>
38β
39 <h4 className="text-2xl pt-4">Episodes</h4>
40 {data.episode.map((episode) => {
41 const episodeUrlParts = episode.split('/').filter(Boolean)
42 const episodeId = episodeUrlParts[episodeUrlParts.length - 1]
43 return <Episode episodeId={episodeId} key={`${episodeId}`} />
44 })}
45 </div>
46 )
47}
48β
49function Episode({ episodeId }: { episodeId: string }) {
50 const { data, status } = useQuery(episodesQueries.detail(episodeId))
51β
52 if (status === 'success') {
53 return (
54 <article key={episodeId}>
55 <RouterLink
56 className="text-blue-500 hover:underline"
57 to={`/episodes/${episodeId}`}
58 >
59 <h6 className="text-lg">
60 {data.episode}. {data.name} - {data.air_date}
61 </h6>
62 </RouterLink>
63 </article>
64 )
65 }
66}
67β
68function Location({ locationId }: { locationId: string }) {
69 const { data, status } = useQuery(locationsQueries.detail(locationId))
70β
71 if (status === 'pending') return <span>Loading...</span>
72 if (status === 'error') return <span>Error :(</span>
73β
74 return (
75 <span>
76 {data.name} - {data.type}
77 </span>
78 )
79}
80β
81export default Character