File size: 1,353 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<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>
      {#if $posts.isFetching}
        <div style="color:darkgreen; font-weight:700">
          Background Updating...
        </div>
      {/if}
    {/if}
  </div>
</div>

<style>
  article {
    text-align: left;
  }
  a {
    display: block;
    color: white;
    font-size: 1.5rem;
    margin-bottom: 1rem;
  }
</style>