@@ -4,13 +4,19 @@ import MusicCard from '@/components/MusicCard';
44import Comment from '@/pages/post/components/Comment' ;
55import { useEffect , useState } from 'react' ;
66import { useSheetStore } from '@/store/sheetStore' ;
7- import { getEmotionRecordById , postEmotionRecord , putEmotionRecord } from '@/apis/emotionRecord' ;
7+ import {
8+ getEmotionRecordById ,
9+ getSpotifyVideoId ,
10+ postEmotionRecord ,
11+ putEmotionRecord ,
12+ } from '@/apis/emotionRecord' ;
813import { useModalStore } from '@/store/modalStore' ;
914import { useNavigate , useParams } from 'react-router' ;
1015import { useMusicCardStore } from '@/store/MusicCardStore' ;
1116import SpinLoading from '@/components/loading/SpinLoading' ;
1217import Complete from '@/components/loading/Complete' ;
1318import ErrorShake from '@/components/loading/ErrorShake' ;
19+ import { searchYoutubeVideo } from '@/apis/youtube' ;
1420
1521export default function Post ( ) {
1622 const navigate = useNavigate ( ) ;
@@ -111,15 +117,43 @@ export default function Post() {
111117 } ) ;
112118 } ;
113119
120+ // spotifyId로 videoId 조회
121+ const fetchSpotifyVideoId = async ( spotifyId : string , artist : string , title : string ) => {
122+ try {
123+ const data = await getSpotifyVideoId ( spotifyId ) ; // 서버에 videoId 조회
124+ console . log ( 'videoId 조회 결과:' , data ) ;
125+
126+ // 서버에 videoId 가 있으면
127+ if ( data . code === 200 && data . data ) return data . videoId ;
128+ // 서버에 videoId 가 없으면
129+ else {
130+ // youtube 검색
131+ const videoId = await searchYoutubeVideo ( `${ artist } - ${ title } lyrics` ) ;
132+ console . log ( '유튜브 videoId:' , videoId ) ;
133+ return videoId ;
134+ }
135+ } catch ( error ) {
136+ console . error ( error ) ;
137+ }
138+ } ;
139+
114140 // 기록 완료
115141 const onCompletePost = async ( ) => {
116142 if ( ! isCompletePost ) return ;
117143
144+ // 음악 선택 시 videoId 조회
145+ const videoId = await fetchSpotifyVideoId (
146+ selectedPostMusic ?. spotifyId ,
147+ selectedPostMusic ?. artistName ,
148+ selectedPostMusic ?. songTitle ,
149+ ) ;
150+
118151 try {
119152 setIsLoading ( true ) ;
120153
121154 const requestData = {
122155 spotifyId : selectedPostMusic ?. spotifyId ,
156+ videoId : videoId ,
123157 title : selectedPostMusic ?. songTitle ,
124158 artist : selectedPostMusic ?. artistName ,
125159 albumImage : selectedPostMusic ?. albumImage ,
@@ -136,6 +170,7 @@ export default function Post() {
136170 data = await postEmotionRecord ( requestData ) ;
137171 }
138172 console . log ( isEditMode ? '수정 완료' : '기록 완료:' , data ) ;
173+ console . log ( '요청 데이터:' , requestData ) ;
139174
140175 setIsComplete ( true ) ;
141176 handlePostSuccessModal ( ) ;
0 commit comments