@@ -22,9 +22,12 @@ let socket;
2222const { API_BASE_URL } = config ;
2323const RoomsContainer = ( ) => {
2424 const [ roomCode , setRoomCode ] = useState ( '' ) ;
25- const [ confirmRoom , setConfirmRoom ] = useState ( '' ) ;
25+ const [ userName , setUserName ] = useState ( '' ) ;
26+ const [ userList , setUserList ] = useState ( new Map ( ) ) ;
27+ // const [confirmRoom, setConfirmRoom] = useState('');
2628 const [ userJoined , setUserJoined ] = useState ( false ) ; //setting up state for joinning a room
2729 const [ emptyInput , setEmptyInput ] = useState ( false ) ;
30+
2831 const dispatch = useDispatch ( ) ;
2932 const { state, joinedRoom } = useSelector ( ( store : RootState ) => ( {
3033 state : store . appState ,
@@ -45,27 +48,28 @@ const RoomsContainer = () => {
4548 } ) ;
4649
4750 socket . on ( 'connect' , ( ) => {
48- console . log ( `You connected with id : ${ socket . id } ` ) ;
51+ console . log ( `You Connected With Id : ${ socket . id } ` ) ;
4952 socket . emit ( 'join-room' , roomCode ) ; // Join the room when connected
53+ //passing current client nickname to server
54+ console . log ( `Your Nickname Is: ${ userName } ` ) ;
55+ socket . emit ( 'user' , userName ) ;
5056 } ) ;
5157
52- // Receiving the room state from the backend
53- socket . on ( 'room-state-update' , ( stateFromServer ) => {
54- const newState = JSON . parse ( stateFromServer ) ;
55- // Dispatch actions to update your Redux store with the received state
56- store . dispatch ( allCooperativeState ( newState . appState ) ) ;
57- store . dispatch ( codePreviewCooperative ( newState . codePreviewCooperative ) ) ;
58- store . dispatch ( cooperativeStyle ( newState . styleSlice ) ) ;
59- } ) ;
58+ // // Receiving the room state from the backend
59+ // socket.on('room-state-update', (stateFromServer) => {
60+ // const newState = JSON.parse(stateFromServer);
61+ // // Dispatch actions to update your Redux store with the received state
62+ // store.dispatch(allCooperativeState(newState.appState));
63+ // store.dispatch(codePreviewCooperative(newState.codePreviewCooperative));
64+ // store.dispatch(cooperativeStyle(newState.styleSlice));
65+ // });
6066
6167 // receiving the message from the back end
6268 socket . on ( 'receive message' , ( event ) => {
63- // console.log('message from server: ', event);
6469 let currentStore : any = JSON . stringify ( store . getState ( ) ) ;
6570 if ( currentStore !== event ) {
6671 currentStore = JSON . parse ( currentStore ) ;
6772 event = JSON . parse ( event ) ;
68-
6973 if ( currentStore . appState !== event . appState ) {
7074 store . dispatch ( allCooperativeState ( event . appState ) ) ;
7175 } else if (
@@ -86,15 +90,16 @@ const RoomsContainer = () => {
8690 let previousState = store . getState ( ) ;
8791 // console.log('Store States: ', store.getState);
8892 // sending info to backend whenever the redux store changes
93+ //working!
8994 const handleStoreChange = debounce ( ( ) => {
9095 const newState = store . getState ( ) ;
9196 const roomCode = newState . roomCodeSlice . roomCode ;
9297
9398 if ( roomCode !== '' ) {
99+ //why emitting room code every 100 milisecond
94100 // Emit the current room code
95101 socket . emit ( 'room-code' , roomCode ) ;
96102 }
97-
98103 if ( newState !== previousState ) {
99104 // Send the current state to the server
100105 socket . emit (
@@ -117,17 +122,29 @@ const RoomsContainer = () => {
117122 // Call handleUserEnteredRoom when joining a room
118123 handleUserEnteredRoom ( roomCode ) ;
119124 dispatch ( changeRoom ( roomCode ) ) ;
120- setConfirmRoom ( ( confirmRoom ) => roomCode ) ;
125+ // setConfirmRoom((confirmRoom) => roomCode);
121126 setUserJoined ( true ) ; //setting joined room to true for rendering leave room button
122127 }
123128
124129 function leaveRoom ( ) {
125130 if ( socket ) socket . disconnect ( ) ; //disconnecting socket
126131 dispatch ( changeRoom ( '' ) ) ;
127132 setRoomCode ( '' ) ;
133+ setUserName ( '' ) ;
128134 setUserJoined ( false ) ; //setting joined to false so join button appear
129135 }
130136
137+ //checking if both text field have any input (not including spaces)
138+ function checkInputField ( ...inputs : any ) {
139+ let userName : String = inputs [ 0 ] . trim ( ) ;
140+ let roomCode : String = inputs [ 1 ] . trim ( ) ;
141+ if ( userName . length !== 0 && roomCode . length !== 0 ) {
142+ return false ;
143+ } else {
144+ return true ;
145+ }
146+ }
147+
131148 return (
132149 < div >
133150 < Stack //stack styling for container
@@ -163,6 +180,15 @@ const RoomsContainer = () => {
163180 </ Button >
164181 ) : (
165182 < >
183+ < TextField
184+ hiddenLabel = { true }
185+ id = "filled-hidden-label-small"
186+ variant = "filled"
187+ size = "small"
188+ value = { userName }
189+ placeholder = "Input nickname"
190+ onChange = { ( e ) => setUserName ( e . target . value ) }
191+ />
166192 < TextField
167193 hiddenLabel = { true }
168194 id = "filled-hidden-label-small"
@@ -172,9 +198,10 @@ const RoomsContainer = () => {
172198 placeholder = "Input Room Number"
173199 onChange = { ( e ) => setRoomCode ( e . target . value ) }
174200 />
201+
175202 < Button
176203 variant = "contained"
177- disabled = { roomCode . trim ( ) === '' }
204+ disabled = { checkInputField ( userName , roomCode ) }
178205 onClick = { ( ) => joinRoom ( ) }
179206 sx = { {
180207 backgroundColor : '#ffffff' ,
0 commit comments