Open History to see the last commit
1<script lang="ts">
2 import { createQuery, useQueryClient } from '@tanstack/svelte-query'
3 import { postsQueries } from './services/posts/queries'
4β
5 type Props = {
6 setPostId: (postId: string | null) => void
7 }
8β
9 let { setPostId }: Props = $props()
10β
11 const queryClient = useQueryClient()
12 const query = createQuery(() => postsQueries.list())
13</script>
14β
15<div>
16 <h1>Posts</h1>
17 <div>
18 {#if query.status === 'pending'}
19 Loading...
20 {:else if query.status === 'error'}
21 <span>Error: {query.error.message}</span>
22 {:else}
23 <div>
24 {#each query.data as post (post.id)}
25 <p>
26 <a
27 onclick={(e) => {
28 e.preventDefault()
29 setPostId(post.id)
30 }}
31 href="#{post.id}"
32 style={// We can find the existing query data here to show bold links for
33 // ones that are cached
34 queryClient.getQueryData(postsQueries.detail(post.id).queryKey)
35 ? 'font-weight: bold; color: green'
36 : ''}
37 >
38 {post.title}
39 </a>
40 </p>
41 {/each}
42 </div>
43 <div>{query.isFetching ? 'Background Updating...' : ' '}</div>
44 {/if}
45 </div>
46</div>