Skip to content

Commit 6e73278

Browse files
feat: More tests and examples!
1 parent cc54952 commit 6e73278

File tree

29 files changed

+1934
-221
lines changed

29 files changed

+1934
-221
lines changed

examples/svelte/auto-refetching/src/routes/+page.svelte

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
1111
const client = useQueryClient()
1212
13-
const endpoint = 'http://localhost:5173/api/data'
13+
const endpoint = '/api/data'
1414
1515
const todos = createQuery<{ items: string[] }>(() => ({
1616
queryKey: ['refetch'],
@@ -21,8 +21,13 @@
2121
2222
const addMutation = createMutation(() => ({
2323
mutationFn: (value: string) =>
24-
fetch(`${endpoint}?add=${value}`).then((r) => r.json()),
25-
onSuccess: () => client.invalidateQueries({ queryKey: ['refetch'] }),
24+
fetch(`${endpoint}?add=${encodeURIComponent(value)}`).then((r) =>
25+
r.json(),
26+
),
27+
onSuccess: async () => {
28+
await todos.refetch()
29+
value = ''
30+
},
2631
}))
2732
2833
const clearMutation = createMutation(() => ({
@@ -31,7 +36,7 @@
3136
}))
3237
</script>
3338

34-
<h1>Auto Refetch with stale-time set to 1s</h1>
39+
<h1>Auto Refetch with stale-time set to {(intervalMs / 1000).toFixed(2)}s</h1>
3540

3641
<p>
3742
This example is best experienced on your own machine, where you can open
@@ -86,14 +91,22 @@
8691
<button onclick={() => clearMutation.mutate(undefined)}> Clear All </button>
8792
</div>
8893
{/if}
89-
{#if todos.isFetching}
90-
<div style="color:darkgreen; font-weight:700">
91-
'Background Updating...' : ' '
92-
</div>
93-
{/if}
94+
95+
<pre
96+
class={['updating-text', todos.isFetching && 'on']}
97+
style="font-weight:700">Background Updating...</pre>
9498

9599
<style>
96100
li {
97101
text-align: left;
98102
}
103+
104+
.updating-text {
105+
color: transparent;
106+
transition: all 0.3s ease;
107+
}
108+
.updating-text.on {
109+
color: green;
110+
transition: none;
111+
}
99112
</style>

examples/svelte/basic/src/lib/Posts.svelte

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,9 @@
3838
</article>
3939
{/each}
4040
</ul>
41-
{#if posts.isFetching}
42-
<div style="color:darkgreen; font-weight:700">
43-
Background Updating...
44-
</div>
45-
{/if}
41+
<pre
42+
class={['updating-text', posts.isFetching && 'on']}
43+
style="font-weight:700">Background Updating...</pre>
4644
{/if}
4745
</div>
4846
</div>
@@ -53,8 +51,16 @@
5351
}
5452
a {
5553
display: block;
56-
color: white;
5754
font-size: 1.5rem;
5855
margin-bottom: 1rem;
5956
}
57+
58+
.updating-text {
59+
color: transparent;
60+
transition: all 0.3s ease;
61+
}
62+
.updating-text.on {
63+
color: green;
64+
transition: none;
65+
}
6066
</style>

examples/svelte/load-more-infinite-scroll/src/lib/LoadMore.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,6 @@
6060
.card {
6161
background-color: #111;
6262
margin-bottom: 1rem;
63+
color: rgba(255, 255, 255, 0.87);
6364
}
6465
</style>

examples/svelte/optimistic-updates/src/routes/+page.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
2121
const client = useQueryClient()
2222
23-
const endpoint = 'http://localhost:5173/api/data'
23+
const endpoint = '/api/data'
2424
2525
const fetchTodos = async (): Promise<Todos> =>
2626
await fetch(endpoint).then((r) => r.json())

examples/svelte/optimistic-updates/src/routes/api/data/+server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ type Todo = {
66
text: string
77
}
88

9-
const items: Todo[] = []
9+
const items: Array<Todo> = []
1010

1111
/** @type {import('./$types').RequestHandler} */
1212
export const GET: RequestHandler = async (req) => {

examples/svelte/playground/src/routes/AddTodo.svelte

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
let name = $state('')
1515
1616
const postTodo = async ({ name, notes }: Omit<Todo, 'id'>) => {
17-
console.info('postTodo', { name, notes })
1817
return new Promise((resolve, reject) => {
1918
setTimeout(
2019
() => {
@@ -31,7 +30,7 @@
3130
}
3231
const todo = { name, notes, id: id.value }
3332
id.value = id.value + 1
34-
list.value = [...list.value, todo]
33+
list.value.push(todo)
3534
resolve(todo)
3635
},
3736
queryTimeMin.value +

examples/svelte/playground/src/routes/App.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
<button
2828
onclick={() => {
29-
views.value = [...views.value, '']
29+
views.value.push('')
3030
}}
3131
>
3232
Add Filter List

examples/svelte/ssr/src/lib/Posts.svelte

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,9 @@
3838
</article>
3939
{/each}
4040
</ul>
41-
{#if posts.isFetching}
42-
<div style="color:darkgreen; font-weight:700">
43-
Background Updating...
44-
</div>
45-
{/if}
41+
<pre
42+
class={['updating-text', posts.isFetching && 'on']}
43+
style="font-weight:700">Background Updating...</pre>
4644
{/if}
4745
</div>
4846
</div>
@@ -53,8 +51,15 @@
5351
}
5452
a {
5553
display: block;
56-
color: white;
5754
font-size: 1.5rem;
5855
margin-bottom: 1rem;
5956
}
57+
.updating-text {
58+
color: transparent;
59+
transition: all 0.3s ease;
60+
}
61+
.updating-text.on {
62+
color: green;
63+
transition: none;
64+
}
6065
</style>

examples/svelte/ssr/src/routes/+layout.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { browser } from '$app/environment'
21
import { QueryClient } from '@tanstack/svelte-query'
32
import type { LayoutLoad } from './$types'
3+
import { browser } from '$app/environment'
44

55
export const load: LayoutLoad = () => {
66
const queryClient = new QueryClient({

examples/svelte/ssr/src/routes/+page.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { api } from '$lib/api'
21
import type { PageLoad } from './$types'
2+
import { api } from '$lib/api'
33

44
export const load: PageLoad = async ({ parent, fetch }) => {
55
const { queryClient } = await parent()

0 commit comments

Comments
 (0)