1
1
/* eslint-disable react-refresh/only-export-components */
2
2
3
- import React , { createContext , useContext , useState } from "react" ;
3
+ import React , { createContext , useContext , useEffect , useState } from "react" ;
4
4
import {
5
5
USE_MATCH_ERROR_MESSAGE ,
6
6
FAILED_TESTCASE_MESSAGE ,
@@ -11,7 +11,7 @@ import {
11
11
import { toast } from "react-toastify" ;
12
12
13
13
import { useMatch } from "./MatchContext" ;
14
- import { codeExecutionClient } from "../utils/api" ;
14
+ import { qnHistoryClient , codeExecutionClient } from "../utils/api" ;
15
15
import { useReducer } from "react" ;
16
16
import { updateQnHistoryById } from "../reducers/qnHistoryReducer" ;
17
17
import qnHistoryReducer , { initialQHState } from "../reducers/qnHistoryReducer" ;
@@ -33,7 +33,7 @@ export type CompilerResult = {
33
33
} ;
34
34
35
35
type CollabContextType = {
36
- handleSubmitSessionClick : ( time : number ) => void ;
36
+ handleSubmitSessionClick : ( ) => void ;
37
37
handleEndSessionClick : ( ) => void ;
38
38
handleRejectEndSession : ( ) => void ;
39
39
handleConfirmEndSession : ( ) => void ;
@@ -42,6 +42,8 @@ type CollabContextType = {
42
42
compilerResult : CompilerResult [ ] ;
43
43
setCompilerResult : React . Dispatch < React . SetStateAction < CompilerResult [ ] > > ;
44
44
isEndSessionModalOpen : boolean ;
45
+ time : number ;
46
+ resetCollab : ( ) => void ;
45
47
} ;
46
48
47
49
const CollabContext = createContext < CollabContextType | null > ( null ) ;
@@ -66,6 +68,17 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
66
68
qnHistoryId,
67
69
} = match ;
68
70
71
+ const [ time , setTime ] = useState < number > ( 0 ) ;
72
+
73
+ useEffect ( ( ) => {
74
+ const intervalId = setInterval (
75
+ ( ) => setTime ( ( prevTime ) => prevTime + 1 ) ,
76
+ 1000
77
+ ) ;
78
+
79
+ return ( ) => clearInterval ( intervalId ) ;
80
+ } , [ time ] ) ;
81
+
69
82
// eslint-disable-next-line
70
83
const [ _qnHistoryState , qnHistoryDispatch ] = useReducer (
71
84
qnHistoryReducer ,
@@ -76,7 +89,7 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
76
89
const [ isEndSessionModalOpen , setIsEndSessionModalOpen ] =
77
90
useState < boolean > ( false ) ;
78
91
79
- const handleSubmitSessionClick = async ( time : number ) => {
92
+ const handleSubmitSessionClick = async ( ) => {
80
93
try {
81
94
const res = await codeExecutionClient . post ( "/" , {
82
95
questionId,
@@ -124,9 +137,26 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
124
137
setIsEndSessionModalOpen ( false ) ;
125
138
} ;
126
139
127
- const handleConfirmEndSession = ( ) => {
140
+ const handleConfirmEndSession = async ( ) => {
128
141
setIsEndSessionModalOpen ( false ) ;
129
142
143
+ // Get queston history
144
+ const data = await qnHistoryClient . get ( qnHistoryId as string ) ;
145
+
146
+ // Only update question history if it has not been submitted before
147
+ if ( ! data . data . qnHistory . code ) {
148
+ updateQnHistoryById (
149
+ qnHistoryId as string ,
150
+ {
151
+ submissionStatus : "Attempted" ,
152
+ dateAttempted : new Date ( ) . toISOString ( ) ,
153
+ timeTaken : time ,
154
+ code : code . replace ( / \t / g, " " . repeat ( 4 ) ) ,
155
+ } ,
156
+ qnHistoryDispatch
157
+ ) ;
158
+ }
159
+
130
160
// Leave collaboration room
131
161
leave ( matchUser ?. id as string , getMatchId ( ) as string , true ) ;
132
162
leave ( partner ?. id as string , getMatchId ( ) as string , true ) ;
@@ -137,6 +167,9 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
137
167
// Delete match data
138
168
stopMatch ( ) ;
139
169
appNavigate ( "/home" ) ;
170
+
171
+ // Reset collab state
172
+ resetCollab ( ) ;
140
173
} ;
141
174
142
175
const checkPartnerStatus = ( ) => {
@@ -148,6 +181,11 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
148
181
} ) ;
149
182
} ;
150
183
184
+ const resetCollab = ( ) => {
185
+ setCompilerResult ( [ ] ) ;
186
+ setTime ( 0 ) ;
187
+ } ;
188
+
151
189
return (
152
190
< CollabContext . Provider
153
191
value = { {
@@ -160,6 +198,8 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
160
198
compilerResult,
161
199
setCompilerResult,
162
200
isEndSessionModalOpen,
201
+ time,
202
+ resetCollab,
163
203
} }
164
204
>
165
205
{ children }
0 commit comments