Open History to see the last commit
1/* @refresh reload */
2β
3import {
4 QueryClient,
5 QueryClientProvider,
6 useQueries,
7} from '@tanstack/solid-query'
8import { SolidQueryDevtools } from '@tanstack/solid-query-devtools'
9import { Index, Match, Show, Switch, createSignal } from 'solid-js'
10import { render } from 'solid-js/web'
11import { charactersQueries } from './services/characters/queries'
12β
13const queryClient = new QueryClient({
14 defaultOptions: {
15 queries: {
16 retry: 0,
17 },
18 },
19})
20β
21function Characters(props: { ids: Array<number> }) {
22 const characters = useQueries(() => ({
23 queries: props.ids.map((id) => charactersQueries.detail(id)),
24 }))
25β
26 return (
27 <Index each={characters}>
28 {(character) => (
29 <Switch>
30 <Match when={character().status === 'pending'}>Loading...</Match>
31 <Match when={character().status === 'error'}>Error...</Match>
32 <Match when={character().data === null}>Not found ...</Match>
33 <Match when={character().data}>
34 {(data) => (
35 <div style={{ display: 'flex' }}>
36 <img
37 width={100}
38 height={100}
39 alt={data().image}
40 src={data().image}
41 />
42 <div style={{ 'padding-left': '10px', 'text-align': 'left' }}>
43 <h5 style={{ 'margin-top': 0, 'margin-bottom': '5px' }}>
44 {data().name} (#{data().id})
45 </h5>
46 <p style={{ 'margin-top': 0, 'margin-bottom': '2px' }}>
47 Status: {data().status}
48 </p>
49 <p style={{ 'margin-top': 0, 'margin-bottom': '2px' }}>
50 Species: {data().species}
51 </p>
52 <p style={{ 'margin-top': 0, 'margin-bottom': '2px' }}>
53 Gender: {data().gender}
54 </p>
55 </div>
56 </div>
57 )}
58 </Match>
59 </Switch>
60 )}
61 </Index>
62 )
63}
64β
65function App() {
66 const [ids, setIds] = createSignal<Array<number>>([])
67 const [idsToLoad, setIdsToLoad] = createSignal<Array<number>>([])
68 const [error, setError] = createSignal('')
69β
70 return (
71 <div class="App">
72 <h2>Character fetcher</h2>
73 <div>
74 <label style={{ display: 'block' }}>
75 Character ids (numeric, comma separated)
76 </label>
77 <input
78 style={{ display: 'inline-block' }}
79 placeholder="ids"
80 onInput={(event) => {
81 const parsedIds = event.currentTarget.value
82 .split(',')
83 .map((id) => id.trim())
84β
85 if (parsedIds.every((id) => /^\d+$/.test(id))) {
86 setIds(parsedIds.map(Number))
87 setError('')
88 } else {
89 setError('Invalid.. only use numbers and comma')
90 }
91 }}
92 />
93 </div>
94β
95 <Show when={error()}>
96 <h3 style={{ color: 'red' }}>{error()}</h3>
97 </Show>
98β
99 <button
100 type="button"
101 disabled={!!error()}
102 onClick={() => {
103 setIdsToLoad(ids())
104 }}
105 >
106 Load characters
107 </button>
108 <div>
109 <h4 style={{ 'text-align': 'left' }}>Characters:</h4>
110 <Characters ids={idsToLoad()} />
111 </div>
112 </div>
113 )
114}
115β
116function Root() {
117 return (
118 <QueryClientProvider client={queryClient}>
119 <App />
120 <SolidQueryDevtools initialIsOpen />
121 </QueryClientProvider>
122 )
123}
124β
125const rootElement = document.getElementById('root')
126if (!rootElement) throw new Error('Missing #root element')
127render(() => <Root />, rootElement)