@@ -5,41 +5,86 @@ import axios from "axios";
55interface ModalProps {
66 name : string ;
77 onClose : ( ) => void ;
8+ scoreSummaryId : number ;
89}
910
10- export default function Modal ( { name, onClose } : ModalProps ) {
11+ export default function Modal ( { name, onClose, scoreSummaryId } : ModalProps ) {
1112 const [ feedback , setFeedback ] = useState ( "" ) ;
13+ const [ initialFeedback , setInitialFeedback ] = useState < string | null > ( null ) ;
1214
1315 useEffect ( ( ) => {
1416 const token = localStorage . getItem ( "accessToken" ) ;
1517 const getFeedback = async ( ) => {
1618 try {
17- const res = await axios . get ( `${ process . env . NEXT_PUBLIC_BACKEND_DOMAIN } ` , {
18- headers : { Authorization : `Bearer ${ token } ` } ,
19- } ) ;
20- const data = res . data ;
21- console . log ( data ) ;
22- // 초기 피드백 데이터를 여기서 넣고 싶으면:
23- // setFeedback(data.feedback);
19+ const res = await axios . get ( `${ process . env . NEXT_PUBLIC_BACKEND_DOMAIN } /score-summary/feedback/${ scoreSummaryId } ` , { headers : { Authorization : `Bearer ${ token } ` } } ) ;
20+ const feedbackData = res . data . response . feedback ;
21+ setInitialFeedback ( feedbackData ) ;
22+ setFeedback ( feedbackData || "" ) ; // null이면 빈 문자열로
2423 } catch ( err ) {
25- console . error ( err ) ;
24+ console . error ( "피드백 불러오기 실패:" , err ) ;
2625 }
2726 } ;
2827 getFeedback ( ) ;
29- } , [ ] ) ;
28+ } , [ scoreSummaryId ] ) ;
29+
30+ const token = localStorage . getItem ( "accessToken" ) ;
31+
32+ const postFeedback = async ( ) => {
33+ try {
34+ const res = await axios . post (
35+ `${ process . env . NEXT_PUBLIC_BACKEND_DOMAIN } /score-summary/feedback` ,
36+ {
37+ scoreSummaryId,
38+ feedback,
39+ } ,
40+ { headers : { Authorization : `Bearer ${ token } ` } }
41+ ) ;
42+ console . log ( "POST 성공:" , res . data ) ;
43+ } catch ( err ) {
44+ console . error ( "POST 실패:" , err ) ;
45+ }
46+ } ;
47+
48+ const putFeedback = async ( ) => {
49+ try {
50+ const res = await axios . put (
51+ `${ process . env . NEXT_PUBLIC_BACKEND_DOMAIN } /score-summary/feedback/${ scoreSummaryId } ` ,
52+ { feedback } ,
53+ { headers : { Authorization : `Bearer ${ token } ` } }
54+ ) ;
55+ console . log ( "PUT 성공:" , res . data ) ;
56+ } catch ( err ) {
57+ console . error ( "PUT 실패:" , err ) ;
58+ }
59+ } ;
60+
61+ const handleSave = async ( ) => {
62+ // 변경 사항 없으면 요청 없이 닫기
63+ if ( feedback === ( initialFeedback ?? "" ) ) {
64+ onClose ( ) ;
65+ return ;
66+ }
67+
68+ if ( initialFeedback === null ) {
69+ await postFeedback ( ) ;
70+ } else {
71+ await putFeedback ( ) ;
72+ }
73+ onClose ( ) ;
74+ } ;
3075
3176 return (
3277 < div className = "fixed inset-0 bg-black bg-opacity-40 flex justify-center items-center z-50" onClick = { onClose } >
3378 < div className = "flex flex-col bg-white p-6 rounded shadow-lg w-[600px] min-h-[300px] z-100" onClick = { ( e ) => e . stopPropagation ( ) } >
3479 < h2 className = "text-xl font-bold mb-2" > { name } </ h2 >
3580 < div className = "flex flex-col justify-between h-80" >
3681 < textarea
37- className = "w-full h-full resize-none border border-gray-300 rounded p-2 select-none "
82+ className = "w-full h-full resize-none border border-gray-300 rounded p-2"
3883 placeholder = "피드백을 작성해주세요"
3984 value = { feedback }
4085 onChange = { ( e ) => setFeedback ( e . target . value ) }
4186 />
42- < Button className = "mt-4 h-12" onClick = { onClose } >
87+ < Button className = "mt-4 h-12" onClick = { handleSave } >
4388 닫기
4489 </ Button >
4590 </ div >
0 commit comments