1- import { queryKeys } from '@src/constants' ;
1+ import { NoMemoListError , queryKeys } from '@src/constants' ;
22import { MemoSupabaseClient , MemoSupabaseResponse , MemoTableInsert } from '@src/types' ;
33import { insertMemo } from '@src/utils' ;
44import { useMutation , UseMutationOptions , useQueryClient } from '@tanstack/react-query' ;
@@ -19,9 +19,24 @@ export default function useMemoPostMutation({
1919 return useMutation < MemoSupabaseResponse , Error , PostMemoProps > ( {
2020 ...useMutationProps ,
2121 mutationFn : async ( postMemoProps : PostMemoProps ) => await insertMemo ( supabaseClient , postMemoProps ) ,
22- onSuccess : async ( ) => {
23- queryClient . invalidateQueries ( { queryKey : queryKeys . memoList ( ) } ) ;
22+ onSuccess : async ( { data : newData } ) => {
23+ await queryClient . cancelQueries ( { queryKey : queryKeys . memoList ( ) } ) ;
24+
25+ const previousMemoList = queryClient . getQueryData < MemoSupabaseResponse > ( queryKeys . memoList ( ) ) ;
26+
27+ if ( ! previousMemoList || ! newData ) throw new NoMemoListError ( ) ;
28+
29+ const { data : previousMemoListData } = previousMemoList ;
30+
31+ if ( ! previousMemoListData ) throw new NoMemoListError ( ) ;
32+
33+ const newMemoListData = previousMemoListData . concat ( newData ) ;
34+
35+ await queryClient . setQueryData ( queryKeys . memoList ( ) , { ...previousMemoList , data : newMemoListData } ) ;
36+
2437 handleSuccess ( ) ;
38+
39+ return { previousMemoList } ;
2540 } ,
2641 } ) ;
2742}
0 commit comments