|
| 1 | +import { Stack, Typography } from '@mui/material'; |
| 2 | +import { useDispatch, useSelector } from 'react-redux'; |
| 3 | + |
| 4 | +import Button from '@mui/material/Button'; |
1 | 5 | import React from 'react'; |
| 6 | +import { RootState } from '../../redux/store'; |
| 7 | +import TextField from '@mui/material/TextField'; |
| 8 | +import { allCooperativeState } from '../../redux/reducers/slice/appStateSlice'; |
| 9 | +import { changeRoom } from '../../redux/reducers/slice/roomCodeSlice'; |
| 10 | +import { codePreviewCooperative } from '../../redux/reducers/slice/codePreviewSlice'; |
| 11 | +import config from '../../../../config'; |
| 12 | +import { cooperativeStyle } from '../../redux/reducers/slice/styleSlice'; |
| 13 | +// websocket front end starts here |
| 14 | +import { io } from 'socket.io-client'; |
| 15 | +import store from '../../redux/store'; |
| 16 | +import { toggleDarkMode } from '../../redux/reducers/slice/darkModeSlice'; |
2 | 17 |
|
| 18 | +// for websockets |
| 19 | +// Part - join room and room code functionality |
| 20 | +let socket; |
| 21 | +const { API_BASE_URL } = config; |
3 | 22 | const RoomsContainer = () => { |
4 | | - return <div>RoomsContainer</div>; |
| 23 | + const [roomCode, setRoomCode] = React.useState(''); |
| 24 | + const [confirmRoom, setConfirmRoom] = React.useState(''); |
| 25 | + const dispatch = useDispatch(); |
| 26 | + const { isDarkMode, state, joinedRoom } = useSelector((store: RootState) => ({ |
| 27 | + isDarkMode: store.darkMode.isDarkMode, |
| 28 | + state: store.appState, |
| 29 | + joinedRoom: store.roomCodeSlice.roomCode |
| 30 | + })); |
| 31 | + React.useEffect(() => { |
| 32 | + console.log('joinedRoom: ', joinedRoom); |
| 33 | + }, [joinedRoom]); |
| 34 | + |
| 35 | + function initSocketConnection(roomCode) { |
| 36 | + if (socket) { |
| 37 | + socket.disconnect(); |
| 38 | + } |
| 39 | + |
| 40 | + socket = io(API_BASE_URL, { |
| 41 | + transports: ['websocket'] |
| 42 | + }); |
| 43 | + |
| 44 | + socket.on('connect', () => { |
| 45 | + console.log(`You connected with id: ${socket.id}`); |
| 46 | + socket.emit('join-room', roomCode); // Join the room when connected |
| 47 | + }); |
| 48 | + |
| 49 | + // Receiving the room state from the backend |
| 50 | + socket.on('room-state-update', (stateFromServer) => { |
| 51 | + const newState = JSON.parse(stateFromServer); |
| 52 | + // Dispatch actions to update your Redux store with the received state |
| 53 | + store.dispatch(allCooperativeState(newState.appState)); |
| 54 | + store.dispatch(codePreviewCooperative(newState.codePreviewCooperative)); |
| 55 | + store.dispatch(cooperativeStyle(newState.styleSlice)); |
| 56 | + }); |
| 57 | + |
| 58 | + // receiving the message from the back end |
| 59 | + socket.on('receive message', (event) => { |
| 60 | + // console.log('message from server: ', event); |
| 61 | + let currentStore: any = JSON.stringify(store.getState()); |
| 62 | + if (currentStore !== event) { |
| 63 | + currentStore = JSON.parse(currentStore); |
| 64 | + event = JSON.parse(event); |
| 65 | + console.log('stores do not match'); |
| 66 | + if (currentStore.darkMode.isDarkMode !== event.darkMode.isDarkMode) { |
| 67 | + store.dispatch(toggleDarkMode()); |
| 68 | + } else if (currentStore.appState !== event.appState) { |
| 69 | + store.dispatch(allCooperativeState(event.appState)); |
| 70 | + } else if ( |
| 71 | + currentStore.codePreviewSlice !== event.codePreviewCooperative |
| 72 | + ) { |
| 73 | + store.dispatch(codePreviewCooperative(event.codePreviewCooperative)); |
| 74 | + } else if (currentStore.styleSlice !== event.styleSlice) { |
| 75 | + store.dispatch(cooperativeStyle(event.styleSlice)); |
| 76 | + } |
| 77 | + } |
| 78 | + console.log('updated user Store from another user: ', store.getState()); |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + function handleUserEnteredRoom(roomCode) { |
| 83 | + initSocketConnection(roomCode); |
| 84 | + } |
| 85 | + |
| 86 | + function joinRoom() { |
| 87 | + console.log(roomCode); |
| 88 | + dispatch(changeRoom(roomCode)); |
| 89 | + setConfirmRoom((confirmRoom) => roomCode); |
| 90 | + |
| 91 | + // Call handleUserEnteredRoom when joining a room |
| 92 | + handleUserEnteredRoom(roomCode); |
| 93 | + } |
| 94 | + return ( |
| 95 | + <div> |
| 96 | + <Stack |
| 97 | + spacing={2} |
| 98 | + sx={{ |
| 99 | + paddingTop: '20px', |
| 100 | + maxWidth: '230px', |
| 101 | + alignItems: 'center', |
| 102 | + margin: '0 auto 0 auto' |
| 103 | + }} |
| 104 | + > |
| 105 | + {' '} |
| 106 | + <TextField |
| 107 | + hiddenLabel |
| 108 | + id="filled-hidden-label-small" |
| 109 | + variant="filled" |
| 110 | + size="small" |
| 111 | + onChange={(e) => setRoomCode(e.target.value)} |
| 112 | + /> |
| 113 | + {/* <input |
| 114 | + type="text" |
| 115 | + style={{ |
| 116 | + margin: '3px 5%', |
| 117 | + borderRadius: '5px', |
| 118 | + padding: '3px', |
| 119 | + width: '90%' |
| 120 | + }} |
| 121 | + placeholder="Room Code" |
| 122 | + onChange={(e) => setRoomCode(e.target.value)} |
| 123 | + ></input> */} |
| 124 | + <Button |
| 125 | + variant="contained" |
| 126 | + onClick={() => joinRoom()} |
| 127 | + sx={{ |
| 128 | + backgroundColor: '#ffffff', |
| 129 | + color: '#000000', |
| 130 | + '&:hover': { |
| 131 | + backgroundColor: '#C6C6C6', |
| 132 | + borderColor: '#0062cc', |
| 133 | + boxShadow: 'none' |
| 134 | + } |
| 135 | + }} |
| 136 | + > |
| 137 | + Join Room |
| 138 | + </Button> |
| 139 | + <Typography variant="h6" color={'white'}> |
| 140 | + In Room: {joinedRoom} |
| 141 | + </Typography> |
| 142 | + </Stack> |
| 143 | + {/* <button onClick={() => joinRoom()}>Join Room</button> */} |
| 144 | + </div> |
| 145 | + ); |
5 | 146 | }; |
6 | 147 |
|
7 | 148 | export default RoomsContainer; |
0 commit comments