1
- import React , { useState , useEffect , useReducer , useRef } from 'react' ;
2
- import './CollaborationWindow.css' ;
3
- import Timer from './Timer' ;
4
- import { useNavigate } from 'react-router-dom' ;
5
- import socketIOClient , { Socket } from "socket.io-client" ;
6
- import 'firebase/auth' ;
7
- import CodeEditor from './CodeEditor' ;
8
- import Popup from 'reactjs-popup' ;
9
- import 'reactjs-popup/dist/index.css' ;
10
- import { getUserId } from '../../User/UserState' ;
11
- import { useLocation } from 'react-router-dom' ;
12
- import CommunicationWindow from './CommunicationWindow' ;
13
-
14
- import axios from "axios"
15
-
1
+ import React , { useState , useEffect , useReducer , useRef } from "react" ;
2
+ import "./CollaborationWindow.css" ;
3
+ import Timer from "./Timer" ;
4
+ import { useNavigate } from "react-router-dom" ;
5
+ import socketIOClient , { Socket } from "socket.io-client" ;
6
+ import "firebase/auth" ;
7
+ import CodeEditor from "./CodeEditor" ;
8
+ import Popup from "reactjs-popup" ;
9
+ import "reactjs-popup/dist/index.css" ;
10
+ import { getUserId } from "../../User/UserState" ;
11
+ import { useLocation } from "react-router-dom" ;
12
+ import CommunicationWindow from "./CommunicationWindow" ;
13
+
14
+ import axios from "axios" ;
16
15
17
16
const CollaborationWindow = ( ) => {
18
17
const [ sessionStarted , setSessionStarted ] = useState ( false ) ;
@@ -81,15 +80,6 @@ const CollaborationWindow = () => {
81
80
navigate ( "/landing" ) ; // Navigate to home or another route
82
81
} ) ;
83
82
84
- socket . current . on ( "user-disconnected" , ( userId ) => {
85
- console . log ( `User ${ userId } has disconnected` ) ;
86
- // Update the UI to reflect the user's disconnection
87
- showToast (
88
- `Experiencing a temporary glitch. Reestablishing your connection...`
89
- ) ;
90
- // Additional logic can be added here
91
- } ) ;
92
-
93
83
socket . current . on ( "user-reconnected" , ( userId ) => {
94
84
console . log ( `User ${ userId } has reconnected` ) ;
95
85
// Handle the user's reconnection in the UI
@@ -188,78 +178,101 @@ const CollaborationWindow = () => {
188
178
189
179
// Modify the handleExtendTimer function
190
180
const handleExtendTimer = ( ) => {
191
- if ( socket . current ) {
192
- socket . current . emit ( ' extend-time' ) ; // 15 minutes in milliseconds
193
- showToast ( ' Timer extended for 15 minutes' ) ;
194
- } else {
195
- console . log ( "reponse not available" ) ;
196
- }
197
- } ;
181
+ if ( socket . current ) {
182
+ socket . current . emit ( " extend-time" ) ; // 15 minutes in milliseconds
183
+ showToast ( " Timer extended for 15 minutes" ) ;
184
+ } else {
185
+ console . log ( "reponse not available" ) ;
186
+ }
187
+ } ;
198
188
199
189
// useEffect for the countdown logic
200
- useEffect ( ( ) => {
201
- let interval ;
202
-
203
- if ( sessionStarted && timeRemaining > 0 ) {
204
- interval = setInterval ( ( ) => {
205
- setTimeRemaining ( prevTime => {
206
- // When time runs out, show the popup and stop the interval
207
- if ( prevTime <= 1000 ) {
208
- clearInterval ( interval ) ;
209
- setPopup ( true ) ;
210
- return 0 ;
211
- }
212
- // Otherwise, continue counting down
213
- return prevTime - 1000 ;
214
- } ) ;
215
- } , 1000 ) ;
216
- }
217
-
218
- // Cleanup interval on component unmount or when session ends
219
- return ( ) => {
220
- if ( interval ) {
221
- clearInterval ( interval ) ;
222
- }
223
- } ;
224
- } , [ sessionStarted , timeRemaining ] ) ;
190
+ useEffect ( ( ) => {
191
+ let interval ;
192
+
193
+ if ( sessionStarted && timeRemaining > 0 ) {
194
+ interval = setInterval ( ( ) => {
195
+ setTimeRemaining ( ( prevTime ) => {
196
+ // When time runs out, show the popup and stop the interval
197
+ if ( prevTime <= 1000 ) {
198
+ clearInterval ( interval ) ;
199
+ setPopup ( true ) ;
200
+ return 0 ;
201
+ }
202
+ // Otherwise, continue counting down
203
+ return prevTime - 1000 ;
204
+ } ) ;
205
+ } , 1000 ) ;
206
+ }
207
+
208
+ // Cleanup interval on component unmount or when session ends
209
+ return ( ) => {
210
+ if ( interval ) {
211
+ clearInterval ( interval ) ;
212
+ }
213
+ } ;
214
+ } , [ sessionStarted , timeRemaining ] ) ;
225
215
226
216
return (
227
217
< div className = "collaboration-window" >
228
- < Timer sessionId = { sessionId } userId = { userId } setTimeRemaining = { setTimeRemaining } socket = { socket . current } />
229
- < Popup open = { popup } >
230
- < div className = "modal" >
231
- < div className = "header" > Time Up </ div >
232
- < div className = "content" >
233
- < div > Do you want to extend the time ?</ div >
234
- </ div >
235
- < div className = "actions" >
236
- < button className = "button extend-popup" onClick = { ( e ) => { handleExtendTimer ( ) ; onClosePopup ( ) } } >
237
- Yes
238
- </ button >
239
- < button className = "button close-popup" onClick = { ( e ) => { onClosePopup ( ) ; handleEndSession ( ) } } >
240
- No
241
- </ button >
242
- </ div >
243
- </ div >
244
- </ Popup >
245
- < div className = "timer-bar" >
246
- < div className = "left" >
247
- < span className = "time-remaining" > Time remaining: { formatTime ( timeRemaining ) } </ span >
248
- < button className = "extend-time" onClick = { handleExtendTimer } > Extend Timer</ button >
249
- </ div >
250
- < div className = "right" >
251
- < button className = "end-session" onClick = { handleEndSession } > End Session</ button >
252
- </ div >
253
- </ div >
254
- < div className = "content-area" >
255
- < div className = "question-section" >
256
- { question && (
257
- < >
258
- < h2 > { question . title } </ h2 >
259
- < p > { question . description } </ p >
260
- </ >
261
- ) }
262
- </ div >
218
+ < Timer
219
+ sessionId = { sessionId }
220
+ userId = { userId }
221
+ setTimeRemaining = { setTimeRemaining }
222
+ socket = { socket . current }
223
+ />
224
+ < Popup open = { popup } >
225
+ < div className = "modal" >
226
+ < div className = "header" > Time Up </ div >
227
+ < div className = "content" >
228
+ < div > Do you want to extend the time ?</ div >
229
+ </ div >
230
+ < div className = "actions" >
231
+ < button
232
+ className = "button extend-popup"
233
+ onClick = { ( e ) => {
234
+ handleExtendTimer ( ) ;
235
+ onClosePopup ( ) ;
236
+ } }
237
+ >
238
+ Yes
239
+ </ button >
240
+ < button
241
+ className = "button close-popup"
242
+ onClick = { ( e ) => {
243
+ onClosePopup ( ) ;
244
+ handleEndSession ( ) ;
245
+ } }
246
+ >
247
+ No
248
+ </ button >
249
+ </ div >
250
+ </ div >
251
+ </ Popup >
252
+ < div className = "timer-bar" >
253
+ < div className = "left" >
254
+ < span className = "time-remaining" >
255
+ Time remaining: { formatTime ( timeRemaining ) }
256
+ </ span >
257
+ < button className = "extend-time" onClick = { handleExtendTimer } >
258
+ Extend Timer
259
+ </ button >
260
+ </ div >
261
+ < div className = "right" >
262
+ < button className = "end-session" onClick = { handleEndSession } >
263
+ End Session
264
+ </ button >
265
+ </ div >
266
+ </ div >
267
+ < div className = "content-area" >
268
+ < div className = "question-section" >
269
+ { question && (
270
+ < >
271
+ < h2 > { question . title } </ h2 >
272
+ < p > { question . description } </ p >
273
+ </ >
274
+ ) }
275
+ </ div >
263
276
< div className = "editor-section" >
264
277
{ /* Placeholder for code editor */ }
265
278
{ /*<p>Code editor will go here...</p>*/ }
@@ -285,4 +298,4 @@ const CollaborationWindow = () => {
285
298
) ;
286
299
} ;
287
300
288
- export default CollaborationWindow ;
301
+ export default CollaborationWindow ;
0 commit comments