@@ -4,17 +4,16 @@ import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
44import { Profile } from '../types/type' ;
55
66function useFetchProfile ( ) {
7-
8- const queryClient = useQueryClient ( )
7+ const queryClient = useQueryClient ( ) ;
98
109 const fetchProfile = async ( ) => {
1110 const res = await fetch ( `${ getApi } /me/profile` , {
1211 method : 'GET' ,
1312 credentials : 'include' ,
1413 } ) ;
1514 const json = await res . json ( ) ;
16-
17- return json . data
15+
16+ return json . data ;
1817 } ;
1918
2019 const patchNickName = useMutation ( {
@@ -23,25 +22,25 @@ function useFetchProfile() {
2322 method : 'PATCH' ,
2423 credentials : 'include' ,
2524 headers : { 'Content-Type' : 'application/json' } ,
26- body : JSON . stringify ( { nickname} )
27- } )
28- if ( ! res . ok ) throw new Error ( '닉네임 수정 실패' )
29- const json = await res . json ( )
30- return json . data
25+ body : JSON . stringify ( { nickname } ) ,
26+ } ) ;
27+ if ( ! res . ok ) throw new Error ( '닉네임 수정 실패' ) ;
28+ const json = await res . json ( ) ;
29+ return json . data ;
3130 } ,
3231
3332 onMutate : async ( nickname ) => {
3433 // 같은 키로 요청중인 fetch 중단
35- await queryClient . cancelQueries ( { queryKey : [ 'myProfile' ] } )
34+ await queryClient . cancelQueries ( { queryKey : [ 'myProfile' ] } ) ;
3635 // 캐시에 저장된 데이터를 즉시 가져오는 역할 실패시 prev로 롤백
37- const prev = queryClient . getQueryData ( [ 'myProfile' ] )
36+ const prev = queryClient . getQueryData ( [ 'myProfile' ] ) ;
3837 // 캐시 내용을 수정
39- queryClient . setQueryData ( [ 'myProfile' ] , ( old :Profile ) => ( { ...old , nickname } ) )
40- return { prev}
41- }
42- } )
43- const profile = useQuery ( { queryKey :[ 'myProfile' ] , queryFn :fetchProfile } )
38+ queryClient . setQueryData ( [ 'myProfile' ] , ( old : Profile ) => ( { ...old , nickname } ) ) ;
39+ return { prev } ;
40+ } ,
41+ } ) ;
42+ const profile = useQuery ( { queryKey : [ 'myProfile' ] , queryFn : fetchProfile } ) ;
4443
45- return { fetchProfile , profile, patchNickName}
44+ return { fetchProfile , profile, patchNickName } ;
4645}
4746export default useFetchProfile ;
0 commit comments