Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

Posts.svelte

61 lines1.4 KB

1<script lang="ts">
2 import { useQueryClient, createQuery } from '@tanstack/svelte-query'
3 import { resolve } from '$app/paths'
4 import { postsQueries } from './services/posts/queries'
5​
6 const client = useQueryClient()
7​
8 let limit = 10
9​
10 const posts = createQuery(() => postsQueries.list(limit))
11</script>
12​
13<div>
14 <div>
15 {#if posts.status === 'pending'}
16 <span>Loading...</span>
17 {:else if posts.status === 'error'}
18 <span>Error: {posts.error.message}</span>
19 {:else}
20 <ul>
21 {#each posts.data as post (post.id)}
22 <article>
23 <a
24 href={resolve('/[postId]', { postId: String(post.id) })}
25 style={// We can use the queryCache here to show bold links for
26 // ones that are cached
27 client.getQueryData(postsQueries.detail(post.id).queryKey)
28 ? 'font-weight: bold; color: indianred'
29 : 'cursor: pointer'}
30 >
31 {post.title}
32 </a>
33 </article>
34 {/each}
35 </ul>
36 <pre
37 class={['updating-text', posts.isFetching && 'on']}
38 style="font-weight:700">Background Updating...</pre>
39 {/if}
40 </div>
41</div>
42​
43<style>
44 article {
45 text-align: left;
46 }
47 a {
48 display: block;
49 font-size: 1.5rem;
50 margin-bottom: 1rem;
51 }
52​
53 .updating-text {
54 color: transparent;
55 transition: all 0.3s ease;
56 }
57 .updating-text.on {
58 color: green;
59 transition: none;
60 }
61</style>