@@ -2,39 +2,45 @@ import { useQueryClient, useMutation } from '@tanstack/react-query';
2
2
import Toast from '@/utils/notification' ;
3
3
import { BoardList } from '@/types/types' ;
4
4
import { deleteBoardLike } from '@/service/board' ;
5
+ import { queryKeys } from '@/constant/query-keys' ;
5
6
6
7
export default function useUnlikeBoard ( ) {
7
8
const queryClient = useQueryClient ( ) ;
8
9
const { mutate, isLoading } = useMutation ( {
9
10
mutationFn : deleteBoardLike ,
10
11
onMutate : async ( boardId ) => {
11
- await queryClient . cancelQueries ( { queryKey : [ 'boardList' ] } ) ;
12
- const previousBoards = queryClient . getQueryData < BoardList > ( [ 'boardList' ] ) ;
12
+ await queryClient . cancelQueries ( { queryKey : [ queryKeys . BOARD_LIST ] } ) ;
13
+ const previousBoards = queryClient . getQueryData < BoardList > ( [
14
+ queryKeys . BOARD_LIST ,
15
+ ] ) ;
13
16
14
17
// 낙관적 업데이트 시작
15
18
if ( previousBoards ) {
16
- queryClient . setQueryData < BoardList > ( [ 'boardList' ] , ( oldData ) => {
17
- if ( oldData ) {
18
- const updatedData = { ...oldData } ;
19
- const updatedBoard = updatedData . content ?. find (
20
- ( board ) => board . id === boardId ,
21
- ) ;
22
- if ( updatedBoard ) {
23
- updatedBoard . boardLiked = false ;
19
+ queryClient . setQueryData < BoardList > (
20
+ [ queryKeys . BOARD_LIST ] ,
21
+ ( oldData ) => {
22
+ if ( oldData ) {
23
+ const updatedData = { ...oldData } ;
24
+ const updatedBoard = updatedData . content ?. find (
25
+ ( board ) => board . id === boardId ,
26
+ ) ;
27
+ if ( updatedBoard ) {
28
+ updatedBoard . boardLiked = false ;
29
+ }
30
+ return updatedData ;
24
31
}
25
- return updatedData ;
26
- }
27
- return oldData ;
28
- } ) ;
32
+ return oldData ;
33
+ } ,
34
+ ) ;
29
35
}
30
36
return { previousBoards } ;
31
37
} ,
32
38
onSuccess : ( ) => {
33
- queryClient . invalidateQueries ( [ 'boardList' ] ) ;
39
+ queryClient . invalidateQueries ( [ queryKeys . BOARD_LIST ] ) ;
34
40
} ,
35
41
onError : ( _err , _ , context ) => {
36
42
// 캐시를 저장된 값으로 롤백
37
- queryClient . setQueryData ( [ 'boardList' ] , context ?. previousBoards ) ;
43
+ queryClient . setQueryData ( [ queryKeys . BOARD_LIST ] , context ?. previousBoards ) ;
38
44
Toast . error (
39
45
'좋아요를 취소하는데 실패했습니다. 잠시후 다시 시도해주세요.🥲' ,
40
46
) ;
0 commit comments