Skip to content

Commit 2496eb0

Browse files
authored
Merge pull request #26 from oslabs-beta/Yoheze/websockets
Yoheze/websockets
2 parents 042caf9 + a60bc0c commit 2496eb0

File tree

12 files changed

+83
-16
lines changed

12 files changed

+83
-16
lines changed

app/src/Dashboard/NavbarDash.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const sortMethods = ['RATING', 'DATE', 'USER'];
4646
// Drop down menu button for SORT PROJECTS
4747
const StyledMenu = withStyles({
4848
paper: {
49-
border: '1px solid #d3d4d5'
49+
border: '1px solid #d3d4d5',
5050
}
5151
})(props => (
5252
<Menu

app/src/components/main/Canvas.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ function Canvas(props): JSX.Element {
131131

132132
const canvasStyle = combineStyles(defaultCanvasStyle, currentComponent.style);
133133
const darkCombinedCanvasStyle = combineStyles(darkCanvasStyle, currentComponent.style);
134-
console.log('canvas comp', currentComponent)
135134
return (
136135
<div className={'componentContainer'} ref={drop} style={!isDarkMode ? canvasStyle : darkCombinedCanvasStyle} onClick={onClickHandler}>
137136
{renderChildren(currentComponent.children)}

app/src/components/right/DeleteProjects.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ export default function ProjectsFolder() {
109109
color="primary"
110110
onClick={handleClickOpen}
111111
endIcon={<DeleteRoundedIcon />}
112+
sx={{fontSize: '9px'}}
112113
>
113114
Delete Project
114115
</Button>

app/src/components/right/OpenProjects.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export default function ProjectsFolder(props) {
9999
id="openProject"
100100
onClick={handleClickOpen}
101101
endIcon={<FolderOpenIcon />}
102+
sx={{fontSize: '9px'}}
102103
>
103104
Open Project
104105
</Button>

app/src/components/right/SaveProjectButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const dispatch = useDispatch();
6464
<Button
6565
color="primary"
6666
onClick={handleClickOpen}
67-
endIcon={<SaveOutlinedIcon />}
67+
endIcon={<SaveOutlinedIcon />} sx={{fontSize: '9px'}}
6868
>
6969
SAVE PROJECT AS
7070
</Button>

app/src/components/top/NavBar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const NavBar = (props) => {
2121
onMouseOver={()=>setDropMenu(true)}>
2222
<path d="M7.068.727c.243-.97 1.62-.97 1.864 0l.071.286a.96.96 0 0 0 1.622.434l.205-.211c.695-.719 1.888-.03 1.613.931l-.08.284a.96.96 0 0 0 1.187 1.187l.283-.081c.96-.275 1.65.918.931 1.613l-.211.205a.96.96 0 0 0 .434 1.622l.286.071c.97.243.97 1.62 0 1.864l-.286.071a.96.96 0 0 0-.434 1.622l.211.205c.719.695.03 1.888-.931 1.613l-.284-.08a.96.96 0 0 0-1.187 1.187l.081.283c.275.96-.918 1.65-1.613.931l-.205-.211a.96.96 0 0 0-1.622.434l-.071.286c-.243.97-1.62.97-1.864 0l-.071-.286a.96.96 0 0 0-1.622-.434l-.205.211c-.695.719-1.888.03-1.613-.931l.08-.284a.96.96 0 0 0-1.186-1.187l-.284.081c-.96.275-1.65-.918-.931-1.613l.211-.205a.96.96 0 0 0-.434-1.622l-.286-.071c-.97-.243-.97-1.62 0-1.864l.286-.071a.96.96 0 0 0 .434-1.622l-.211-.205c-.719-.695-.03-1.888.931-1.613l.284.08a.96.96 0 0 0 1.187-1.186l-.081-.284c-.275-.96.918-1.65 1.613-.931l.205.211a.96.96 0 0 0 1.622-.434l.071-.286zM12.973 8.5H8.25l-2.834 3.779A4.998 4.998 0 0 0 12.973 8.5zm0-1a4.998 4.998 0 0 0-7.557-3.779l2.834 3.78h4.723zM5.048 3.967c-.03.021-.058.043-.087.065l.087-.065zm-.431.355A4.984 4.984 0 0 0 3.002 8c0 1.455.622 2.765 1.615 3.678L7.375 8 4.617 4.322zm.344 7.646.087.065-.087-.065z"/>
2323
</svg>
24-
{dropMenu && <NavbarDropDown></NavbarDropDown>}
24+
<NavbarDropDown dropMenu={dropMenu}></NavbarDropDown>
2525
</div>
2626
</nav>
2727
)

app/src/components/top/NavBarButtons.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import SaveProjectButton from '../right/SaveProjectButton.tsx';
1818
import ProjectsFolder from '../right/OpenProjects.tsx';
1919
import DeleteProjects from '../right/DeleteProjects.tsx';
2020
import Menu from '@mui/material/Menu';
21+
import { io } from 'socket.io-client'
22+
import { changeRoom } from '../../redux/reducers/slice/roomCodeSlice';
2123

2224
const useStyles = makeStyles((theme) =>
2325
createStyles({
@@ -35,14 +37,16 @@ const useStyles = makeStyles((theme) =>
3537
},
3638
manageProject: {
3739
display: 'flex',
38-
justifyContent: 'center'
40+
justifyContent: 'center',
41+
width: '100px',
42+
overflow: 'none',
3943
}
4044
})
4145
);
4246

4347
const StyledMenu = withStyles({
4448
paper: {
45-
border: '1px solid #d3d4d5'
49+
border: '1px solid #d3d4d5',
4650
}
4751
})(props => (
4852
<Menu
@@ -70,16 +74,21 @@ const StyledMenu = withStyles({
7074
}
7175
}))(MenuItem);
7276

73-
function navbarDropDown () {
77+
78+
// where the main function starts //
79+
function navbarDropDown (props) {
7480

7581
const dispatch = useDispatch();
7682
const [modal, setModal] = React.useState(null);
7783
const [anchorEl, setAnchorEl] = React.useState(null);
84+
const [roomCode, setRoomCode] = React.useState('');
85+
const [confirmRoom, setConfirmRoom] = React.useState('')
7886
const classes = useStyles();
7987

80-
const { isDarkMode, state } = useSelector(store=>({
88+
const { isDarkMode, state, joinedRoom } = useSelector(store=>({
8189
isDarkMode: store.darkMode.isDarkMode,
8290
state: store.appState,
91+
joinedRoom: store.roomCodeSlice.roomCode
8392
}
8493
))
8594
const closeModal = () => setModal('');
@@ -132,13 +141,22 @@ function navbarDropDown () {
132141
dispatch(toggleDarkMode());
133142
// Add your logic to update the style and theme based on darkMode
134143
isDarkMode ? dispatch(setStyle(null)) : dispatch(setStyle({ backgroundColor: '#21262c' }));
135-
props.setTheme(isDarkMode);
136144
};
137145

138146
const handleClose = () => {
139147
setAnchorEl(null);
140148
};
141149

150+
React.useEffect(()=>{
151+
console.log('joinedRoom: ', joinedRoom)
152+
}, [joinedRoom])
153+
154+
function joinRoom () {
155+
console.log(roomCode)
156+
dispatch(changeRoom(roomCode))
157+
setConfirmRoom(confirmRoom=>roomCode)
158+
}
159+
142160
const switchDark = isDarkMode ?
143161
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" className="bi bi-lightbulb" viewBox="0 0 16 16">
144162
<path d="M2 6a6 6 0 1 1 10.174 4.31c-.203.196-.359.4-.453.619l-.762 1.769A.5.5 0 0 1 10.5 13a.5.5 0 0 1 0 1 .5.5 0 0 1 0 1l-.224.447a1 1 0 0 1-.894.553H6.618a1 1 0 0 1-.894-.553L5.5 15a.5.5 0 0 1 0-1 .5.5 0 0 1 0-1 .5.5 0 0 1-.46-.302l-.761-1.77a1.964 1.964 0 0 0-.453-.618A5.984 5.984 0 0 1 2 6zm6-5a5 5 0 0 0-3.479 8.592c.263.254.514.564.676.941L5.83 12h4.342l.632-1.467c.162-.377.413-.687.676-.941A5 5 0 0 0 8 1z"/>
@@ -148,8 +166,10 @@ function navbarDropDown () {
148166
<path d="M6 .278a.768.768 0 0 1 .08.858 7.208 7.208 0 0 0-.878 3.46c0 4.021 3.278 7.277 7.318 7.277.527 0 1.04-.055 1.533-.16a.787.787 0 0 1 .81.316.733.733 0 0 1-.031.893A8.349 8.349 0 0 1 8.344 16C3.734 16 0 12.286 0 7.71 0 4.266 2.114 1.312 5.124.06A.752.752 0 0 1 6 .278zM4.858 1.311A7.269 7.269 0 0 0 1.025 7.71c0 4.02 3.279 7.276 7.319 7.276a7.316 7.316 0 0 0 5.205-2.162c-.337.042-.68.063-1.029.063-4.61 0-8.343-3.714-8.343-8.29 0-1.167.242-2.278.681-3.286z"/>
149167
</svg>
150168

169+
const showMenu = props.dropMenu ? 'navDropDown' : 'hideNavDropDown'
170+
151171
return (
152-
<div className="navDropDown">
172+
<div className={showMenu}>
153173
<Link to="/tutorial" style={{ textDecoration: 'none' }} target='_blank'>
154174
<button>Tutorial
155175
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" className="bi bi-book" viewBox="0 0 16 16">
@@ -169,6 +189,10 @@ function navbarDropDown () {
169189
<path d="M4.318 2.687C5.234 2.271 6.536 2 8 2s2.766.27 3.682.687C12.644 3.125 13 3.627 13 4c0 .374-.356.875-1.318 1.313C10.766 5.729 9.464 6 8 6s-2.766-.27-3.682-.687C3.356 4.875 3 4.373 3 4c0-.374.356-.875 1.318-1.313ZM13 5.698V7c0 .374-.356.875-1.318 1.313C10.766 8.729 9.464 9 8 9s-2.766-.27-3.682-.687C3.356 7.875 3 7.373 3 7V5.698c.271.202.58.378.904.525C4.978 6.711 6.427 7 8 7s3.022-.289 4.096-.777A4.92 4.92 0 0 0 13 5.698ZM14 4c0-1.007-.875-1.755-1.904-2.223C11.022 1.289 9.573 1 8 1s-3.022.289-4.096.777C2.875 2.245 2 2.993 2 4v9c0 1.007.875 1.755 1.904 2.223C4.978 15.71 6.427 16 8 16s3.022-.289 4.096-.777C13.125 14.755 14 14.007 14 13V4Zm-1 4.698V10c0 .374-.356.875-1.318 1.313C10.766 11.729 9.464 12 8 12s-2.766-.27-3.682-.687C3.356 10.875 3 10.373 3 10V8.698c.271.202.58.378.904.525C4.978 9.71 6.427 10 8 10s3.022-.289 4.096-.777A4.92 4.92 0 0 0 13 8.698Zm0 3V13c0 .374-.356.875-1.318 1.313C10.766 14.729 9.464 15 8 15s-2.766-.27-3.682-.687C3.356 13.875 3 13.373 3 13v-1.302c.271.202.58.378.904.525C4.978 12.71 6.427 13 8 13s3.022-.289 4.096-.777c.324-.147.633-.323.904-.525Z"/>
170190
</svg>
171191
</button>}
192+
<input type="text" style={{ margin: '3px 5%', borderRadius: '5px', padding: '3px', width: '90%' }}
193+
placeholder="Room Code" onChange={(e)=>setRoomCode(e.target.value)}></input>
194+
<button onClick={()=>joinRoom()}>Join Room</button>
195+
<p>In Room: {joinedRoom}</p>
172196
<LoginButton style={{marginLeft: '10%'}}/>
173197
<StyledMenu // Dropdown menu connected to Manage Project Button
174198
id="customized-menu"

app/src/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,12 @@ let previousState = store.getState();
6565
// sending info to backend whenever the redux store changes
6666
const handleStoreChange = debounce(() => {
6767
const newState = store.getState();
68+
if (store.getState().roomCodeSlice.roomCode!==''){
69+
socket.emit('room-code', store.getState().roomCodeSlice.roomCode)
70+
}
6871
if (newState !== previousState){
6972
console.log('before sending to server: ', newState)
70-
socket.emit('custom-event', 'sent from front-end', JSON.stringify(newState))
73+
socket.emit('custom-event', 'sent from front-end', JSON.stringify(newState), store.getState().roomCodeSlice.roomCode)
7174
previousState = newState;
7275
}
7376
}, 100);

app/src/public/styles/style.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,11 @@ NAVBAR
566566
top: 45px;
567567
padding-top: 5px;
568568
padding-bottom: 5px;
569+
display: hidden;
570+
}
571+
572+
.hideNavDropDown{
573+
display: none;
569574
}
570575

571576
.navDropDown button{
@@ -580,6 +585,10 @@ NAVBAR
580585
align-items: center;
581586
}
582587

588+
.navDropDown p{
589+
margin: 3px 5%;
590+
}
591+
583592
.navDropDown button:hover{
584593
cursor: pointer;
585594
background-color: #007ab8

app/src/redux/reducers/rootReducer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import codePreviewReducer from './slice/codePreviewSlice';
66
import contextReducer from './slice/contextReducer';
77
import appStateReducer from './slice/appStateSlice.ts';
88
import styleReducer from './slice/styleSlice';
9+
import roomCodeReducer from './slice/roomCodeSlice'
910

1011
const rootReducer = combineReducers({
1112
// Add desired slices here
@@ -14,7 +15,7 @@ const rootReducer = combineReducers({
1415
contextSlice: contextReducer,
1516
appState: appStateReducer,
1617
styleSlice: styleReducer,
17-
18+
roomCodeSlice: roomCodeReducer,
1819
});
1920

2021
export default rootReducer;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// With redux toolkit you can combine all the actions, action types, and reducers into one 'slice'
2+
import { createSlice } from '@reduxjs/toolkit';
3+
// Sets initial state to false
4+
const initialState = {
5+
roomCode: ''
6+
};
7+
8+
// Creates new slice with the name darkMode, initial state, and reducer function which toggles dark mode state between true and false
9+
const roomCodeSlice = createSlice({
10+
name: 'roomCode',
11+
initialState,
12+
reducers: {
13+
changeRoom: (state, action) => {
14+
state.roomCode = action.payload
15+
}
16+
}
17+
});
18+
19+
// Exports the action creator function to be used with useDispatch
20+
export const { changeRoom } = roomCodeSlice.actions;
21+
// Exports so we can combine in rootReducer
22+
export default roomCodeSlice.reducer;

server/server.mjs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,17 @@ const io = new Server(httpServer, {
8686

8787
io.on('connection', socket => {
8888
console.log(socket.id)
89-
socket.on('custom-event', (string, redux_store) => {
90-
// console.log(string)
91-
// console.log(redux_store)
92-
socket.broadcast.emit('receive message', redux_store)
89+
socket.on('custom-event', (string, redux_store, room) => {
90+
console.log(room)
91+
if (room){
92+
socket.to(room).emit('receive message', redux_store)
93+
} else {
94+
socket.broadcast.emit('receive message', redux_store)
95+
}
96+
})
97+
socket.on('room-code', (roomCode)=>{
98+
console.log('joined room: ', roomCode)
99+
socket.join(roomCode)
93100
})
94101
})
95102

0 commit comments

Comments
 (0)