@@ -5,10 +5,13 @@ import List from '@mui/material/List';
5
5
import ListItem from '@mui/material/ListItem' ;
6
6
import ListItemText from '@mui/material/ListItemText' ;
7
7
import Button from '@mui/material/Button' ;
8
- import React , { useState } from 'react' ;
8
+ import React , { useEffect } from 'react' ;
9
9
import { RootState } from '../../redux/store' ;
10
10
import TextField from '@mui/material/TextField' ;
11
- import { allCooperativeState } from '../../redux/reducers/slice/appStateSlice' ;
11
+ import {
12
+ allCooperativeState ,
13
+ addChild
14
+ } from '../../redux/reducers/slice/appStateSlice' ;
12
15
import {
13
16
setRoomCode ,
14
17
setUserName ,
@@ -28,6 +31,7 @@ import debounce from '../../../../node_modules/lodash/debounce.js';
28
31
// // Part - join room and room code functionality
29
32
let socket ;
30
33
const { API_BASE_URL } = config ;
34
+
31
35
const RoomsContainer = ( ) => {
32
36
const dispatch = useDispatch ( ) ;
33
37
//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.
@@ -60,87 +64,98 @@ const RoomsContainer = () => {
60
64
//If you are the new user: receive the state from the host
61
65
socket . on ( 'server emitting state from host' , ( state , callback ) => {
62
66
//dispatching new state to change user current state
63
- console . log ( 'state recieved by new joiner :' , state ) ;
67
+ console . log ( 'state recieved by new join :' , state ) ;
64
68
store . dispatch ( allCooperativeState ( state . appState ) ) ;
65
69
store . dispatch ( codePreviewCooperative ( state . codePreviewCooperative ) ) ;
66
70
store . dispatch ( cooperativeStyle ( state . styleSlice ) ) ;
67
- callback ( { status : 'confirmed' } ) ;
71
+ callback ( { status : 'confirmed' } ) ;
68
72
} ) ;
69
73
70
74
// update user list when there's a change: new join or leave the room
71
75
socket . on ( 'updateUserList' , ( newUserList : object ) => {
72
76
dispatch ( setUserList ( Object . values ( newUserList ) ) ) ;
73
77
} ) ;
74
78
75
- // receive the new state from the server and dispatch action creators to update state
76
- socket . on ( 'new state from back' , ( event ) => {
77
- const currentStore = JSON . parse ( JSON . stringify ( store . getState ( ) ) ) ;
78
- const newState = JSON . parse ( event ) ;
79
+ // // receive the new state from the server and dispatch action creators to update state
80
+ // socket.on('new state from back', (event) => {
81
+ // const currentStore = JSON.parse(JSON.stringify(store.getState()));
82
+ // const newState = JSON.parse(event);
79
83
80
- const areStatesEqual = ( stateA , stateB ) =>
81
- JSON . stringify ( stateA ) === JSON . stringify ( stateB ) ;
84
+ // const areStatesEqual = (stateA, stateB) =>
85
+ // JSON.stringify(stateA) === JSON.stringify(stateB);
82
86
83
- //checking if current state are equal to the state being sent from server
84
- if ( ! areStatesEqual ( currentStore , newState ) ) {
85
- if ( ! areStatesEqual ( currentStore . appState , newState . appState ) ) {
86
- store . dispatch ( allCooperativeState ( newState . appState ) ) ;
87
- } else if (
88
- ! areStatesEqual (
89
- currentStore . codePreviewSlice ,
90
- newState . codePreviewCooperative
91
- )
92
- ) {
93
- store . dispatch (
94
- codePreviewCooperative ( newState . codePreviewCooperative )
95
- ) ;
96
- } else if (
97
- ! areStatesEqual ( currentStore . styleSlice , newState . styleSlice )
98
- ) {
99
- store . dispatch ( cooperativeStyle ( newState . styleSlice ) ) ;
100
- }
101
- }
102
- } ) ;
87
+ // //checking if current state are equal to the state being sent from server
88
+ // if (!areStatesEqual(currentStore, newState)) {
89
+ // if (!areStatesEqual(currentStore.appState, newState.appState)) {
90
+ // store.dispatch(allCooperativeState(newState.appState));
91
+ // } else if (
92
+ // !areStatesEqual(
93
+ // currentStore.codePreviewSlice,
94
+ // newState.codePreviewCooperative
95
+ // )
96
+ // ) {
97
+ // store.dispatch(
98
+ // codePreviewCooperative(newState.codePreviewCooperative)
99
+ // );
100
+ // } else if (
101
+ // !areStatesEqual(currentStore.styleSlice, newState.styleSlice)
102
+ // ) {
103
+ // store.dispatch(cooperativeStyle(newState.styleSlice));
104
+ // }
105
+ // }
106
+ // });
103
107
}
104
108
109
+ // let previousState = store.getState();
110
+ // // sending info to backend whenever the redux store changes
111
+ // //handling state changes and send to server
112
+
113
+ // const findStateDiff = (prevState, newState) => {
114
+ // const changes = {};
115
+ // for (let key in newState) {
116
+ // if (JSON.stringify(newState[key]) !== JSON.stringify(prevState[key])) {
117
+ // changes[key] = newState[key];
118
+ // }
119
+ // }
120
+ // return changes;
121
+ // };
122
+
123
+ // const handleStoreChange = debounce(() => {
124
+ // const newState = store.getState();
125
+ // const roomCode = newState.roomSlice.roomCode;
126
+ // const changes = findStateDiff(previousState, newState);
127
+
128
+ // if (Object.keys(changes).length > 0) {
129
+ // // Send the current state to the server
130
+ // console.log('newState:', newState);
131
+ // console.log('changes:', changes);
132
+ // socket.emit('new state from front', JSON.stringify(changes), roomCode);
133
+ // //re-assgin previousState to be the newState
134
+ // previousState = newState;
135
+ // }
136
+ // }, 100);
137
+
138
+ // //listening to changes from store by users, whenever the store's state changes, invoke handleStoreChange function
139
+ // store.subscribe(() => {
140
+ // if (socket) {
141
+ // handleStoreChange();
142
+ // }
143
+ // });
144
+
105
145
function handleUserEnteredRoom ( roomCode ) {
106
146
initSocketConnection ( roomCode ) ;
107
147
}
108
148
109
- let previousState = store . getState ( ) ;
110
- // sending info to backend whenever the redux store changes
111
- //handling state changes and send to server
149
+ //-----------------------
112
150
113
- const findStateDiff = ( prevState , newState ) => {
114
- const changes = { } ;
115
- for ( let key in newState ) {
116
- if ( JSON . stringify ( newState [ key ] ) !== JSON . stringify ( prevState [ key ] ) ) {
117
- changes [ key ] = newState [ key ] ;
118
- }
119
- }
120
- return changes ;
121
- } ;
122
-
123
- const handleStoreChange = debounce ( ( ) => {
124
- const newState = store . getState ( ) ;
125
- const roomCode = newState . roomSlice . roomCode ;
126
- const changes = findStateDiff ( previousState , newState ) ;
127
-
128
- if ( Object . keys ( changes ) . length > 0 ) {
129
- // Send the current state to the server
130
- console . log ( 'newState:' , newState ) ;
131
- console . log ( 'changes:' , changes ) ;
132
- socket . emit ( 'new state from front' , JSON . stringify ( changes ) , roomCode ) ;
133
- //re-assgin previousState to be the newState
134
- previousState = newState ;
135
- }
136
- } , 100 ) ;
151
+ if ( socket ) {
152
+ socket . on ( 'child data from back' , ( childData : string ) => {
153
+ console . log ( 'child data received by users' , JSON . parse ( childData ) ) ;
154
+ store . dispatch ( addChild ( JSON . parse ( childData ) ) ) ;
155
+ } ) ;
156
+ }
137
157
138
- //listening to changes from store by users, whenever the store's state changes, invoke handleStoreChange function
139
- store . subscribe ( ( ) => {
140
- if ( socket ) {
141
- handleStoreChange ( ) ;
142
- }
143
- } ) ;
158
+ //-----------------------
144
159
145
160
//joining room function
146
161
function joinRoom ( ) {
0 commit comments