Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

index.tsx

126 lines3.5 KB

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