@@ -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,26 +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
69
let currentStore : any = JSON . stringify ( store . getState ( ) ) ;
64
70
if ( currentStore !== event ) {
65
71
currentStore = JSON . parse ( currentStore ) ;
66
72
event = JSON . parse ( event ) ;
67
-
68
73
if ( currentStore . appState !== event . appState ) {
69
74
store . dispatch ( allCooperativeState ( event . appState ) ) ;
70
75
} else if (
@@ -85,15 +90,16 @@ const RoomsContainer = () => {
85
90
let previousState = store . getState ( ) ;
86
91
// console.log('Store States: ', store.getState);
87
92
// sending info to backend whenever the redux store changes
93
+ //working!
88
94
const handleStoreChange = debounce ( ( ) => {
89
95
const newState = store . getState ( ) ;
90
96
const roomCode = newState . roomCodeSlice . roomCode ;
91
97
92
98
if ( roomCode !== '' ) {
99
+ //why emitting room code every 100 milisecond
93
100
// Emit the current room code
94
101
socket . emit ( 'room-code' , roomCode ) ;
95
102
}
96
-
97
103
if ( newState !== previousState ) {
98
104
// Send the current state to the server
99
105
socket . emit (
@@ -113,20 +119,32 @@ const RoomsContainer = () => {
113
119
} ) ;
114
120
115
121
function joinRoom ( ) {
116
- dispatch ( changeRoom ( roomCode ) ) ;
117
- setConfirmRoom ( ( confirmRoom ) => roomCode ) ;
118
122
// Call handleUserEnteredRoom when joining a room
119
123
handleUserEnteredRoom ( roomCode ) ;
124
+ dispatch ( changeRoom ( roomCode ) ) ;
125
+ // setConfirmRoom((confirmRoom) => roomCode);
120
126
setUserJoined ( true ) ; //setting joined room to true for rendering leave room button
121
127
}
122
128
123
129
function leaveRoom ( ) {
124
130
if ( socket ) socket . disconnect ( ) ; //disconnecting socket
125
131
dispatch ( changeRoom ( '' ) ) ;
126
132
setRoomCode ( '' ) ;
133
+ setUserName ( '' ) ;
127
134
setUserJoined ( false ) ; //setting joined to false so join button appear
128
135
}
129
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
+
130
148
return (
131
149
< div >
132
150
< Stack //stack styling for container
@@ -162,6 +180,15 @@ const RoomsContainer = () => {
162
180
</ Button >
163
181
) : (
164
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
+ />
165
192
< TextField
166
193
hiddenLabel = { true }
167
194
id = "filled-hidden-label-small"
@@ -171,9 +198,10 @@ const RoomsContainer = () => {
171
198
placeholder = "Input Room Number"
172
199
onChange = { ( e ) => setRoomCode ( e . target . value ) }
173
200
/>
201
+
174
202
< Button
175
203
variant = "contained"
176
- disabled = { roomCode . trim ( ) === '' }
204
+ disabled = { checkInputField ( userName , roomCode ) }
177
205
onClick = { ( ) => joinRoom ( ) }
178
206
sx = { {
179
207
backgroundColor : '#ffffff' ,
0 commit comments