@@ -53,8 +53,15 @@ import {
53
53
DeleteContextPayload ,
54
54
addComponentToContext
55
55
} from '../../../src/redux/reducers/slice/contextReducer' ;
56
+ import { useState } from 'react' ;
56
57
57
58
const RoomsContainer = ( ) => {
59
+ const [ isJoinCallabRoom , setIsJoinCollabRoom ] = useState ( false ) ;
60
+ const [ joinedPasswordAttempt , setJoinedPasswordAttempt ] = useState ( '' ) ;
61
+ const [ isPasswordAttemptIncorrect , setIsPasswordAttemptIncorrect ] =
62
+ useState ( true ) ;
63
+ const [ isCollabRoomTaken , setIsCollabRoomTaken ] = useState ( false ) ;
64
+
58
65
const dispatch = useDispatch ( ) ;
59
66
const roomCode = useSelector ( ( store : RootState ) => store . roomSlice . roomCode ) ;
60
67
const userName = useSelector ( ( store : RootState ) => store . roomSlice . userName ) ;
@@ -68,7 +75,11 @@ const RoomsContainer = () => {
68
75
) ;
69
76
const messages = useSelector ( ( store : RootState ) => store . roomSlice . messages ) ;
70
77
71
- const initSocketConnection = ( roomCode : string ) => {
78
+ const initSocketConnection = (
79
+ roomCode : string ,
80
+ roomPassword : string ,
81
+ method : string
82
+ ) => {
72
83
// helper function to create socket connection
73
84
initializeSocket ( ) ;
74
85
// assign socket to result of helper function to return socket created
@@ -77,10 +88,31 @@ const RoomsContainer = () => {
77
88
if ( socket ) {
78
89
//run everytime when a client connects to server
79
90
socket . on ( 'connect' , ( ) => {
80
- socket . emit ( 'joining' , userName , roomCode ) ;
81
- // console.log(`${userName} Joined room ${roomCode} from RoomsContainer`);
91
+ socket . emit (
92
+ 'creating a room' ,
93
+ userName ,
94
+ roomCode ,
95
+ roomPassword ,
96
+ method
97
+ ) ;
98
+ } ) ;
99
+
100
+ socket . on ( 'wrong password' , ( ) => {
101
+ setIsPasswordAttemptIncorrect ( false ) ;
102
+ } ) ;
103
+
104
+ socket . on ( 'correct password' , ( ) => {
105
+ setIsPasswordAttemptIncorrect ( true ) ;
106
+ addNewUserToCollabRoom ( ) ;
107
+ } ) ;
108
+
109
+ socket . on ( 'user created a new room' , ( ) => {
110
+ addNewUserToCollabRoom ( ) ;
82
111
} ) ;
83
112
113
+ socket . on ( 'room is already taken' , ( ) => {
114
+ setIsCollabRoomTaken ( true ) ;
115
+ } ) ;
84
116
//If you are the host: send current state to server when a new user joins
85
117
socket . on ( 'requesting state from host' , ( callback ) => {
86
118
const newState = store . getState ( ) ; //pull the current state
@@ -275,23 +307,35 @@ const RoomsContainer = () => {
275
307
}
276
308
} ;
277
309
278
- const handleUserEnteredRoom = ( roomCode ) => {
279
- initSocketConnection ( roomCode ) ;
310
+ const createNewCollabRoom = ( ) => {
311
+ if ( userList . length !== 0 ) {
312
+ dispatch ( setUserList ( [ ] ) ) ;
313
+ }
314
+
315
+ initSocketConnection ( roomCode , roomPassword , 'CREATE' ) ;
280
316
} ;
281
317
282
- const joinRoom = ( ) => {
283
- if ( userList . length !== 0 ) dispatch ( setUserList ( [ ] ) ) ;
284
- handleUserEnteredRoom ( roomCode ) ;
318
+ const addNewUserToCollabRoom = ( ) => {
285
319
dispatch ( setRoomCode ( roomCode ) ) ;
286
320
dispatch ( setPassword ( roomPassword ) ) ;
287
321
dispatch ( setUserJoined ( true ) ) ;
288
322
} ;
289
323
324
+ const joinExistingCollabRoom = async ( ) => {
325
+ if ( userList . length !== 0 ) {
326
+ dispatch ( setUserList ( [ ] ) ) ;
327
+ }
328
+
329
+ initSocketConnection ( roomCode , joinedPasswordAttempt , 'JOIN' ) ;
330
+ } ;
331
+
290
332
const leaveRoom = ( ) => {
291
333
let socket = getSocket ( ) ;
334
+
292
335
if ( socket ) {
293
336
socket . disconnect ( ) ;
294
337
}
338
+
295
339
dispatch ( setRoomCode ( '' ) ) ;
296
340
dispatch ( setUserName ( '' ) ) ;
297
341
dispatch ( setUserList ( [ ] ) ) ;
@@ -303,16 +347,18 @@ const RoomsContainer = () => {
303
347
const checkInputField = ( ...inputs ) => {
304
348
let userName : string = inputs [ 0 ] . trim ( ) ;
305
349
let roomCode : string = inputs [ 1 ] . trim ( ) ;
306
- return userName . length === 0 || roomCode . length === 0 ;
350
+ let password : string = inputs [ 2 ] . trim ( ) ;
351
+ return (
352
+ userName . length === 0 || roomCode . length === 0 || password . length === 0
353
+ ) ;
307
354
} ;
308
355
309
356
const handleKeyDown = ( e ) => {
310
357
if ( e . key === 'Enter' && e . target . id === 'filled-hidden-label-small' ) {
311
358
e . preventDefault ( ) ;
312
- joinRoom ( ) ;
359
+ createNewCollabRoom ( ) ;
313
360
}
314
361
} ;
315
-
316
362
const userColors = [
317
363
'#FC00BD' ,
318
364
'#D0FC00' ,
@@ -333,22 +379,18 @@ const RoomsContainer = () => {
333
379
margin : '0 auto 0 auto'
334
380
} }
335
381
>
336
- { ' ' }
337
382
< Typography variant = "h5" color = { '#f2fbf8' } >
338
383
Live Room: { roomCode }
339
384
</ Typography >
340
385
{ userJoined ? (
341
386
< >
342
- < Typography
343
- variant = "h6"
344
- color = { userColors [ userList . indexOf ( userName ) ] }
345
- >
387
+ < Typography variant = "h6" color = "#898a8b" >
346
388
Nickname: { userName }
347
389
</ Typography >
348
390
< Typography
349
391
variant = "body1"
350
392
sx = { {
351
- color : 'white '
393
+ color : '#898a8b '
352
394
} }
353
395
>
354
396
Users: { userList . length }
@@ -359,11 +401,9 @@ const RoomsContainer = () => {
359
401
height : 300 ,
360
402
maxWidth : 200 ,
361
403
bgcolor : '#333333' ,
362
- border : '3px solid #f2fbf8' ,
363
404
borderRadius : '5%' ,
364
405
display : 'flex' ,
365
406
flexDirection : 'column' ,
366
- alignItems : 'center' ,
367
407
overflow : 'auto' ,
368
408
color : 'white'
369
409
} }
@@ -387,7 +427,7 @@ const RoomsContainer = () => {
387
427
>
388
428
< ListItemText
389
429
primary = { `${ index + 1 } . ${
390
- index === 0 ? `${ user } (host)` : user
430
+ index === 0 ? `${ user . userName } (host)` : user . userName
391
431
} `}
392
432
style = { { color : userColors [ userList . indexOf ( user ) ] } }
393
433
/>
@@ -402,13 +442,12 @@ const RoomsContainer = () => {
402
442
backgroundColor : '#f2fbf8' ,
403
443
color : '#092a26' ,
404
444
'&:hover' : {
405
- backgroundColor : '#354e9c ' ,
445
+ backgroundColor : '#a5ead6 ' ,
406
446
borderColor : '#0062cc'
407
447
}
408
448
} }
409
449
>
410
- { ' ' }
411
- Leave Room{ ' ' }
450
+ Leave Room
412
451
</ Button >
413
452
</ >
414
453
) : (
@@ -424,6 +463,7 @@ const RoomsContainer = () => {
424
463
onChange = { ( e ) => dispatch ( setUserName ( e . target . value ) ) }
425
464
/>
426
465
< TextField
466
+ error = { isCollabRoomTaken }
427
467
fullWidth
428
468
hiddenLabel = { true }
429
469
id = "filled-hidden-label-small"
@@ -434,33 +474,68 @@ const RoomsContainer = () => {
434
474
onChange = { ( e ) => dispatch ( setRoomCode ( e . target . value ) ) }
435
475
className = "enterRoomInput"
436
476
onKeyDown = { handleKeyDown }
477
+ helperText = { isCollabRoomTaken ? 'Room name already taken' : '' }
437
478
/>
438
- < TextField
439
- fullWidth
440
- hiddenLabel = { true }
441
- id = "filled-hidden-label-small"
442
- variant = "standard"
443
- size = "small"
444
- value = { roomCode }
445
- placeholder = "Password"
446
- onChange = { ( e ) => dispatch ( setPassword ( e . target . value ) ) }
447
- // className="enterRoomInput"
448
- />
479
+ { isJoinCallabRoom ? (
480
+ < TextField
481
+ error = { isPasswordAttemptIncorrect === false }
482
+ fullWidth
483
+ hiddenLabel = { true }
484
+ id = "filled-hidden-label-small"
485
+ variant = "standard"
486
+ size = "small"
487
+ value = { joinedPasswordAttempt }
488
+ placeholder = "Password"
489
+ helperText = {
490
+ isPasswordAttemptIncorrect === false
491
+ ? 'Incorrect password.'
492
+ : ''
493
+ }
494
+ onChange = { ( e ) => setJoinedPasswordAttempt ( e . target . value ) }
495
+ />
496
+ ) : (
497
+ < TextField
498
+ fullWidth
499
+ hiddenLabel = { true }
500
+ id = "filled-hidden-label-small"
501
+ variant = "standard"
502
+ size = "small"
503
+ value = { roomPassword }
504
+ placeholder = "Password"
505
+ onChange = { ( e ) => dispatch ( setPassword ( e . target . value ) ) }
506
+ />
507
+ ) }
508
+
449
509
< Button
450
510
variant = "contained"
451
- disabled = { checkInputField ( userName , roomCode ) }
511
+ disabled = { checkInputField ( userName , roomCode , roomCode ) }
452
512
fullWidth
453
- onClick = { ( ) => joinRoom ( ) }
513
+ onClick = { ( e ) =>
514
+ isJoinCallabRoom
515
+ ? joinExistingCollabRoom ( )
516
+ : createNewCollabRoom ( )
517
+ }
454
518
sx = { {
455
519
backgroundColor : '#e9e9e9' ,
456
- color : '#354e9c ' ,
520
+ color : '#253b80 ' ,
457
521
'&:hover' : {
458
- backgroundColor : '#354e9c '
522
+ backgroundColor : '#99d7f2 '
459
523
}
460
524
} }
461
525
>
462
- Join Room
526
+ { isJoinCallabRoom ? ' Join' : 'Start' }
463
527
</ Button >
528
+ < Typography
529
+ onClick = { ( ) => setIsJoinCollabRoom ( ! isJoinCallabRoom ) }
530
+ sx = { {
531
+ color : 'grey' ,
532
+ '&:hover' : {
533
+ textDecoration : 'underline'
534
+ }
535
+ } }
536
+ >
537
+ { isJoinCallabRoom ? 'Start a new room' : 'Join a room' }
538
+ </ Typography >
464
539
</ >
465
540
) }
466
541
</ Stack >
0 commit comments