({ setPostId }: { setPostId: (postId: string | null) => void })
| 37 | } |
| 38 | |
| 39 | function Posts({ setPostId }: { setPostId: (postId: string | null) => void }) { |
| 40 | const queryClient = useQueryClient() |
| 41 | const { status, data, error, isFetching } = useQuery(postsQueries.list()) |
| 42 | |
| 43 | return ( |
| 44 | <div> |
| 45 | <h1>Posts</h1> |
| 46 | <div> |
| 47 | {status === 'pending' ? ( |
| 48 | 'Loading...' |
| 49 | ) : status === 'error' ? ( |
| 50 | <span>Error: {error.message}</span> |
| 51 | ) : ( |
| 52 | <> |
| 53 | <div> |
| 54 | {data.map((post) => ( |
| 55 | <p key={post.id}> |
| 56 | <a |
| 57 | onClick={() => setPostId(post.id)} |
| 58 | href="#" |
| 59 | style={ |
| 60 | // We can find the existing query data here to show bold links for |
| 61 | // ones that are cached |
| 62 | queryClient.getQueryData( |
| 63 | postsQueries.detail(post.id).queryKey, |
| 64 | ) |
| 65 | ? { |
| 66 | fontWeight: 'bold', |
| 67 | color: 'green', |
| 68 | } |
| 69 | : {} |
| 70 | } |
| 71 | > |
| 72 | {post.title} |
| 73 | </a> |
| 74 | </p> |
| 75 | ))} |
| 76 | </div> |
| 77 | <div>{isFetching ? 'Background Updating...' : ' '}</div> |
| 78 | </> |
| 79 | )} |
| 80 | </div> |
| 81 | </div> |
| 82 | ) |
| 83 | } |
| 84 | |
| 85 | function Post({ |
| 86 | postId, |
nothing calls this directly
no test coverage detected