Skip to content

Commit da58254

Browse files
authored
Merge pull request #211 from prgrms-web-devcourse-final-project/feat/204-edit-profile-videoid
[feat] 프로필 수정 비디오아이디 추가
2 parents 17dfb87 + 91558bd commit da58254

File tree

3 files changed

+40
-28
lines changed

3 files changed

+40
-28
lines changed

src/pages/editprofile/components/ProfileEditForm.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import NicknameInput from '@/pages/signup/components/NicknameInput';
88
import { useModalStore } from '@/store/modalStore';
99
import { useMusicCardStore } from '@/store/MusicCardStore';
1010
import { useSheetStore } from '@/store/sheetStore';
11+
import { fetchSpotifyVideoId } from '@/utils/fetchSpotifyVideoId';
1112
import _ from 'lodash';
1213
import { useEffect, useRef, useState } from 'react';
1314
import { useNavigate } from 'react-router';
@@ -20,6 +21,7 @@ type ProfileFormType = {
2021
title: string;
2122
artist: string;
2223
albumImage: string;
24+
videoId: string;
2325
};
2426

2527
// 노래, 닉네임 변경폼
@@ -45,6 +47,7 @@ function ProfileEditForm() {
4547

4648
const handleProfileSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
4749
e.preventDefault();
50+
4851
try {
4952
setIsLoading(true);
5053
// 업데이트 된 것만 전송
@@ -56,10 +59,19 @@ function ProfileEditForm() {
5659
if (selectedProfileMusic !== prevProfileMusic.current) {
5760
// 프로필 뮤직이 선택되어있을 경우
5861
if (selectedProfileMusic) {
62+
// videoId 조회
63+
const videoId = await fetchSpotifyVideoId(
64+
selectedProfileMusic?.spotifyId,
65+
selectedProfileMusic?.artist,
66+
selectedProfileMusic?.title,
67+
);
68+
console.log('videoId:', videoId);
69+
5970
updatedData.spotifyId = selectedProfileMusic.spotifyId;
6071
updatedData.title = selectedProfileMusic.title;
6172
updatedData.artist = selectedProfileMusic.artist;
6273
updatedData.albumImage = selectedProfileMusic.album;
74+
updatedData.videoId = videoId;
6375
}
6476
// 프로필 뮤직이 선택되지않아서 삭제하는 로직
6577
else {

src/pages/post/Post.tsx

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,14 @@ import MusicCard from '@/components/MusicCard';
44
import Comment from '@/pages/post/components/Comment';
55
import { useEffect, useState } from 'react';
66
import { useSheetStore } from '@/store/sheetStore';
7-
import {
8-
getEmotionRecordById,
9-
getSpotifyVideoId,
10-
postEmotionRecord,
11-
putEmotionRecord,
12-
} from '@/apis/emotionRecord';
7+
import { getEmotionRecordById, postEmotionRecord, putEmotionRecord } from '@/apis/emotionRecord';
138
import { useModalStore } from '@/store/modalStore';
149
import { useNavigate, useParams } from 'react-router';
1510
import { useMusicCardStore } from '@/store/MusicCardStore';
1611
import SpinLoading from '@/components/loading/SpinLoading';
1712
import Complete from '@/components/loading/Complete';
1813
import ErrorShake from '@/components/loading/ErrorShake';
19-
import { searchYoutubeVideo } from '@/apis/youtube';
14+
import { fetchSpotifyVideoId } from '@/utils/fetchSpotifyVideoId';
2015

2116
export default function Post() {
2217
const navigate = useNavigate();
@@ -117,31 +112,11 @@ export default function Post() {
117112
});
118113
};
119114

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-
140115
// 기록 완료
141116
const onCompletePost = async () => {
142117
if (!isCompletePost) return;
143118

144-
// 음악 선택 시 videoId 조회
119+
// videoId 조회
145120
const videoId = await fetchSpotifyVideoId(
146121
selectedPostMusic?.spotifyId,
147122
selectedPostMusic?.artistName,

src/utils/fetchSpotifyVideoId.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { getSpotifyVideoId } from '@/apis/emotionRecord';
2+
import { searchYoutubeVideo } from '@/apis/youtube';
3+
4+
export const fetchSpotifyVideoId = async (spotifyId: string, artist: string, title: string) => {
5+
try {
6+
const data = await getSpotifyVideoId(spotifyId); // 서버에 videoId 조회
7+
console.log('videoId 조회 결과:', data);
8+
9+
// 서버에 videoId 가 있으면
10+
if (data.code === 200 && data.data) {
11+
console.log('서버 videoId 존재함!:', data.data);
12+
return data.data;
13+
}
14+
// 서버에 videoId 가 없으면
15+
else {
16+
// youtube 검색
17+
const newVideoId = await searchYoutubeVideo(`${artist} - ${title} lyrics`);
18+
console.log('유튜브 videoId:', newVideoId);
19+
return newVideoId;
20+
}
21+
} catch (error) {
22+
console.error(error);
23+
return null;
24+
}
25+
};

0 commit comments

Comments
 (0)