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