Skip to content
Snippets Groups Projects
Commit 745494f2 authored by 문경호's avatar 문경호
Browse files

Fix: Fix Routine API

parent 43f6c4a3
No related branches found
No related tags found
No related merge requests found
Pipeline #10940 passed
...@@ -35,7 +35,7 @@ async function fetchWithOptions(url, options) { ...@@ -35,7 +35,7 @@ async function fetchWithOptions(url, options) {
method: 'GET', method: 'GET',
}); });
// 빈 루틴도 표시할 수 있도록 // 빈 루틴도 표시할 수 있도록 ��
return response.map(routine => ({ return response.map(routine => ({
_id: routine._id, _id: routine._id,
user_id: routine.user_id, user_id: routine.user_id,
...@@ -50,6 +50,12 @@ async function fetchWithOptions(url, options) { ...@@ -50,6 +50,12 @@ async function fetchWithOptions(url, options) {
}; };
const getRoutineVideos = async (routineName) => { const getRoutineVideos = async (routineName) => {
// routineName이 null이거나 undefined인 경우 빈 배열 반환
if (!routineName) {
console.log("루틴 이름이 제공되지 않았습니다.");
return [];
}
try { try {
const response = await fetchWithOptions(`/api/routine/videos?routine_name=${encodeURIComponent(routineName)}`, { const response = await fetchWithOptions(`/api/routine/videos?routine_name=${encodeURIComponent(routineName)}`, {
method: 'GET', method: 'GET',
...@@ -93,11 +99,13 @@ async function fetchWithOptions(url, options) { ...@@ -93,11 +99,13 @@ async function fetchWithOptions(url, options) {
body: JSON.stringify(data), body: JSON.stringify(data),
}); });
const responseData = await response.json(); const responseData = await response.json();
if(!responseData || !response.ok) if(!response.ok) {
throw new Error(responseData.message || '루틴을 불러오는데 실패했습니다.'); throw new Error(responseData.message || '루틴을 불러오는데 실패했습니다.');
else return data; }
return responseData; // 성공 시 응답 데이터 반환
} catch(err){ } catch(err){
console.log(err.message); console.error("루틴 추가 중 오류:", err.message);
throw err; // 에러를 상위로 전파
} }
} }
......
...@@ -43,27 +43,11 @@ function List({ onRoutineSelect, isActive }) { ...@@ -43,27 +43,11 @@ function List({ onRoutineSelect, isActive }) {
// 루틴 데이터와 비디오 데이터 함께 가져오기 // 루틴 데이터와 비디오 데이터 함께 가져오기
const fetchRoutines = async () => { const fetchRoutines = async () => {
try { try {
const routineData = await getUserRoutines(); const data = await getUserRoutines();
if (routineData) { setRoutines(data || []); // 데이터가 없는 경우 빈 배열 설정
// 각 루틴에 대해 비디오 정보 가져오기 } catch (error) {
const routinesWithVideos = await Promise.all( console.error("루틴 목록 가져오기 실패:", error);
routineData.map(async (routine) => { setRoutines([]); // 에러 발생 시 빈 배열로 설정
const videos = await getRoutineVideos(routine.routine_name);
return {
...routine,
exercises: videos.map(video => ({
title: video.video_title,
duration: video.video_time,
link: video.video_url,
thumbnail: video.video_thumbnail
}))
};
})
);
setRoutines(routinesWithVideos);
}
} catch (err) {
console.error("루틴 데이터 가져오기 실패:", err);
} }
}; };
...@@ -71,14 +55,21 @@ function List({ onRoutineSelect, isActive }) { ...@@ -71,14 +55,21 @@ function List({ onRoutineSelect, isActive }) {
fetchRoutines(); fetchRoutines();
}, []); }, []);
const handleRoutineClick = (routine) => { const handleRoutineClick = async (routine) => {
if (!modify) { if (!modify && routine) {
try {
const videos = await getRoutineVideos(routine.routine_name);
const formattedRoutine = { const formattedRoutine = {
name: routine.routine_name, name: routine.routine_name,
exercises: [] exercises: videos
}; };
setSelectedRoutine(formattedRoutine); setSelectedRoutine(formattedRoutine);
onRoutineSelect(formattedRoutine); onRoutineSelect(formattedRoutine);
} catch (error) {
console.error("루틴 비디오 가져오기 실패:", error);
// 에러 발생 시 사용자에게 알림
alert("루틴 정보를 가져오는데 실패했습니다.");
}
} }
}; };
...@@ -120,9 +111,13 @@ function List({ onRoutineSelect, isActive }) { ...@@ -120,9 +111,13 @@ function List({ onRoutineSelect, isActive }) {
<div id="list-content"> <div id="list-content">
<ul> <ul>
{routines && routines.length > 0 ? ( {routines && routines.length > 0 ? (
routines.map((routine) => ( routines.map((routine, index) => (
<li key={routine._id} onClick={() => handleRoutineClick(routine)}> <li
<span>{truncateText(routine.routine_name, 10)}</span> key={index}
onClick={() => handleRoutineClick(routine)}
className={selectedRoutine?.name === routine.routine_name ? 'pick' : ''}
>
<span>{truncateText(routine.routine_name || '')}</span>
{modify && ( {modify && (
<button onClick={(e) => { <button onClick={(e) => {
e.stopPropagation(); e.stopPropagation();
......
...@@ -14,8 +14,12 @@ function VideoDetails({video, routines}) { ...@@ -14,8 +14,12 @@ function VideoDetails({video, routines}) {
} }
try{ try{
const response = await addRoutineVideo(data); const response = await addRoutineVideo(data);
if (response) {
alert("루틴에 성공적으로 추가되었습니다.");
}
} catch(err) { } catch(err) {
alert(err.message); console.error("루틴 추가 실패:", err);
alert(err.message || "루틴 추가에 실패했습니다.");
} }
} }
......
import React from 'react'; import React from 'react';
import ReactDOM from 'react-dom'; import ReactDOM from 'react-dom/client'; // React 18 API 사용
//import './index.css';
import App from './App'; import App from './App';
import reportWebVitals from './reportWebVitals'; import reportWebVitals from './reportWebVitals';
ReactDOM.render( const root = ReactDOM.createRoot(document.getElementById('root')); // createRoot로 변경
<App />, root.render(
document.getElementById('root') <React.StrictMode>
<App />
</React.StrictMode>
); );
// If you want to start measuring performance in your app, pass a function // If you want to start measuring performance in your app, pass a function
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment