From 6961086c0b49d46e3d42f1d8aa114c794085751e Mon Sep 17 00:00:00 2001 From: BakoD <rudghrnt@ajou.ac.kr> Date: Sun, 8 Dec 2024 22:47:04 +0900 Subject: [PATCH] Fix: Implement Routine Add --- front/src/api/routineAPI.js | 37 +++++++++++++++++++++++---- front/src/components/routine/List.jsx | 23 ++++++++++++----- 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/front/src/api/routineAPI.js b/front/src/api/routineAPI.js index aebbcf8..ffbf4fb 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 0e6dd5c..98cd405 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 || "루틴 추가에 실패했습니다."); } }; -- GitLab