Skip to content

Commit 9a38ea4

Browse files
committed
Merge branch 'dev' into Rick/backend
2 parents 4a7a0aa + 7cce827 commit 9a38ea4

23 files changed

+645
-425
lines changed

app/src/components/App.tsx

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import { saveProject } from '../helperFunctions/projectGetSaveDel';
1717

1818
// Intermediary component to wrap main App component with higher order provider components
1919
export const App = (): JSX.Element => {
20-
const state = useSelector((store: RootState) => store.appState);
21-
20+
// const state = useSelector((store: RootState) => store.appState);
21+
2222
const dispatch = useDispatch();
2323
// checks if user is signed in as guest or actual user and changes loggedIn boolean accordingly
2424
useEffect(() => {
@@ -29,21 +29,21 @@ export const App = (): JSX.Element => {
2929

3030
// FOR LOCAL FORAGE: still trying to get this to work
3131

32-
// following useEffect runs on first mount
32+
// // following useEffect runs on first mount
3333
// useEffect(() => {
34-
// // console.log('cookies.get in App', Cookies.get())
34+
// console.log('cookies.get in App', Cookies.get());
3535
// // if user is a guest, see if a project exists in localforage and retrieve it
36-
// // v17 May not currently work yet
36+
// // v17: May not currently work yet
3737
// if (!state.isLoggedIn) {
3838
// localforage.getItem('guestProject').then((project) => {
3939
// // if project exists, use dispatch to set initial state to that project
40+
// console.log('local forage get project', project);
4041
// if (project) {
4142
// dispatch(setInitialState(project));
4243
// }
4344
// });
4445
// } else {
4546
// // otherwise if a user is logged in, use a fetch request to load user's projects from DB
46-
4747
// let userId;
4848
// if (Cookies.get('ssid')) {
4949
// userId = Cookies.get('ssid');
@@ -62,24 +62,26 @@ export const App = (): JSX.Element => {
6262
// });
6363
// }
6464
// }, []);
65+
66+
// // New project save configuration to optimize server load and minimize Ajax requests
6567
// useEffect(() => {
6668
// // provide config properties to legacy projects so new edits can be auto saved
67-
// // if (state.config === undefined) {
68-
// // state.config = { saveFlag: true, saveTimer: false };
69-
// // }
70-
// // New project save configuration to optimize server load and minimize Ajax requests
69+
// if (state.config === undefined) {
70+
// state.config = { saveFlag: true, saveTimer: false };
71+
// }
72+
7173
// if (state.config.saveFlag) {
72-
// // state.config.saveFlag = false;
73-
// // state.config.saveTimer = true;
74-
// // dispatch(configToggle())
74+
// state.config.saveFlag = false;
75+
// state.config.saveTimer = true;
76+
// //dispatch(configToggle());
7577

7678
// let userId;
7779
// if (Cookies.get('ssid')) {
7880
// userId = Cookies.get('ssid');
7981
// } else {
8082
// userId = window.localStorage.getItem('ssid');
8183
// }
82-
// // if (state.isLoggedIn === false) {
84+
8385
// if (!state.isLoggedIn) {
8486
// localforage.setItem('guestProject', state);
8587
// } else if (state.name !== '') {
@@ -95,18 +97,15 @@ export const App = (): JSX.Element => {
9597
// }
9698
// }, [state]);
9799

98-
99100
return (
100101
<div className="app">
101-
102-
<header
103-
style={{ height: '40px', width: '100%', backgroundColor: 'white' }}
104-
>
105-
ReacType
106-
</header>
102+
<header
103+
style={{ height: '40px', width: '100%', backgroundColor: 'white' }}
104+
>
105+
ReacType
106+
</header>
107107

108-
<AppContainer />
109-
108+
<AppContainer />
110109
</div>
111110
);
112111
};

app/src/components/ContextAPIManager/CreateTab/components/AddContextForm.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ const AddContextForm = ({
2525
const { allContext } = contextStore;
2626
const [btnDisabled, setBtnDisabled] = useState(false);
2727
const [open, setOpen] = useState(false);
28-
const { state } = useSelector((store: RootState) => ({
29-
state: store.appState
30-
}));
28+
// const { state } = useSelector((store: RootState) => ({
29+
// state: store.appState
30+
// }));
3131

3232
const color = 'white';
3333

app/src/components/ContextAPIManager/CreateTab/components/AddDataForm.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import TextField from '@mui/material/TextField';
33
import Button from '@mui/material/Button';
44
import Box from '@mui/material/Box';
55
import { Typography } from '@mui/material';
6-
import { useSelector } from 'react-redux';
7-
import { RootState } from '../../../../redux/store';
6+
// import { useSelector } from 'react-redux';
7+
// import { RootState } from '../../../../redux/store';
88

99
const AddDataForm = ({ handleClickInputData, currentContext }) => {
1010
//const [contextInput, setContextInput] = React.useState(null);

app/src/components/left/ElementsContainer.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import ComponentDrag from './ComponentDrag';
66
import DragDropPanel from './DragDropPanel';
77

88
import { deleteChild } from '../../redux/reducers/slice/appStateSlice';
9+
import { RootState } from '../../redux/store';
910

1011
// Left-hand portion of the app, where component options are displayed
1112
const ElementsContainer = (props): JSX.Element => {
1213
const [selectedTab, setSelectedTab] = React.useState('files');
1314

14-
const { contextParam, style } = useSelector((store) => ({
15+
const { contextParam, style } = useSelector((store: RootState) => ({
1516
contextParam: store.contextSlice,
1617
style: store.styleSlice
1718
}));

app/src/components/left/HTMLItem.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { RootState } from '../../redux/store';
99
import createModal from '../right/createModal';
1010
import makeStyles from '@mui/styles/makeStyles';
1111
import { useDrag } from 'react-dnd';
12-
import { useSelector } from 'react-redux';
12+
// import { useSelector } from 'react-redux';
1313

1414
const useStyles = makeStyles({
1515
HTMLPanelItem: {

app/src/components/left/RoomsContainer.tsx

Lines changed: 64 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -30,53 +30,74 @@ let socket;
3030
const { API_BASE_URL } = config;
3131
const RoomsContainer = () => {
3232
const dispatch = useDispatch();
33-
const { state, roomCode, userName, userList, userJoined } = useSelector(
33+
const { roomCode, userName, userList, userJoined } = useSelector(
3434
(store: RootState) => ({
35-
state: store.appState,
3635
roomCode: store.roomSlice.roomCode,
3736
userName: store.roomSlice.userName,
3837
userList: store.roomSlice.userList,
3938
userJoined: store.roomSlice.userJoined
4039
})
4140
);
42-
React.useEffect(() => {
43-
console.log('You Joined Room---front end:', roomCode);
44-
}, [roomCode]);
4541

46-
function initSocketConnection(roomCode) {
42+
function initSocketConnection(roomCode: string) {
4743
if (socket) socket.disconnect(); //edge case check if socket connection existed
4844

4945
socket = io(API_BASE_URL, {
46+
//establishing client and server connection
5047
transports: ['websocket']
5148
});
5249

50+
//connecting user to server
5351
socket.on('connect', () => {
54-
console.log(`You Connected With Id: ${socket.id}`);
55-
socket.emit('join-room', roomCode); // Join the room when connected
56-
console.log(`Your Nickname Is: ${userName}`);
57-
//passing current client nickname to server
58-
socket.emit('userJoined', userName, roomCode);
59-
//listening to back end for updating user list
60-
socket.on('updateUserList', (newUserList) => {
61-
dispatch(setUserList(Object.values(newUserList)));
62-
});
52+
socket.emit('joining', userName, roomCode);
53+
console.log(`${userName} Joined room ${roomCode}`);
54+
});
55+
56+
//send state from host to room when new user joins
57+
socket.on('requesting state from host', (callback) => {
58+
//getting state request from user from back end
59+
const newState = store.getState();
60+
callback(newState); //pull new state from host and send it to back end
61+
});
62+
63+
socket.on('server emitting state from host', (state, callback) => {
64+
//getting state from host once joined a room
65+
//dispatching new state to change user current state
66+
store.dispatch(allCooperativeState(state.appState));
67+
store.dispatch(codePreviewCooperative(state.codePreviewCooperative));
68+
store.dispatch(cooperativeStyle(state.styleSlice));
69+
callback({ status: 'confirmed' });
70+
});
71+
72+
//listening to back end for updating user list
73+
socket.on('updateUserList', (newUserList: object) => {
74+
dispatch(setUserList(Object.values(newUserList)));
6375
});
6476

6577
// receiving the message from the back end
66-
socket.on('receive message', (event) => {
67-
let currentStore: any = JSON.stringify(store.getState());
68-
// console.log('event ', event);
69-
if (currentStore !== event) {
70-
currentStore = JSON.parse(currentStore);
71-
event = JSON.parse(event);
72-
if (currentStore.appState !== event.appState) {
73-
store.dispatch(allCooperativeState(event.appState));
78+
socket.on('new state from back', (event) => {
79+
const currentStore = JSON.parse(JSON.stringify(store.getState()));
80+
const parsedEvent = JSON.parse(event);
81+
82+
const areStatesEqual = (stateA, stateB) =>
83+
JSON.stringify(stateA) === JSON.stringify(stateB);
84+
85+
if (!areStatesEqual(currentStore, parsedEvent)) {
86+
if (!areStatesEqual(currentStore.appState, parsedEvent.appState)) {
87+
store.dispatch(allCooperativeState(parsedEvent.appState));
88+
} else if (
89+
!areStatesEqual(
90+
currentStore.codePreviewSlice,
91+
parsedEvent.codePreviewCooperative
92+
)
93+
) {
94+
store.dispatch(
95+
codePreviewCooperative(parsedEvent.codePreviewCooperative)
96+
);
7497
} else if (
75-
currentStore.codePreviewSlice !== event.codePreviewCooperative
98+
!areStatesEqual(currentStore.styleSlice, parsedEvent.styleSlice)
7699
) {
77-
store.dispatch(codePreviewCooperative(event.codePreviewCooperative));
78-
} else if (currentStore.styleSlice !== event.styleSlice) {
79-
store.dispatch(cooperativeStyle(event.styleSlice));
100+
store.dispatch(cooperativeStyle(parsedEvent.styleSlice));
80101
}
81102
}
82103
});
@@ -88,50 +109,49 @@ const RoomsContainer = () => {
88109

89110
let previousState = store.getState();
90111
// sending info to backend whenever the redux store changes
112+
//handling state changes and send to server
91113
const handleStoreChange = debounce(() => {
92114
const newState = store.getState();
93115
const roomCode = newState.roomSlice.roomCode;
94116

95-
if (newState !== previousState) {
117+
if (JSON.stringify(newState) !== JSON.stringify(previousState)) {
96118
// Send the current state to the server
97-
socket.emit('custom-event', JSON.stringify(newState), roomCode);
119+
socket.emit('new state from front', JSON.stringify(newState), roomCode);
98120
previousState = newState;
99121
}
100122
}, 100);
101123

124+
//listening to changes from store from user, invoke handle store change.
102125
store.subscribe(() => {
103126
if (socket) {
104127
handleStoreChange();
105128
}
106129
});
107130

131+
//joining room function
108132
function joinRoom() {
109-
if (userList.length !== 0) setUserList([]); //edge case check if userList not empty.
133+
if (userList.length !== 0) dispatch(setUserList([])); //edge case check if userList not empty.
110134
handleUserEnteredRoom(roomCode); // Call handleUserEnteredRoom when joining a room
111135
dispatch(setRoomCode(roomCode));
112136
dispatch(setUserJoined(true)); //setting joined room to true for rendering leave room button
113137
}
114138

115139
function leaveRoom() {
116140
if (socket) {
117-
socket.emit('updateUserDisconnect', roomCode);
118-
socket.disconnect();
119-
} //disconnecting socket functionality
141+
socket.disconnect(); //disconnecting socket from server
142+
}
143+
//reset all state values
120144
dispatch(setRoomCode(''));
121145
dispatch(setUserName(''));
122146
dispatch(setUserList([]));
123147
dispatch(setUserJoined(false)); //setting joined to false so join button appear
124148
}
125149

126-
//checking if both text field have any input (not including spaces)
127-
function checkInputField(...inputs: any) {
128-
let userName: String = inputs[0].trim();
129-
let roomCode: String = inputs[1].trim();
130-
if (userName.length !== 0 && roomCode.length !== 0) {
131-
return false;
132-
} else {
133-
return true;
134-
}
150+
//checking empty input field (not including spaces)
151+
function checkInputField(...inputs) {
152+
let userName: string = inputs[0].trim();
153+
let roomCode: string = inputs[1].trim();
154+
return userName.length === 0 || roomCode.length === 0;
135155
}
136156

137157
return (
@@ -171,8 +191,7 @@ const RoomsContainer = () => {
171191
<Typography
172192
variant="body1"
173193
sx={{
174-
color: 'white', // Text color for the count
175-
borderRadius: 4 // Optional: Add rounded corners
194+
color: 'white' // Text color for the count
176195
}}
177196
>
178197
Users: {userList.length}
@@ -206,14 +225,13 @@ const RoomsContainer = () => {
206225
) : (
207226
//after joinning room
208227
<>
209-
<></>
210228
<TextField
211229
hiddenLabel={true}
212230
id="filled-hidden-label-small"
213231
variant="filled"
214232
size="small"
215233
value={userName}
216-
placeholder="Input nickname"
234+
placeholder="Input Nickname"
217235
onChange={(e) => dispatch(setUserName(e.target.value))}
218236
/>
219237
<TextField

app/src/components/right/createModal.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,33 @@ import React from 'react';
22
import SimpleModal from './SimpleModal';
33

44
type Props = {
5-
closeModal: any;
6-
primBtnAction: any;
7-
secBtnAction: any;
85
open: boolean;
9-
children: any;
106
message: string;
117
primBtnLabel: any;
128
secBtnLabel: any;
9+
primBtnAction: any;
10+
secBtnAction: any;
11+
children: any;
12+
closeModal: any;
1313
};
1414

1515
const createModal = ({
16+
open = true,
1617
message,
17-
closeModal,
1818
primBtnLabel,
19+
secBtnLabel = null,
1920
primBtnAction,
2021
secBtnAction = null,
21-
secBtnLabel = null,
2222
children = null,
23-
open = true
23+
closeModal
2424
}: Props) => (
2525
<SimpleModal
2626
open={open}
2727
message={message}
28-
secBtnLabel={secBtnLabel}
2928
primBtnLabel={primBtnLabel}
30-
secBtnAction={secBtnAction}
29+
secBtnLabel={secBtnLabel}
3130
primBtnAction={primBtnAction}
31+
secBtnAction={secBtnAction}
3232
closeModal={closeModal}
3333
>
3434
{children}

app/src/containers/MainContainer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const MainContainer = (props): JSX.Element => {
1717
(store: RootState) => store.appState.screenshotTrigger
1818
);
1919
const id: string = useSelector((store: RootState) => store.appState._id);
20-
const { style } = useSelector((store) => ({
20+
const { style } = useSelector((store: RootState) => ({
2121
style: store.styleSlice
2222
}));
2323
const containerRef: React.RefObject<HTMLDivElement> = useRef(null);

0 commit comments

Comments
 (0)