@@ -22,9 +22,12 @@ let socket;
22
22
const { API_BASE_URL } = config ;
23
23
const RoomsContainer = ( ) => {
24
24
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('');
26
28
const [ userJoined , setUserJoined ] = useState ( false ) ; //setting up state for joinning a room
27
29
const [ emptyInput , setEmptyInput ] = useState ( false ) ;
30
+
28
31
const dispatch = useDispatch ( ) ;
29
32
const { state, joinedRoom } = useSelector ( ( store : RootState ) => ( {
30
33
state : store . appState ,
@@ -45,27 +48,28 @@ const RoomsContainer = () => {
45
48
} ) ;
46
49
47
50
socket . on ( 'connect' , ( ) => {
48
- console . log ( `You connected with id : ${ socket . id } ` ) ;
51
+ console . log ( `You Connected With Id : ${ socket . id } ` ) ;
49
52
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 ) ;
50
56
} ) ;
51
57
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
+ // });
60
66
61
67
// receiving the message from the back end
62
68
socket . on ( 'receive message' , ( event ) => {
63
- // console.log('message from server: ', event);
64
69
let currentStore : any = JSON . stringify ( store . getState ( ) ) ;
65
70
if ( currentStore !== event ) {
66
71
currentStore = JSON . parse ( currentStore ) ;
67
72
event = JSON . parse ( event ) ;
68
-
69
73
if ( currentStore . appState !== event . appState ) {
70
74
store . dispatch ( allCooperativeState ( event . appState ) ) ;
71
75
} else if (
@@ -86,15 +90,16 @@ const RoomsContainer = () => {
86
90
let previousState = store . getState ( ) ;
87
91
// console.log('Store States: ', store.getState);
88
92
// sending info to backend whenever the redux store changes
93
+ //working!
89
94
const handleStoreChange = debounce ( ( ) => {
90
95
const newState = store . getState ( ) ;
91
96
const roomCode = newState . roomCodeSlice . roomCode ;
92
97
93
98
if ( roomCode !== '' ) {
99
+ //why emitting room code every 100 milisecond
94
100
// Emit the current room code
95
101
socket . emit ( 'room-code' , roomCode ) ;
96
102
}
97
-
98
103
if ( newState !== previousState ) {
99
104
// Send the current state to the server
100
105
socket . emit (
@@ -114,20 +119,32 @@ const RoomsContainer = () => {
114
119
} ) ;
115
120
116
121
function joinRoom ( ) {
117
- dispatch ( changeRoom ( roomCode ) ) ;
118
- setConfirmRoom ( ( confirmRoom ) => roomCode ) ;
119
122
// Call handleUserEnteredRoom when joining a room
120
123
handleUserEnteredRoom ( roomCode ) ;
124
+ dispatch ( changeRoom ( roomCode ) ) ;
125
+ // setConfirmRoom((confirmRoom) => roomCode);
121
126
setUserJoined ( true ) ; //setting joined room to true for rendering leave room button
122
127
}
123
128
124
129
function leaveRoom ( ) {
125
130
if ( socket ) socket . disconnect ( ) ; //disconnecting socket
126
131
dispatch ( changeRoom ( '' ) ) ;
127
132
setRoomCode ( '' ) ;
133
+ setUserName ( '' ) ;
128
134
setUserJoined ( false ) ; //setting joined to false so join button appear
129
135
}
130
136
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
+
131
148
return (
132
149
< div >
133
150
< Stack //stack styling for container
@@ -163,6 +180,15 @@ const RoomsContainer = () => {
163
180
</ Button >
164
181
) : (
165
182
< >
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
+ />
166
192
< TextField
167
193
hiddenLabel = { true }
168
194
id = "filled-hidden-label-small"
@@ -172,9 +198,10 @@ const RoomsContainer = () => {
172
198
placeholder = "Input Room Number"
173
199
onChange = { ( e ) => setRoomCode ( e . target . value ) }
174
200
/>
201
+
175
202
< Button
176
203
variant = "contained"
177
- disabled = { roomCode . trim ( ) === '' }
204
+ disabled = { checkInputField ( userName , roomCode ) }
178
205
onClick = { ( ) => joinRoom ( ) }
179
206
sx = { {
180
207
backgroundColor : '#ffffff' ,
0 commit comments