-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
query.[id].vue
219 lines (196 loc) · 5.24 KB
/
query.[id].vue
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<script lang="ts">
// FIXME: should be able to import from vue-router or auto import
// import { defineQueryLoader } from 'unplugin-vue-router/data-loaders/vue-query'
import {
type TypesConfig,
type RouteRecordName,
} from 'unplugin-vue-router/types'
import { computed } from 'vue'
const a: RouteRecordName = '/articles'
// import type { RouteLocationNormalized, _RouteLocationNormalized } from 'vue-router/auto'
import type {
RouteLocationNormalized,
RouteLocationNormalized as _RouteLocationNormalized,
} from 'vue-router/auto'
declare const b: RouteLocationNormalized<'/[name]'>
declare const c: _RouteLocationNormalized<'/[name]'>
function test(fn: (to: _RouteLocationNormalized) => void): void
function test<Name extends RouteRecordName>(
name: Name,
fn: (to: _RouteLocationNormalized<Name>) => void
): void
function test<Name extends RouteRecordName>(...args: unknown[]) {}
test('/[name]', (to) => {
to.params.name
// @ts-expect-error
to.params.nope
})
test('/@[profileId]' as RouteRecordName, (to) => {
// @ts-expect-error: no all params have this
to.params.profileId
if (to.name === '/users/[id].edit') {
to.params.id
// @ts-expect-error: no param other
to.params.other
}
})
test((to) => {
// @ts-expect-error: not all params object have a name
to.params.name
// @ts-expect-error: no route named like that
if (to.name === '') {
}
if (to.name === '/articles/[id]') {
to.params.id
// @ts-expect-error: no param other
to.params.other
}
})
const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
// FIXME: export once doable
// NOTE: it's a bit different from the one in /[name].vue
// const useUserData = defineQueryLoader(
// '/users/[id]',
// async (route, { signal }) => {
// console.log('useUserData', route.fullPath)
// await delay(700)
// const user = {
// id: route.params.id,
// // @ts-expect-error: no param "name"!
// name: route.params.name || 'Edu',
// when: new Date().toUTCString(),
// }
// return user
// },
// {
// queryKey: ['user-id'],
// // queryKey: (route) => ['users', route.params.id],
// staleTime: 5000,
// lazy: false,
// }
// )
</script>
<script lang="ts" setup>
import { useMutation, useQuery } from '@tanstack/vue-query'
const route = useRoute('/users/[id]')
// const { data: user, isLoading, error } = useUserData()
const {
data: tqUser,
error: tqError,
status,
fetchStatus,
refetch,
} = useQuery({
async queryFn() {
console.log('[TQ]useUserData', route.fullPath, route.params)
await delay(500)
if (route.params.id === '0') {
throw new Error('no user')
}
const user = {
id: route.params.id,
// @ts-expect-error: no param "name"!
name: route.params.name || 'Edu',
when: new Date().toUTCString(),
}
return user
},
// FIXME: (to) => ['user-id', to.params.id]
queryKey: ['users', route.params.id],
staleTime: 5000,
retry: 0,
})
const {
mutateAsync: mutate,
data,
context,
status: mutationStatus,
error,
} = useMutation({
mutationFn: async (id: number) => {
console.log('mutate', id)
await delay(id)
// throw new Error('data ' + id)
return id
},
mutationKey: [
'mutate',
() => {
console.log('mutationKey')
return 205
},
],
// mutationKey: () => {
// console.log('mutationKey')
// return ['mutate']
// },
onMutate: (id) => {
console.log('onMutate', id)
// throw new Error('hello')
return { id }
},
onSuccess: (newData, vars, context) => {
console.log('onSuccess', newData, data.value, vars, context)
// throw new Error('onSuccess')
},
onError: (err) => {
console.log('onError', err)
// throw new Error('onError')
},
async onSettled(data, error) {
await new Promise((r) => setTimeout(r, 100))
console.log('onSettled', data, error)
throw new Error('onSettled')
},
retry: false,
})
function multipleMutate() {
mutate(100).then((d) => console.log('DATA 100', d))
mutate(200).then((d) => console.log('DATA 200', d))
mutate(50).then((d) => console.log('DATA 50', d))
}
</script>
<template>
<main>
<h1>defineQueryLoader()</h1>
<pre>route.params.id: {{ route.params.id }}</pre>
<RouterLink :to="{ params: { id: Number(route.params.id) - 1 } }"
>Previous</RouterLink
>
|
<RouterLink :to="{ params: { id: Number(route.params.id) + 1 } }"
>Next</RouterLink
>
|
<button
@click="
refetch().then((d) => {
console.log('Got ', d)
})
"
>
Refresh
</button>
<!-- <h2>Data Loaders</h2>
<pre v-if="isLoading">Loading...</pre>
<pre v-else-if="error">Error: {{ error }}</pre>
<pre v-else>{{ user }}</pre>
<hr />
-->
<h2>Query</h2>
<p>
<code>status: {{ status }}</code>
<br />
<code>fetchStatus: {{ fetchStatus }}</code>
</p>
<pre>Error: {{ tqError }}</pre>
<pre>User: {{ tqUser == null ? String(tqUser) : tqUser }}</pre>
<hr />
<h2>Mutations</h2>
<pre>Context: {{ context }}</pre>
<pre>status: {{ mutationStatus }}</pre>
<pre>error: {{ error }}</pre>
<button @click="mutate(123)">Mutate</button>
<button @click="multipleMutate()">Multi Mutate</button>
</main>
</template>