-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathPosts.svelte
66 lines (60 loc) · 1.44 KB
/
Posts.svelte
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
<script lang="ts">
import { useQueryClient, createQuery } from '@tanstack/svelte-query'
import { getPosts } from './data'
const client = useQueryClient()
let limit = 10
const posts = createQuery<
{ id: number; title: string; body: string }[],
Error
>(() => ({
queryKey: ['posts', limit],
queryFn: () => getPosts(limit),
}))
</script>
<div>
<div>
{#if posts.status === 'pending'}
<span>Loading...</span>
{:else if posts.status === 'error'}
<span>Error: {posts.error.message}</span>
{:else}
<ul>
{#each posts.data as post}
<article>
<a
href={`/${post.id}`}
style={// We can use the queryCache here to show bold links for
// ones that are cached
client.getQueryData(['post', post.id])
? 'font-weight: bold; color: indianred'
: 'cursor: pointer'}
>
{post.title}
</a>
</article>
{/each}
</ul>
<pre
class={['updating-text', posts.isFetching && 'on']}
style="font-weight:700">Background Updating...</pre>
{/if}
</div>
</div>
<style>
article {
text-align: left;
}
a {
display: block;
font-size: 1.5rem;
margin-bottom: 1rem;
}
.updating-text {
color: transparent;
transition: all 0.3s ease;
}
.updating-text.on {
color: green;
transition: none;
}
</style>