1
1
<script setup lang="ts">
2
+ import { todosQuery } from ' ~/queries/todos'
3
+
2
4
definePageMeta ({
3
5
middleware: ' auth'
4
6
})
@@ -8,14 +10,7 @@ const toast = useToast()
8
10
const { user } = useUserSession ()
9
11
const queryCache = useQueryCache ()
10
12
11
- const { data : todos } = useQuery ({
12
- key: [' todos' ],
13
- // using $fetch directly doesn't avoid the round trip to the server
14
- // when doing SSR
15
- // https://github.com/nuxt/nuxt/issues/24813
16
- // NOTE: the cast sometimes avoids an "Excessive depth check" TS error
17
- query : ({ signal }) => useRequestFetch ()(' /api/todos' , { signal }) as Promise <Todo []>
18
- })
13
+ const { data : todos } = useQuery (todosQuery )
19
14
20
15
const { mutate : addTodo } = useMutation ({
21
16
mutation : (title : string ) => {
@@ -33,7 +28,7 @@ const { mutate: addTodo } = useMutation({
33
28
onMutate(title ) {
34
29
// let the user enter new todos right away!
35
30
newTodo .value = ' '
36
- const oldTodos = queryCache .getQueryData < Todo []>([ ' todos ' ] ) || []
31
+ const oldTodos = queryCache .getQueryData ( todosQuery . key ) || []
37
32
const newTodoItem = {
38
33
title ,
39
34
completed: 0 ,
@@ -45,13 +40,10 @@ const { mutate: addTodo } = useMutation({
45
40
// we use newTodos to check for the cache consistency
46
41
// a better way would be to save the entry time
47
42
// const when = queryCache.getEntries({ key: ['todos'], exact: true }).at(0)?.when
48
- const newTodos = [
49
- ... oldTodos ,
50
- newTodoItem
51
- ]
52
- queryCache .setQueryData ([' todos' ], newTodos )
43
+ const newTodos = [... oldTodos , newTodoItem ]
44
+ queryCache .setQueryData (todosQuery .key , newTodos )
53
45
54
- queryCache .cancelQueries ({ key: [ ' todos ' ] , exact: true })
46
+ queryCache .cancelQueries ({ key: todosQuery . key , exact: true })
55
47
56
48
return { oldTodos , newTodos , newTodoItem }
57
49
},
@@ -60,24 +52,30 @@ const { mutate: addTodo } = useMutation({
60
52
// update the todo with the information from the server
61
53
// since we are invalidating queries, this allows us to progressively
62
54
// update the todo list even if the user is adding a lot very quickly
63
- const todoList = queryCache .getQueryData < Todo []>([ ' todos ' ] ) || []
55
+ const todoList = queryCache .getQueryData ( todosQuery . key ) || []
64
56
const todoIndex = todoList .findIndex (t => t .id === newTodoItem .id )
65
57
if (todoIndex >= 0 ) {
66
- queryCache .setQueryData ([' todos' ], todoList .toSpliced (todoIndex , 1 , todo ))
58
+ queryCache .setQueryData (
59
+ todosQuery .key ,
60
+ todoList .toSpliced (todoIndex , 1 , todo )
61
+ )
67
62
}
68
63
},
69
64
70
65
onSettled() {
71
66
// always refetch the todos after a mutation
72
- queryCache .invalidateQueries ({ key: [ ' todos ' ] })
67
+ queryCache .invalidateQueries ({ key: todosQuery . key })
73
68
},
74
69
75
70
onError(err , _title , { oldTodos , newTodos }) {
76
71
// oldTodos can be undefined if onMutate errors
77
72
// we also want to check if the oldTodos are still in the cache
78
73
// because the cache could have been updated by another query
79
- if (newTodos != null && newTodos === queryCache .getQueryData ([' todos' ])) {
80
- queryCache .setQueryData ([' todos' ], oldTodos )
74
+ if (
75
+ newTodos != null
76
+ && newTodos === queryCache .getQueryData (todosQuery .key )
77
+ ) {
78
+ queryCache .setQueryData (todosQuery .key , oldTodos )
81
79
}
82
80
83
81
if (isNuxtZodError (err )) {
@@ -104,31 +102,34 @@ const { mutate: toggleTodo } = useMutation({
104
102
}),
105
103
106
104
onMutate(todo ) {
107
- const oldTodos = queryCache .getQueryData < Todo []>([ ' todos ' ] ) || []
105
+ const oldTodos = queryCache .getQueryData ( todosQuery . key ) || []
108
106
const todoIndex = oldTodos .findIndex (t => t .id === todo .id )
109
107
let newTodos = oldTodos
110
108
if (todoIndex >= 0 ) {
111
109
newTodos = oldTodos .toSpliced (todoIndex , 1 , {
112
110
... todo ,
113
111
completed: Number (! todo .completed )
114
112
})
115
- queryCache .setQueryData ([ ' todos ' ] , newTodos )
113
+ queryCache .setQueryData (todosQuery . key , newTodos )
116
114
}
117
115
118
- queryCache .cancelQueries ({ key: [ ' todos ' ] , exact: true })
116
+ queryCache .cancelQueries ({ key: todosQuery . key , exact: true })
119
117
120
118
return { oldTodos , newTodos }
121
119
},
122
120
123
121
onSettled() {
124
122
// always refetch the todos after a mutation
125
- queryCache .invalidateQueries ({ key: [ ' todos ' ] , exact: true })
123
+ queryCache .invalidateQueries ({ key: todosQuery . key , exact: true })
126
124
},
127
125
128
126
onError(err , todo , { oldTodos , newTodos }) {
129
127
// oldTodos can be undefined if onMutate errors
130
- if (newTodos != null && newTodos === queryCache .getQueryData ([' todos' ])) {
131
- queryCache .setQueryData ([' todos' ], oldTodos )
128
+ if (
129
+ newTodos != null
130
+ && newTodos === queryCache .getQueryData (todosQuery .key )
131
+ ) {
132
+ queryCache .setQueryData (todosQuery .key , oldTodos )
132
133
}
133
134
134
135
console .error (err )
@@ -140,28 +141,28 @@ const { mutate: deleteTodo } = useMutation({
140
141
mutation : (todo : Todo ) => $fetch (` /api/todos/${todo .id } ` , { method: ' DELETE' }),
141
142
142
143
onMutate(todo ) {
143
- const oldTodos = queryCache .getQueryData < Todo []>([ ' todos ' ] ) || []
144
+ const oldTodos = queryCache .getQueryData ( todosQuery . key ) || []
144
145
const todoIndex = oldTodos .findIndex (t => t .id === todo .id )
145
146
let newTodos = oldTodos
146
147
if (todoIndex >= 0 ) {
147
148
newTodos = oldTodos .toSpliced (todoIndex , 1 )
148
- queryCache .setQueryData ([ ' todos ' ] , newTodos )
149
+ queryCache .setQueryData (todosQuery . key , newTodos )
149
150
}
150
151
151
- queryCache .cancelQueries ({ key: [ ' todos ' ] , exact: true })
152
+ queryCache .cancelQueries ({ key: todosQuery . key , exact: true })
152
153
153
154
return { oldTodos , newTodos }
154
155
},
155
156
156
157
onSettled() {
157
158
// always refetch the todos after a mutation
158
- queryCache .invalidateQueries ({ key: [ ' todos ' ] , exact: true })
159
+ queryCache .invalidateQueries ({ key: todosQuery . key , exact: true })
159
160
},
160
161
161
162
onError(err , todo , { oldTodos , newTodos }) {
162
163
// oldTodos can be undefined if onMutate errors
163
- if (newTodos != null && newTodos === queryCache .getQueryData ([ ' todos ' ] )) {
164
- queryCache .setQueryData ([ ' todos ' ] , oldTodos )
164
+ if (newTodos != null && newTodos === queryCache .getQueryData (todosQuery . key )) {
165
+ queryCache .setQueryData (todosQuery . key , oldTodos )
165
166
}
166
167
167
168
console .error (err )
0 commit comments