Skip to content
Snippets Groups Projects
Commit 43f6c4a3 authored by 지윤 장's avatar 지윤 장
Browse files

Fix: 데이터 없을 시 에러처리

parent 523156e6
No related branches found
No related tags found
No related merge requests found
Pipeline #10933 failed
...@@ -21,15 +21,15 @@ async function fetchWithOptions(url, options) { ...@@ -21,15 +21,15 @@ async function fetchWithOptions(url, options) {
} }
} }
// 개별 함수들을 export // 개별 함수들을
export const addExerciseRecord = async (record) => { const addExerciseRecord = async (record) => {
return await fetchWithOptions('/api/routine/records', { return await fetchWithOptions('/api/routine/records', {
method: 'POST', method: 'POST',
body: JSON.stringify(record), body: JSON.stringify(record),
}); });
}; };
export const getUserRoutines = async () => { const getUserRoutines = async () => {
try { try {
const response = await fetchWithOptions('/api/routine', { const response = await fetchWithOptions('/api/routine', {
method: 'GET', method: 'GET',
...@@ -49,7 +49,7 @@ export const getUserRoutines = async () => { ...@@ -49,7 +49,7 @@ export const getUserRoutines = async () => {
} }
}; };
export const getRoutineVideos = async (routineName) => { const getRoutineVideos = async (routineName) => {
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',
...@@ -67,14 +67,14 @@ export const getRoutineVideos = async (routineName) => { ...@@ -67,14 +67,14 @@ export const getRoutineVideos = async (routineName) => {
} }
}; };
export const createRoutine = async (routineName) => { const createRoutine = async (routineName) => {
return await fetchWithOptions('/api/routine', { return await fetchWithOptions('/api/routine', {
method: 'POST', method: 'POST',
body: JSON.stringify({ routine_name: routineName }), body: JSON.stringify({ routine_name: routineName }),
}); });
}; };
export const deleteRoutine = async (routineName) => { const deleteRoutine = async (routineName) => {
return await fetchWithOptions('/api/routine', { return await fetchWithOptions('/api/routine', {
method: 'DELETE', method: 'DELETE',
body: JSON.stringify({ routine_name: routineName }), body: JSON.stringify({ routine_name: routineName }),
...@@ -102,4 +102,4 @@ async function addRoutineVideo(data){ ...@@ -102,4 +102,4 @@ async function addRoutineVideo(data){
} }
export { addRoutineVideo, addExerciseRecord, getUserRoutines, getRoutineVideos, deleteRoutine }; export { addRoutineVideo, addExerciseRecord, getUserRoutines, getRoutineVideos, createRoutine, deleteRoutine };
import React, { useState, useEffect } from "react"; import React, { useState, useEffect } from "react";
import "./List.css"; import "./List.css";
import truncateText from './truncateText'; import truncateText from './truncateText';
import routineAPI from '../../api/routineAPI'; import {getUserRoutines, getRoutineVideos, deleteRoutine, createRoutine} from '../../api/routineAPI';
function List({ onRoutineSelect, isActive }) { function List({ onRoutineSelect, isActive }) {
const [routines, setRoutines] = useState([]); const [routines, setRoutines] = useState([]);
...@@ -43,12 +43,12 @@ function List({ onRoutineSelect, isActive }) { ...@@ -43,12 +43,12 @@ function List({ onRoutineSelect, isActive }) {
// 루틴 데이터와 비디오 데이터 함께 가져오기 // 루틴 데이터와 비디오 데이터 함께 가져오기
const fetchRoutines = async () => { const fetchRoutines = async () => {
try { try {
const routineData = await routineAPI.getUserRoutines(); const routineData = await getUserRoutines();
if (routineData) { if (routineData) {
// 각 루틴에 대해 비디오 정보 가져오기 // 각 루틴에 대해 비디오 정보 가져오기
const routinesWithVideos = await Promise.all( const routinesWithVideos = await Promise.all(
routineData.map(async (routine) => { routineData.map(async (routine) => {
const videos = await routineAPI.getRoutineVideos(routine.routine_name); const videos = await getRoutineVideos(routine.routine_name);
return { return {
...routine, ...routine,
exercises: videos.map(video => ({ exercises: videos.map(video => ({
...@@ -88,7 +88,7 @@ function List({ onRoutineSelect, isActive }) { ...@@ -88,7 +88,7 @@ function List({ onRoutineSelect, isActive }) {
const handledelete = async (name) => { const handledelete = async (name) => {
try { try {
await routineAPI.deleteRoutine(name); await deleteRoutine(name);
setIsModalOpen(false); setIsModalOpen(false);
await fetchRoutines(); // 삭제 후 목록 새로고침 await fetchRoutines(); // 삭제 후 목록 새로고침
} catch (err) { } catch (err) {
...@@ -101,7 +101,7 @@ function List({ onRoutineSelect, isActive }) { ...@@ -101,7 +101,7 @@ function List({ onRoutineSelect, isActive }) {
const routineName = prompt("새로운 루틴의 이름을 입력하세요:"); const routineName = prompt("새로운 루틴의 이름을 입력하세요:");
if (!routineName) return; if (!routineName) return;
await routineAPI.createRoutine(routineName); await createRoutine(routineName);
await fetchRoutines(); await fetchRoutines();
} catch (err) { } catch (err) {
console.error("루틴 추가 실패:", err); console.error("루틴 추가 실패:", err);
......
...@@ -30,14 +30,20 @@ function VideoDetails({video, routines}) { ...@@ -30,14 +30,20 @@ function VideoDetails({video, routines}) {
<span>{secToTime(video.video_length)}</span> <span>{secToTime(video.video_length)}</span>
<span><FontAwesomeIcon icon={faHeart} /> {video.video_likes}<br/></span> <span><FontAwesomeIcon icon={faHeart} /> {video.video_likes}<br/></span>
</span> </span>
<select name="routines" id="routines"> {routines ? (
{routines.map((item) => (
<> <>
<option value={item}>{item}</option> <select>
</> {routines.map((item, index) => (
<option key={index} value={item}>
{item}
</option>
))} ))}
</select> </select>
<button className="addbutton" onClick={addRoutine}>추가</button> <button className="addbutton" onClick={addRoutine}>추가</button>
</>
) : (
<label>추가할 루틴이 없습니다.</label>
)}
</div> </div>
</div> </div>
); );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment