diff --git a/front/src/api/routineAPI.js b/front/src/api/routineAPI.js index aebbcf8ec1efcfad6de006b4c55f13b01d88b3d2..ffbf4fbbae8541b3fda511d576df4533f8fd9e7f 100644 --- a/front/src/api/routineAPI.js +++ b/front/src/api/routineAPI.js @@ -35,7 +35,7 @@ async function fetchWithOptions(url, options) { method: 'GET', }); - // 빈 루틴도 표시할 수 있도록 ��환 + // 빈 루틴도 표시할 수 있도록 환 return response.map(routine => ({ _id: routine._id, user_id: routine.user_id, @@ -74,10 +74,37 @@ async function fetchWithOptions(url, options) { }; const createRoutine = async (routineName) => { - return await fetchWithOptions('/api/routine', { - method: 'POST', - body: JSON.stringify({ routine_name: routineName }), - }); + try { + // 입력값 검증 + if (!routineName || typeof routineName !== 'string') { + throw new Error('유효한 루틴 이름을 입력해주세요.'); + } + + const response = await fetch('/api/routine', { + method: 'POST', + headers: { + "Authorization": `Bearer ${localStorage.getItem('accessToken')}`, + "Content-Type": "application/json", + }, + body: JSON.stringify({ routine_name: routineName.trim() }) + }); + + if (!response.ok) { + const errorData = await response.json().catch(() => ({})); + throw new Error(errorData.message || '루틴 생성에 실패했습니다.'); + } + + // 텍스트 응답도 처리할 수 있도록 수정 + const responseText = await response.text(); + try { + return JSON.parse(responseText); + } catch { + return { message: responseText }; + } + } catch (error) { + console.error("루틴 생성 중 오류:", error.message); + throw error; + } }; const deleteRoutine = async (routineName) => { diff --git a/front/src/components/routine/List.jsx b/front/src/components/routine/List.jsx index 0e6dd5c81ae34072029eab526348b213f394c8dc..98cd40587c39e19288a843d1929d12b0cca1a481 100644 --- a/front/src/components/routine/List.jsx +++ b/front/src/components/routine/List.jsx @@ -89,14 +89,23 @@ function List({ onRoutineSelect, isActive }) { const handleAddRoutine = async () => { try { - const routineName = prompt("새로운 루틴의 이름을 입력하세요:"); - if (!routineName) return; - - await createRoutine(routineName); - await fetchRoutines(); + const routineName = prompt("새로운 루틴의 이름을 입력하세요:"); + if (!routineName) { + return; // 사용자가 취소하거나 빈 문자열 입력 시 + } + + // 특수문자나 공백만 있는 경우 체크 + if (!/\S/.test(routineName)) { + alert("유효한 루틴 이름을 입력해주세요."); + return; + } + + await createRoutine(routineName); + alert("루틴이 성공적으로 생성되었습니다."); + await fetchRoutines(); // 목록 새로고침 } catch (err) { - console.error("루틴 추가 실패:", err); - alert("루틴 추가에 실패했습니다."); + console.error("루틴 추가 실패:", err); + alert(err.message || "루틴 추가에 실패했습니다."); } };