Skip to content

Commit 9be9fe8

Browse files
authored
Merge pull request #18 from oslabs-beta/cyrus-two-pass
Collab room password protected
2 parents f2c16d0 + 233a468 commit 9be9fe8

File tree

2 files changed

+195
-90
lines changed

2 files changed

+195
-90
lines changed

app/src/components/left/RoomsContainer.tsx

Lines changed: 114 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,15 @@ import {
5353
DeleteContextPayload,
5454
addComponentToContext
5555
} from '../../../src/redux/reducers/slice/contextReducer';
56+
import { useState } from 'react';
5657

5758
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+
5865
const dispatch = useDispatch();
5966
const roomCode = useSelector((store: RootState) => store.roomSlice.roomCode);
6067
const userName = useSelector((store: RootState) => store.roomSlice.userName);
@@ -68,7 +75,11 @@ const RoomsContainer = () => {
6875
);
6976
const messages = useSelector((store: RootState) => store.roomSlice.messages);
7077

71-
const initSocketConnection = (roomCode: string) => {
78+
const initSocketConnection = (
79+
roomCode: string,
80+
roomPassword: string,
81+
method: string
82+
) => {
7283
// helper function to create socket connection
7384
initializeSocket();
7485
// assign socket to result of helper function to return socket created
@@ -77,10 +88,31 @@ const RoomsContainer = () => {
7788
if (socket) {
7889
//run everytime when a client connects to server
7990
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();
82111
});
83112

113+
socket.on('room is already taken', () => {
114+
setIsCollabRoomTaken(true);
115+
});
84116
//If you are the host: send current state to server when a new user joins
85117
socket.on('requesting state from host', (callback) => {
86118
const newState = store.getState(); //pull the current state
@@ -275,23 +307,35 @@ const RoomsContainer = () => {
275307
}
276308
};
277309

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');
280316
};
281317

282-
const joinRoom = () => {
283-
if (userList.length !== 0) dispatch(setUserList([]));
284-
handleUserEnteredRoom(roomCode);
318+
const addNewUserToCollabRoom = () => {
285319
dispatch(setRoomCode(roomCode));
286320
dispatch(setPassword(roomPassword));
287321
dispatch(setUserJoined(true));
288322
};
289323

324+
const joinExistingCollabRoom = async () => {
325+
if (userList.length !== 0) {
326+
dispatch(setUserList([]));
327+
}
328+
329+
initSocketConnection(roomCode, joinedPasswordAttempt, 'JOIN');
330+
};
331+
290332
const leaveRoom = () => {
291333
let socket = getSocket();
334+
292335
if (socket) {
293336
socket.disconnect();
294337
}
338+
295339
dispatch(setRoomCode(''));
296340
dispatch(setUserName(''));
297341
dispatch(setUserList([]));
@@ -303,16 +347,18 @@ const RoomsContainer = () => {
303347
const checkInputField = (...inputs) => {
304348
let userName: string = inputs[0].trim();
305349
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+
);
307354
};
308355

309356
const handleKeyDown = (e) => {
310357
if (e.key === 'Enter' && e.target.id === 'filled-hidden-label-small') {
311358
e.preventDefault();
312-
joinRoom();
359+
createNewCollabRoom();
313360
}
314361
};
315-
316362
const userColors = [
317363
'#FC00BD',
318364
'#D0FC00',
@@ -333,22 +379,18 @@ const RoomsContainer = () => {
333379
margin: '0 auto 0 auto'
334380
}}
335381
>
336-
{' '}
337382
<Typography variant="h5" color={'#f2fbf8'}>
338383
Live Room: {roomCode}
339384
</Typography>
340385
{userJoined ? (
341386
<>
342-
<Typography
343-
variant="h6"
344-
color={userColors[userList.indexOf(userName)]}
345-
>
387+
<Typography variant="h6" color="#898a8b">
346388
Nickname: {userName}
347389
</Typography>
348390
<Typography
349391
variant="body1"
350392
sx={{
351-
color: 'white'
393+
color: '#898a8b'
352394
}}
353395
>
354396
Users: {userList.length}
@@ -359,11 +401,9 @@ const RoomsContainer = () => {
359401
height: 300,
360402
maxWidth: 200,
361403
bgcolor: '#333333',
362-
border: '3px solid #f2fbf8',
363404
borderRadius: '5%',
364405
display: 'flex',
365406
flexDirection: 'column',
366-
alignItems: 'center',
367407
overflow: 'auto',
368408
color: 'white'
369409
}}
@@ -387,7 +427,7 @@ const RoomsContainer = () => {
387427
>
388428
<ListItemText
389429
primary={`${index + 1}. ${
390-
index === 0 ? `${user} (host)` : user
430+
index === 0 ? `${user.userName} (host)` : user.userName
391431
}`}
392432
style={{ color: userColors[userList.indexOf(user)] }}
393433
/>
@@ -402,13 +442,12 @@ const RoomsContainer = () => {
402442
backgroundColor: '#f2fbf8',
403443
color: '#092a26',
404444
'&:hover': {
405-
backgroundColor: '#354e9c',
445+
backgroundColor: '#a5ead6',
406446
borderColor: '#0062cc'
407447
}
408448
}}
409449
>
410-
{' '}
411-
Leave Room{' '}
450+
Leave Room
412451
</Button>
413452
</>
414453
) : (
@@ -424,6 +463,7 @@ const RoomsContainer = () => {
424463
onChange={(e) => dispatch(setUserName(e.target.value))}
425464
/>
426465
<TextField
466+
error={isCollabRoomTaken}
427467
fullWidth
428468
hiddenLabel={true}
429469
id="filled-hidden-label-small"
@@ -434,33 +474,68 @@ const RoomsContainer = () => {
434474
onChange={(e) => dispatch(setRoomCode(e.target.value))}
435475
className="enterRoomInput"
436476
onKeyDown={handleKeyDown}
477+
helperText={isCollabRoomTaken ? 'Room name already taken' : ''}
437478
/>
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+
449509
<Button
450510
variant="contained"
451-
disabled={checkInputField(userName, roomCode)}
511+
disabled={checkInputField(userName, roomCode, roomCode)}
452512
fullWidth
453-
onClick={() => joinRoom()}
513+
onClick={(e) =>
514+
isJoinCallabRoom
515+
? joinExistingCollabRoom()
516+
: createNewCollabRoom()
517+
}
454518
sx={{
455519
backgroundColor: '#e9e9e9',
456-
color: '#354e9c',
520+
color: '#253b80',
457521
'&:hover': {
458-
backgroundColor: '#354e9c'
522+
backgroundColor: '#99d7f2'
459523
}
460524
}}
461525
>
462-
Join Room
526+
{isJoinCallabRoom ? 'Join' : 'Start'}
463527
</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>
464539
</>
465540
)}
466541
</Stack>

0 commit comments

Comments
 (0)