1- import axios from 'axios' ;
21import { useState } from 'react' ;
2+ import { useNavigate } from 'react-router-dom' ;
3+ import { createRoom } from '../../api/room' ;
34import { userState } from '../../common/user' ;
45import './CreateRoom.css' ;
56
67const landmarks = [ '서울대입구역' , '낙성대역' , '자연대' , '행정관' ] ;
78
89const CreateRoom = ( ) => {
10+ const navigate = useNavigate ( ) ;
911 const [ start , setStart ] = useState ( '' ) ;
1012 const [ end , setEnd ] = useState ( '' ) ;
11- const [ dateTime , setDateTime ] = useState ( '' ) ;
12- const [ roomName , setRoomName ] = useState ( '' ) ;
13- const [ minParticipants , setMinParticipants ] = useState ( 2 ) ;
13+ const [ departureTime , setDepartureTime ] = useState ( '' ) ;
14+ const [ minCapacity , setMinCapacity ] = useState ( 2 ) ;
1415
15- const handleParticipantChange = ( amount : number ) => {
16- setMinParticipants ( ( prev ) => {
16+ const handleCapacityChange = ( amount : number ) => {
17+ setMinCapacity ( ( prev ) => {
1718 const newValue = prev + amount ;
18- if ( newValue >= 2 && newValue <= 4 ) {
19+ if ( newValue >= 1 && newValue <= 4 ) {
1920 return newValue ;
2021 }
2122 return prev ;
2223 } ) ;
2324 } ;
2425
2526 const handleCreateRoom = async ( ) => {
26- if ( ! start || ! end || ! dateTime || ! roomName ) {
27+ // Adjusted validation
28+ if ( ! start || ! end || ! departureTime || ! minCapacity ) {
2729 alert ( '모든 필드를 채워주세요.' ) ;
2830 return ;
2931 }
3032
31- // check for development
3233 if ( ! userState . isLoggedIn ) {
3334 alert ( '로그인이 필요합니다.' ) ;
34- window . location . href = '/login ' ;
35+ window . location . href = '/' ;
3536 return ;
3637 }
37- if ( import . meta. env . DEV ) {
38- userState . email = 'dev@snu.ac.kr' ;
39- }
38+
39+ const departureId = landmarks . indexOf ( start ) + 1 ;
40+ const destinationId = landmarks . indexOf ( end ) + 1 ;
41+
42+ const departureTimeISO = new Date ( departureTime ) . toISOString ( ) ;
4043
4144 try {
42- const response = await axios . post ( '/room' , {
43- name : roomName ,
44- from : start ,
45- to : end ,
46- time : dateTime ,
47- minHeadcount : minParticipants ,
48- user : userState . email , // Send user's email
45+ await createRoom ( {
46+ departureId ,
47+ destinationId ,
48+ departureTime : departureTimeISO ,
49+ minCapacity ,
50+ maxCapacity : 4 ,
51+ estimatedFee : 0 , // Hardcoded placeholder for estimatedFee
4952 } ) ;
5053
51- if ( response . status === 201 || response . status === 200 ) {
52- alert ( '방이 성공적으로 개설되었습니다!' ) ;
53- // Redirect to the new room's page?
54- // window.location.href = `/rooms/${response.data.roomId}`;
55- } else {
56- alert ( '방 개설에 실패했습니다.' ) ;
57- }
54+ alert ( '방이 성공적으로 개설되었습니다!' ) ;
55+ navigate ( '/search-room' ) ;
5856 } catch ( error ) {
5957 console . error ( 'Error creating room:' , error ) ;
6058 alert ( '방 개설 중 오류가 발생했습니다.' ) ;
@@ -94,20 +92,8 @@ const CreateRoom = () => {
9492 < label > 날짜 및 시간</ label >
9593 < input
9694 type = "datetime-local"
97- value = { dateTime }
98- onChange = { ( e ) => setDateTime ( e . target . value ) }
99- />
100- </ div >
101- </ div >
102-
103- < div className = "card" >
104- < div className = "input-group" >
105- < label > 방 이름</ label >
106- < input
107- type = "text"
108- placeholder = "방 이름을 입력하세요"
109- value = { roomName }
110- onChange = { ( e ) => setRoomName ( e . target . value ) }
95+ value = { departureTime }
96+ onChange = { ( e ) => setDepartureTime ( e . target . value ) }
11197 />
11298 </ div >
11399 </ div >
@@ -116,9 +102,9 @@ const CreateRoom = () => {
116102 < div className = "input-group" >
117103 < label > 최소 인원</ label >
118104 < div className = "participant-control" >
119- < button onClick = { ( ) => handleParticipantChange ( - 1 ) } > -</ button >
120- < span > { minParticipants } 명</ span >
121- < button onClick = { ( ) => handleParticipantChange ( 1 ) } > +</ button >
105+ < button onClick = { ( ) => handleCapacityChange ( - 1 ) } > -</ button >
106+ < span > { minCapacity } 명</ span >
107+ < button onClick = { ( ) => handleCapacityChange ( 1 ) } > +</ button >
122108 </ div >
123109 </ div >
124110 </ div >
0 commit comments