@@ -35,14 +35,28 @@ const RoomsContainer = () => {
35
35
roomCode : store . roomSlice . roomCode ,
36
36
userName : store . roomSlice . userName ,
37
37
userList : store . roomSlice . userList ,
38
+ //userIsHost: store.roomSlice.userIsHost,
38
39
userJoined : store . roomSlice . userJoined
39
40
} )
40
41
) ;
42
+ // const [userIsHost, setUserIsHost] = useState(false);
41
43
42
44
React . useEffect ( ( ) => {
43
45
console . log ( 'You Joined Room---:' , roomCode ) ;
44
46
} , [ roomCode ] ) ;
45
47
48
+ // React.useEffect(() => {
49
+ // console.log('userName :', userName);
50
+ // if (userName === userList[0]) {
51
+ // console.log('setting isHost to true');
52
+ // setUserIsHost(true);
53
+ // } else {
54
+ // setUserIsHost(false);
55
+ // }
56
+ // console.log('User list updated:', userList);
57
+ // console.log('userList[0]-------', userList[0]);
58
+ // }, [userList]);
59
+
46
60
function initSocketConnection ( roomCode ) {
47
61
if ( socket ) socket . disconnect ( ) ; //edge case check if socket connection existed
48
62
@@ -57,51 +71,38 @@ const RoomsContainer = () => {
57
71
socket . emit ( 'joining' , userName , roomCode ) ;
58
72
} ) ;
59
73
60
- // if host, send state to server
61
- socket . on ( 'requesting state from host' , ( roomCode ) => {
62
- // if (userName === userList[0]) {
63
- // console.log('host is sending state');
64
- // const newState = store.getState();
65
- // socket.emit('state from host', JSON.stringify(newState), roomCode);
66
- // }
67
-
68
- console . log ( 'receiving room code from new user------' , roomCode ) ;
69
- socket . emit ( 'state from host' , roomCode ) ;
70
- } ) ;
71
-
72
74
//listening to back end for updating user list
73
- socket . on ( 'updateUserList' , ( newUserList ) => {
74
- console . log ( 'received user list from back:' , newUserList ) ;
75
+ socket . on ( 'updateUserList' , ( newUserList : object ) => {
75
76
dispatch ( setUserList ( Object . values ( newUserList ) ) ) ;
76
- console . log ( 'object values new user list' , Object . values ( newUserList ) ) ;
77
- console . log ( 'client user list updated:' , userList ) ;
78
77
} ) ;
79
-
78
+ //send state from host to room when new user joins
79
+ socket . on ( 'requesting state from host' , ( ) => {
80
+ console . log ( 'front received request for host state' ) ;
81
+ console . log ( `${ userName } is host` ) ;
82
+ console . log ( 'host is sending state' ) ;
83
+ const newState = store . getState ( ) ;
84
+ socket . emit ( 'state from host' , JSON . stringify ( newState ) ) ;
85
+ } ) ;
80
86
// receiving the message from the back end
81
87
socket . on ( 'new state from back' , ( event ) => {
82
- console . log ( 'front receiving new state' ) ;
83
88
let currentStore : any = JSON . stringify ( store . getState ( ) ) ;
84
- // console.log('event ', event);
85
89
if ( currentStore !== event ) {
86
90
currentStore = JSON . parse ( currentStore ) ;
87
91
event = JSON . parse ( event ) ;
88
92
if (
89
93
JSON . stringify ( currentStore . appState ) !==
90
94
JSON . stringify ( event . appState )
91
95
) {
92
- console . log ( 'updating app state' ) ;
93
96
store . dispatch ( allCooperativeState ( event . appState ) ) ;
94
97
} else if (
95
98
JSON . stringify ( currentStore . codePreviewSlice ) !==
96
99
JSON . stringify ( event . codePreviewCooperative )
97
100
) {
98
- console . log ( 'updating code preview' ) ;
99
101
store . dispatch ( codePreviewCooperative ( event . codePreviewCooperative ) ) ;
100
102
} else if (
101
103
JSON . stringify ( currentStore . styleSlice ) !==
102
104
JSON . stringify ( event . styleSlice )
103
105
) {
104
- console . log ( 'updating style' ) ;
105
106
store . dispatch ( cooperativeStyle ( event . styleSlice ) ) ;
106
107
}
107
108
}
@@ -120,15 +121,13 @@ const RoomsContainer = () => {
120
121
121
122
if ( JSON . stringify ( newState ) !== JSON . stringify ( previousState ) ) {
122
123
// Send the current state to the server
123
- console . log ( 'front emitting new state' ) ;
124
124
socket . emit ( 'new state from front' , JSON . stringify ( newState ) , roomCode ) ;
125
125
previousState = newState ;
126
126
}
127
127
} , 100 ) ;
128
128
129
129
store . subscribe ( ( ) => {
130
130
if ( socket ) {
131
- //console.log('handling store change');
132
131
handleStoreChange ( ) ;
133
132
}
134
133
} ) ;
@@ -193,8 +192,7 @@ const RoomsContainer = () => {
193
192
< Typography
194
193
variant = "body1"
195
194
sx = { {
196
- color : 'white' , // Text color for the count
197
- borderRadius : 4 // Optional: Add rounded corners
195
+ color : 'white' // Text color for the count
198
196
} }
199
197
>
200
198
Users: { userList . length }
0 commit comments