1- import { deleteEmotionRecord , getUserEmotionRecords } from '@/apis/emotionRecord' ;
1+ import { getUserEmotionRecords } from '@/apis/emotionRecord' ;
22import { getMyProfile , getUserProfile } from '@/apis/user' ;
33import CardDetailModal from '@/components/modalSheet/CardDetailModal' ;
44import MusicCard from '@/components/MusicCard' ;
55import { useModalStore } from '@/store/modalStore' ;
66import { useSheetStore } from '@/store/sheetStore' ;
7- import { useInfiniteQuery , useMutation , useQuery , useQueryClient } from '@tanstack/react-query' ;
7+ import { useInfiniteQuery , useQuery } from '@tanstack/react-query' ;
88import { useEffect , useState } from 'react' ;
99import { useNavigate , useParams } from 'react-router' ;
1010import { useInView } from 'react-intersection-observer' ;
1111import EmotionRecordCardList from '@/pages/userprofile/components/EmotionRecordCardList' ;
1212import { useUserStore } from '@/store/userStore' ;
1313import Loading from '@/components/loading/Loading' ;
14+ import { useDeleteEmotionRecord } from '@/hooks/useDeleteEmotionRecord' ;
1415
1516// 마이페이지 / 유저페이지 동시에 사용
1617function UserProfile ( { isMyPage } : { isMyPage : boolean } ) {
@@ -19,7 +20,6 @@ function UserProfile({ isMyPage }: { isMyPage: boolean }) {
1920 const { userId } = useParams ( ) ; // 유저페이지 경우
2021 const { openSheet, closeSheet } = useSheetStore ( ) ; // 시트
2122 const { openModal, closeModal } = useModalStore ( ) ; // 모달
22- const queryClient = useQueryClient ( ) ; // useMutation 사용
2323 const { setUserData } = useUserStore ( ) ; // 유저 정보 전역 저장
2424
2525 const { ref, inView } = useInView ( ) ;
@@ -37,6 +37,7 @@ function UserProfile({ isMyPage }: { isMyPage: boolean }) {
3737 }
3838 } , [ userData , setUserData ] ) ;
3939
40+ // 감정 기록 데이터 불러오기
4041 const {
4142 data : emotionRecords ,
4243 isLoading : isEmotionLoading ,
@@ -66,53 +67,9 @@ function UserProfile({ isMyPage }: { isMyPage: boolean }) {
6667 setSelectedRecordId ( recordId ) ;
6768 openSheet ( 'isCardSheetOpen' ) ; // 모달 열기
6869 } ;
69- // 감정 기록 삭제
70- const { mutate } = useMutation ( {
71- mutationFn : ( recordId : number ) => deleteEmotionRecord ( recordId ) ,
72- onMutate : async ( recordId ) => {
73- // 낙관적 업데이트 전에 사용자 목록 쿼리를 취소해 잠재적인 충돌 방지!
74- await queryClient . cancelQueries ( {
75- queryKey : isMyPage
76- ? [ 'emotionRecords' , userData ?. data ?. loginId ]
77- : [ 'emotionRecords' , userId ] ,
78- } ) ;
79- // 캐시된 데이터(사용자 목록) 가져오기!
80- const previousRecords = queryClient . getQueryData < EmotionRecord [ ] > ( [
81- 'emotionRecords' ,
82- isMyPage ? userData ?. data ?. loginId : userId ,
83- ] ) ;
8470
85- if ( previousRecords ) {
86- queryClient . setQueryData (
87- [ 'emotionRecords' , isMyPage ? userData ?. data ?. loginId : userId ] ,
88- ( oldData : any ) => ( {
89- ...oldData ,
90- data : {
91- ...oldData . data ,
92- records : oldData . data . records . filter ( ( r : EmotionRecord ) => r . recordId !== recordId ) ,
93- } ,
94- } ) ,
95- ) ;
96- }
97- // 각 콜백의 context로 전달할 데이터 반환!
98- return { previousRecords } ;
99- } ,
100- onError : ( _ , __ , context ) => {
101- if ( context ?. previousRecords ) {
102- queryClient . setQueryData (
103- [ 'emotionRecords' , isMyPage ? userData ?. data ?. loginId : userId ] ,
104- context . previousRecords ,
105- ) ;
106- }
107- } ,
108- onSettled : ( ) => {
109- queryClient . invalidateQueries ( {
110- queryKey : isMyPage
111- ? [ 'emotionRecords' , userData ?. data ?. loginId ]
112- : [ 'emotionRecords' , userId ] ,
113- } ) ;
114- } ,
115- } ) ;
71+ // 감정 기록 삭제
72+ const { mutate } = useDeleteEmotionRecord ( isMyPage ? userData ?. data ?. loginId : userId ) ;
11673
11774 // 삭제모달 띄우기
11875 const handleDeleteModal = ( ) => {
0 commit comments