@@ -59,23 +59,6 @@ import {
59
59
addComponentToContext
60
60
} from '../../../src/redux/reducers/slice/contextReducer' ;
61
61
62
- // // for websockets
63
- // // Part - join room and room code functionality
64
- let socket ;
65
-
66
- //function to create HTML elements and update the position of the cursorr.
67
- // function getCursor(id) {
68
- // let elementId = 'cursor-' + id;
69
- // let element = document.getElementById(elementId);
70
- // if (element == null) {
71
- // element = document.createElement('div');
72
- // element.id = elementId;
73
- // element.className = 'cursor';
74
- // document.appendChild(element);
75
- // }
76
- // return element;
77
- // }
78
-
79
62
const RoomsContainer = ( ) => {
80
63
const dispatch = useDispatch ( ) ;
81
64
//roomCode/userName for emiting to socket io, userList for displaying user List receiving from back end, userJoined fo conditional rendering between join and leave room.
@@ -86,21 +69,18 @@ const RoomsContainer = () => {
86
69
( store : RootState ) => store . roomSlice . userJoined
87
70
) ;
88
71
72
+ // for websockets - initialize socket connection passing in roomCode
89
73
function initSocketConnection ( roomCode : string ) {
90
- // if (socket) socket.disconnect(); //edge case: disconnect previous socket connection
91
- // // establishing client and server connection
92
- // socket = io(API_BASE_URL, {
93
- // transports: ['websocket']
94
- // });
95
-
74
+ // helper function to create socket connection
96
75
initializeSocket ( ) ;
76
+ // assign socket to result of helper function to return socket created
97
77
const socket = getSocket ( ) ;
98
- // only create a new connection if not already connected
78
+ // if socket was created correctly and exists
99
79
if ( socket ) {
100
80
//run everytime when a client connects to server
101
81
socket . on ( 'connect' , ( ) => {
102
82
socket . emit ( 'joining' , userName , roomCode ) ;
103
- console . log ( `${ userName } Joined room ${ roomCode } from RoomsContainer` ) ;
83
+ // console.log(`${userName} Joined room ${roomCode} from RoomsContainer`);
104
84
} ) ;
105
85
106
86
//If you are the host: send current state to server when a new user joins
@@ -112,7 +92,7 @@ const RoomsContainer = () => {
112
92
//If you are the new user: receive the state from the host
113
93
socket . on ( 'server emitting state from host' , ( state , callback ) => {
114
94
//dispatching new state to change user current state
115
- console . log ( 'state recieved by new join:' , state ) ;
95
+ // console.log('state received by new join:', state);
116
96
store . dispatch ( allCooperativeState ( state . appState ) ) ;
117
97
store . dispatch ( codePreviewCooperative ( state . codePreviewCooperative ) ) ;
118
98
store . dispatch ( cooperativeStyle ( state . styleSlice ) ) ;
@@ -123,24 +103,27 @@ const RoomsContainer = () => {
123
103
socket . on ( 'updateUserList' , ( newUserList ) => {
124
104
//console.log('user list received from server');
125
105
dispatch ( setUserList ( newUserList ) ) ;
126
- // console.log('userList:', userList);
127
106
} ) ;
128
107
108
+ // dispatch add child to local state when element has been added by another user
129
109
socket . on ( 'child data from server' , ( childData : object ) => {
130
110
// console.log('child data received by users', childData);
131
111
store . dispatch ( addChild ( childData ) ) ;
132
112
} ) ;
133
113
114
+ // dispatch changeFocus to local state when another user has changed focus by selecting element on canvas
134
115
socket . on ( 'focus data from server' , ( focusData : object ) => {
135
116
// console.log('focus data received from server', focusData);
136
117
store . dispatch ( changeFocus ( focusData ) ) ;
137
118
} ) ;
138
119
120
+ // dispatch deleteChild to local state when another user has deleted an element
139
121
socket . on ( 'delete data from server' , ( deleteData : object ) => {
140
122
// console.log('delete data received from server', deleteData);
141
123
store . dispatch ( deleteChild ( deleteData ) ) ;
142
124
} ) ;
143
125
126
+ // dispatch delete element to local state when another user has deleted an element
144
127
socket . on (
145
128
'delete element data from server' ,
146
129
( deleteElementData : object ) => {
@@ -149,6 +132,7 @@ const RoomsContainer = () => {
149
132
}
150
133
) ;
151
134
135
+ // dispatch all updates to local state when another user has saved from Bottom Panel
152
136
socket . on ( 'update data from server' , ( updateData : BottomPanelObj ) => {
153
137
// console.log('update data received from server', updateData);
154
138
store . dispatch (
@@ -183,76 +167,88 @@ const RoomsContainer = () => {
183
167
) ;
184
168
} ) ;
185
169
170
+ // dispatch update style in local state when CSS panel is updated on their side
186
171
socket . on ( 'update css data from server' , ( cssData : object ) => {
187
172
// console.log('CSS data received from server', cssData);
188
173
store . dispatch ( updateStylesheet ( cssData ) ) ;
189
174
} ) ;
190
175
176
+ // dispatch new item position in local state when item position is changed by another user
191
177
socket . on (
192
178
'item position data from server' ,
193
179
( itemPositionData : object ) => {
194
- console . log (
195
- 'item position data received from server' ,
196
- itemPositionData
197
- ) ;
180
+ // console.log(
181
+ // 'item position data received from server',
182
+ // itemPositionData
183
+ // );
198
184
store . dispatch ( changePosition ( itemPositionData ) ) ;
199
185
}
200
186
) ;
201
187
188
+ // dispatch addComponent to local state when new component is created by another user
202
189
socket . on ( 'new component data from server' , ( newComponent : object ) => {
203
190
store . dispatch ( addComponent ( newComponent ) ) ;
204
191
} ) ;
205
192
193
+ // dispatch addElement to local state when new element is created by another user
206
194
socket . on ( 'new element data from server' , ( newElement : object ) => {
207
195
store . dispatch ( addElement ( newElement ) ) ;
208
196
} ) ;
209
197
198
+ // dispatch addState to local state when component state has been changed by another user
210
199
socket . on (
211
200
'new component state data from server' ,
212
201
( componentState : object ) => {
213
202
store . dispatch ( addState ( componentState ) ) ;
214
203
}
215
204
) ;
216
205
206
+ // dispatch deleteState to local state when component state has been deleted by another user
217
207
socket . on (
218
208
'delete component state data from server' ,
219
209
( componentStateDelete : object ) => {
220
210
store . dispatch ( deleteState ( componentStateDelete ) ) ;
221
211
}
222
212
) ;
223
213
214
+ // dispatch addPassedInProps to local state when p.I.P have been added by another user
224
215
socket . on (
225
216
'new PassedInProps data from server' ,
226
217
( passedInProps : object ) => {
227
218
store . dispatch ( addPassedInProps ( passedInProps ) ) ;
228
219
}
229
220
) ;
230
221
222
+ // dispatch deletePassedInProps to local state when p.I.P have been deleted by another user
231
223
socket . on (
232
224
'PassedInProps delete data from server' ,
233
225
( passedInProps : object ) => {
234
226
store . dispatch ( deletePassedInProps ( passedInProps ) ) ;
235
227
}
236
228
) ;
237
229
230
+ // dispatch addContext to local state when context has been changed by another user
238
231
socket . on ( 'new context from server' , ( context : AddContextPayload ) => {
239
232
store . dispatch ( addContext ( context ) ) ;
240
233
} ) ;
241
234
235
+ // dispatch addContextValues to local state when context values are added by another user
242
236
socket . on (
243
237
'new context value from server' ,
244
238
( contextVal : AddContextValuesPayload ) => {
245
239
store . dispatch ( addContextValues ( contextVal ) ) ;
246
240
}
247
241
) ;
248
242
243
+ // dispatch deleteContext to local state when context is deleted by another user
249
244
socket . on (
250
245
'delete context data from server' ,
251
246
( context : DeleteContextPayload ) => {
252
247
store . dispatch ( deleteContext ( context ) ) ;
253
248
}
254
249
) ;
255
250
251
+ // dispatch addComponentToContext to local state when context is assigned to component by another user
256
252
socket . on ( 'assign context data from server' , ( data ) => {
257
253
store . dispatch (
258
254
addComponentToContext ( {
@@ -267,6 +263,7 @@ const RoomsContainer = () => {
267
263
}
268
264
}
269
265
266
+ // invoked when join room in invoked passing in room code to init socket for that room
270
267
function handleUserEnteredRoom ( roomCode ) {
271
268
initSocketConnection ( roomCode ) ;
272
269
}
@@ -276,17 +273,16 @@ const RoomsContainer = () => {
276
273
//edge case: if userList is not empty, reset it to empty array
277
274
if ( userList . length !== 0 ) dispatch ( setUserList ( [ ] ) ) ;
278
275
handleUserEnteredRoom ( roomCode ) ;
279
- dispatch ( setRoomCode ( roomCode ) ) ; //?
276
+ dispatch ( setRoomCode ( roomCode ) ) ; // setting state to roomCode input
280
277
dispatch ( setUserJoined ( true ) ) ; //setting joined room to true for rendering leave room button
281
278
}
282
279
280
+ // leave room function
283
281
function leaveRoom ( ) {
284
- let socket = getSocket ( ) ;
282
+ let socket = getSocket ( ) ; // assigning socket var to get socket helper func
285
283
if ( socket ) {
286
284
socket . disconnect ( ) ; //disconnecting socket from server
287
- console . log ( socket ) ;
288
- socket = null ;
289
- console . log ( 'user leaves the room' ) ;
285
+ // console.log('user leaves the room');
290
286
}
291
287
//reset all state values
292
288
dispatch ( setRoomCode ( '' ) ) ;
@@ -303,13 +299,15 @@ const RoomsContainer = () => {
303
299
return userName . length === 0 || roomCode . length === 0 ;
304
300
}
305
301
306
- // Turning off the cursor live tracking on canvas with the press of a button.
302
+ // handle keydown enter to join room rather than click if room code has been entered
307
303
const handleKeyDown = ( e ) => {
308
304
if ( e . key === 'Enter' && e . target . id === 'filled-hidden-label-small' ) {
309
305
e . preventDefault ( ) ;
310
306
joinRoom ( ) ;
311
307
}
312
308
} ;
309
+
310
+ // color array for live cursor tracking - if more than six users join, cursor defaults to black currently
313
311
const userColors = [
314
312
'#FC00BD' ,
315
313
'#D0FC00' ,
@@ -460,7 +458,7 @@ const RoomsContainer = () => {
460
458
fontSize : 'smaller'
461
459
} }
462
460
>
463
- Note: Max Occupancy: 6 Users
461
+ For optimal collaboration experience, limit to 6 users per room
464
462
</ Typography >
465
463
</ Stack >
466
464
</ div >
0 commit comments