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