@@ -51,55 +51,46 @@ export const useEditTodoList = () => {
51
51
return useMutation ( {
52
52
mutationFn : api . editTodoList ,
53
53
onMutate : async variables => {
54
- const snapshot = queryClient . getQueryData < ICategory [ ] > ( [ 'categories' ] ) ;
54
+ const snapshot = queryClient . getQueryData < ITodoList > ( [
55
+ 'todo-list' ,
56
+ variables . id ,
57
+ ] ) ;
55
58
56
59
// Optimistically update to the new value
57
- queryClient . setQueryData < ICategory [ ] > (
58
- [ 'categories' ] ,
59
- produce ( draft => {
60
- const category = draft ?. find (
61
- category => category . id === variables . categoryId
62
- ) ;
63
- let todoList = category ?. todoLists . find (
64
- todo => todo . id === variables . id
65
- ) ;
66
-
67
- if ( todoList ) {
68
- todoList = { ...todoList , ...variables } ;
69
- }
70
- } )
71
- ) ;
60
+ queryClient . setQueryData < ITodoList > ( [ 'todo-list' , variables . id ] , prev => {
61
+ if ( ! prev ) return ;
62
+ return {
63
+ ...prev ,
64
+ ...variables ,
65
+ } ;
66
+ } ) ;
72
67
73
68
router . replace ( '/todo-list/' + variables . id ) ;
74
69
75
70
// Return a context object with the snapshot value
76
71
return { snapshot } ;
77
72
} ,
73
+ onSuccess : ( ) => {
74
+ queryClient . refetchQueries ( {
75
+ exact : true ,
76
+ queryKey : [ 'categories' ] ,
77
+ } ) ;
78
+ } ,
78
79
} ) ;
79
80
} ;
80
81
81
- //Should converted to Todo List with immer
82
82
export const useDeleteTodoList = ( ) => {
83
83
const queryClient = useQueryClient ( ) ;
84
84
const router = useRouter ( ) ;
85
85
86
86
return useMutation ( {
87
87
mutationFn : api . deleteTodoList ,
88
- onMutate : async ( { id } ) => {
89
- // Snapshot the previous value
90
- const snapshot = queryClient . getQueryData < ICategory [ ] > ( [ 'categories' ] ) ;
91
-
92
- // Optimistically update to the new value
93
- queryClient . setQueryData < ICategory [ ] > ( [ 'categories' ] , old => old ) ;
94
-
95
- // Redirect to landing page
96
- router . replace ( '/' ) ;
97
-
98
- // Return a context object with the snapshot value
99
- return { snapshot } ;
100
- } ,
101
- onError : ( _error , _variables , context ) => {
102
- queryClient . setQueryData ( [ 'categories' ] , context ?. snapshot ) ;
88
+ onSuccess : ( ) => {
89
+ router . push ( '/' ) ;
90
+ queryClient . refetchQueries ( {
91
+ exact : true ,
92
+ queryKey : [ 'categories' ] ,
93
+ } ) ;
103
94
} ,
104
95
} ) ;
105
96
} ;
0 commit comments