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