|
| 1 | +import { For, Match, Show, Switch } from 'solid-js' |
| 2 | +import { A, useParams } from '@solidjs/router' |
| 3 | +import { useQuery } from '@tanstack/solid-query' |
| 4 | +import { charactersQueries } from './services/characters/queries' |
| 5 | +import { episodesQueries } from './services/episodes/queries' |
| 6 | +import { locationsQueries } from './services/locations/queries' |
| 7 | + |
| 8 | +function Character() { |
| 9 | + const params = useParams<{ characterId: string }>() |
| 10 | + |
| 11 | + const query = useQuery(() => charactersQueries.detail(params.characterId)) |
| 12 | + |
| 13 | + return ( |
| 14 | + <Switch> |
| 15 | + <Match when={query.status === 'pending'}> |
| 16 | + <p>Loading...</p> |
| 17 | + </Match> |
| 18 | + <Match when={query.status === 'error'}> |
| 19 | + <p>Error :(</p> |
| 20 | + </Match> |
| 21 | + <Match when={query.data}> |
| 22 | + {(data) => { |
| 23 | + const locationId = () => { |
| 24 | + const locationUrlParts = data() |
| 25 | + .location.url.split('/') |
| 26 | + .filter(Boolean) |
| 27 | + return locationUrlParts[locationUrlParts.length - 1] |
| 28 | + } |
| 29 | + |
| 30 | + return ( |
| 31 | + <div> |
| 32 | + <h2 class="text-4xl">{data().name}</h2> |
| 33 | + <p> |
| 34 | + <strong>Gender</strong>: {data().gender} |
| 35 | + </p> |
| 36 | + <p> |
| 37 | + <strong>Status</strong>: {data().status} |
| 38 | + </p> |
| 39 | + <p> |
| 40 | + <strong>Species</strong>: {data().species} |
| 41 | + </p> |
| 42 | + <p> |
| 43 | + <strong>Origin</strong>: {data().origin.name} |
| 44 | + </p> |
| 45 | + <p> |
| 46 | + <strong>Location</strong>:{' '} |
| 47 | + <Location locationId={locationId()} /> |
| 48 | + </p> |
| 49 | + |
| 50 | + <h4 class="text-2xl pt-4">Episodes</h4> |
| 51 | + <For each={data().episode}> |
| 52 | + {(episode) => { |
| 53 | + const episodeUrlParts = episode.split('/').filter(Boolean) |
| 54 | + const episodeId = episodeUrlParts[episodeUrlParts.length - 1] |
| 55 | + return <Episode episodeId={episodeId} /> |
| 56 | + }} |
| 57 | + </For> |
| 58 | + </div> |
| 59 | + ) |
| 60 | + }} |
| 61 | + </Match> |
| 62 | + </Switch> |
| 63 | + ) |
| 64 | +} |
| 65 | + |
| 66 | +function Episode(props: { episodeId: string }) { |
| 67 | + const query = useQuery(() => episodesQueries.detail(props.episodeId)) |
| 68 | + |
| 69 | + return ( |
| 70 | + <Show when={query.status === 'success' && query.data}> |
| 71 | + {(data) => ( |
| 72 | + <article> |
| 73 | + <A |
| 74 | + class="text-blue-500 hover:underline" |
| 75 | + href={`/episodes/${props.episodeId}`} |
| 76 | + > |
| 77 | + <h6 class="text-lg"> |
| 78 | + {data().episode}. {data().name} - {data().air_date} |
| 79 | + </h6> |
| 80 | + </A> |
| 81 | + </article> |
| 82 | + )} |
| 83 | + </Show> |
| 84 | + ) |
| 85 | +} |
| 86 | + |
| 87 | +function Location(props: { locationId: string }) { |
| 88 | + const query = useQuery(() => locationsQueries.detail(props.locationId)) |
| 89 | + |
| 90 | + return ( |
| 91 | + <Switch> |
| 92 | + <Match when={query.status === 'pending'}> |
| 93 | + <span>Loading...</span> |
| 94 | + </Match> |
| 95 | + <Match when={query.status === 'error'}> |
| 96 | + <span>Error :(</span> |
| 97 | + </Match> |
| 98 | + <Match when={query.data}> |
| 99 | + {(data) => ( |
| 100 | + <span> |
| 101 | + {data().name} - {data().type} |
| 102 | + </span> |
| 103 | + )} |
| 104 | + </Match> |
| 105 | + </Switch> |
| 106 | + ) |
| 107 | +} |
| 108 | + |
| 109 | +export default Character |
0 commit comments