1+ import { isAxiosError } from 'axios' ;
12import { useAtom } from 'jotai' ;
23import { useState } from 'react' ;
34import { useNavigate } from 'react-router-dom' ;
45import { createRoom } from '../../api/room' ;
56import { isLoggedInAtom } from '../../common/user' ;
67import './CreateRoom.css' ;
78
8- const landmarks = {
9- 서울대입구역 : 1 ,
10- 낙성대역 : 2 ,
11- 자연대 : 3 ,
12- 행정관 : 4 ,
13- } ;
9+ const LANDMARKS = [
10+ { id : 1 , name : '서울대입구역 3번 출구' } ,
11+ { id : 2 , name : '낙성대역 버스정류장' } ,
12+ { id : 3 , name : '낙성대입구 버스정류장' } ,
13+ { id : 4 , name : '대학동고시촌입구(녹두)' } ,
14+ { id : 5 , name : '사당역 4번 출구' } ,
15+ { id : 6 , name : '경영대.행정대학원' } ,
16+ { id : 7 , name : '자연대.행정관입구' } ,
17+ { id : 8 , name : '법대.사회대입구' } ,
18+ { id : 9 , name : '농생대' } ,
19+ { id : 10 , name : '공대입구(글로벌공학)' } ,
20+ { id : 11 , name : '제2공학관(302동)' } ,
21+ { id : 12 , name : '학부생활관' } ,
22+ { id : 13 , name : '수의대입구.보건대학원' } ,
23+ { id : 14 , name : '기숙사 삼거리' } ,
24+ { id : 15 , name : '국제대학원' } ,
25+ ] ;
1426
1527const CreateRoom = ( ) => {
1628 const navigate = useNavigate ( ) ;
17- const [ start , setStart ] = useState ( '' ) ;
18- const [ end , setEnd ] = useState ( '' ) ;
29+ const [ start , setStart ] = useState ( '1 ' ) ;
30+ const [ end , setEnd ] = useState ( '2 ' ) ;
1931 const [ departureTime , setDepartureTime ] = useState ( '' ) ;
2032 const [ minCapacity , setMinCapacity ] = useState ( 2 ) ;
2133 const [ isLoggedIn ] = useAtom ( isLoggedInAtom ) ;
@@ -44,25 +56,31 @@ const CreateRoom = () => {
4456 return ;
4557 }
4658
47- const departureId = landmarks [ start as keyof typeof landmarks ] ;
48- const destinationId = landmarks [ end as keyof typeof landmarks ] ;
59+ const departureId = parseInt ( start , 10 ) ;
60+ const destinationId = parseInt ( end , 10 ) ;
4961
5062 const departureTimeISO = new Date ( departureTime ) . toISOString ( ) ;
5163
64+ const roomDetails = {
65+ departureId,
66+ destinationId,
67+ departureTime : departureTimeISO ,
68+ minCapacity,
69+ maxCapacity : 4 , // Hardcoded
70+ estimatedFee : 0 , // Hardcoded
71+ } ;
72+
5273 try {
53- await createRoom ( {
54- departureId,
55- destinationId,
56- departureTime : departureTimeISO ,
57- minCapacity,
58- maxCapacity : 4 , // Hardcoded
59- estimatedFee : 0 , // Hardcoded
60- } ) ;
61-
62- alert ( '방이 성공적으로 개설되었습니다!' ) ;
74+ const _response = await createRoom ( roomDetails ) ;
75+
76+ alert ( '방이 성공적으로 개설되었습니다! ID: ' + _response . createdPotId ) ;
6377 navigate ( '/search-room' ) ;
64- } catch ( error ) {
65- console . error ( 'Error creating room:' , error ) ;
78+ } catch ( error : unknown ) {
79+ if ( isAxiosError ( error ) ) {
80+ console . error ( 'Error creating room:' , error . response ?. data ) ;
81+ } else {
82+ console . error ( 'An unexpected error occurred:' , error ) ;
83+ }
6684 alert ( '방 개설 중 오류가 발생했습니다.' ) ;
6785 }
6886 } ;
@@ -75,9 +93,9 @@ const CreateRoom = () => {
7593 < div className = "location-select" >
7694 < select value = { start } onChange = { ( e ) => setStart ( e . target . value ) } >
7795 < option value = "" > 출발지</ option >
78- { Object . keys ( landmarks ) . map ( ( landmark ) => (
79- < option key = { `start-${ landmark } ` } value = { landmark } >
80- { landmark }
96+ { LANDMARKS . map ( ( landmark ) => (
97+ < option key = { `start-${ landmark . id } ` } value = { landmark . id } >
98+ { landmark . name }
8199 </ option >
82100 ) ) }
83101 </ select >
@@ -86,9 +104,9 @@ const CreateRoom = () => {
86104 < div className = "location-select" >
87105 < select value = { end } onChange = { ( e ) => setEnd ( e . target . value ) } >
88106 < option value = "" > 도착지</ option >
89- { Object . keys ( landmarks ) . map ( ( landmark ) => (
90- < option key = { `end-${ landmark } ` } value = { landmark } >
91- { landmark }
107+ { LANDMARKS . map ( ( landmark ) => (
108+ < option key = { `end-${ landmark . id } ` } value = { landmark . id } >
109+ { landmark . name }
92110 </ option >
93111 ) ) }
94112 </ select >
0 commit comments