Open History to see the last commit
1import { createQuery } from '@tanstack/solid-query'
2import type { Component } from 'solid-js'
3import { resetErrorBoundaries } from 'solid-js'
4import { createSignal } from 'solid-js'
5import { For } from 'solid-js'
6import { fetchPost } from '~/utils/api'
7import { Example } from './example'
8import { QueryBoundary } from './query-boundary'
9β
10export interface PostViewerProps {
11 deferStream?: boolean
12 sleep?: number
13 simulateError?: boolean
14}
15β
16export const PostViewer: Component<PostViewerProps> = (props) => {
17 const [simulateError, setSimulateError] = createSignal(props.simulateError)
18 const [postId, setPostId] = createSignal(1)
19β
20 const query = createQuery(() => ({
21 queryKey: ['posts', postId()],
22 queryFn: () =>
23 fetchPost({
24 postId: postId(),
25 sleep: props.sleep,
26 simulateError:
27 simulateError() || (simulateError() !== false && postId() === 5),
28 }),
29 deferStream: props.deferStream,
30 throwOnError: true,
31 }))
32β
33 return (
34 <Example
35 title="Post Query"
36 deferStream={props.deferStream}
37 sleep={props.sleep}
38 >
39 <div style={{ 'margin-top': '20px' }}>
40 <button
41 onClick={() => {
42 setPostId(Math.max(postId() - 1, 1))
43 resetErrorBoundaries()
44 }}
45 >
46 Previous Post
47 </button>
48 <button
49 onClick={() => {
50 setPostId(Math.min(postId() + 1, 100))
51 resetErrorBoundaries()
52 }}
53 >
54 Next Post
55 </button>
56 </div>
57β
58 {/* NOTE: without this extra wrapping div, for some reason solid ends up printing two errors... feels like a bug in solid. */}
59 <div>
60 <QueryBoundary
61 query={query}
62 loadingFallback={<div class="loader">loading post...</div>}
63 errorFallback={(err, retry) => (
64 <div>
65 <div class="error">{err.message}</div>
66 <button
67 onClick={() => {
68 setSimulateError(false)
69 retry()
70 }}
71 >
72 retry
73 </button>
74 </div>
75 )}
76 >
77 {(posts) => (
78 <For each={posts}>
79 {(post) => (
80 <div style={{ 'margin-top': '20px' }}>
81 <b>
82 [post {postId()}] {post.title}
83 </b>
84 <p>{post.body}</p>
85 </div>
86 )}
87 </For>
88 )}
89 </QueryBoundary>
90 </div>
91 </Example>
92 )
93}