11/* eslint-disable react-refresh/only-export-components */
22
3- import React , { createContext , useContext , useState } from "react" ;
3+ import React , { createContext , useContext , useEffect , useState } from "react" ;
44import {
55 USE_MATCH_ERROR_MESSAGE ,
66 FAILED_TESTCASE_MESSAGE ,
@@ -11,7 +11,7 @@ import {
1111import { toast } from "react-toastify" ;
1212
1313import { useMatch } from "./MatchContext" ;
14- import { codeExecutionClient } from "../utils/api" ;
14+ import { qnHistoryClient , codeExecutionClient } from "../utils/api" ;
1515import { useReducer } from "react" ;
1616import { updateQnHistoryById } from "../reducers/qnHistoryReducer" ;
1717import qnHistoryReducer , { initialQHState } from "../reducers/qnHistoryReducer" ;
@@ -33,7 +33,7 @@ export type CompilerResult = {
3333} ;
3434
3535type CollabContextType = {
36- handleSubmitSessionClick : ( time : number ) => void ;
36+ handleSubmitSessionClick : ( ) => void ;
3737 handleEndSessionClick : ( ) => void ;
3838 handleRejectEndSession : ( ) => void ;
3939 handleConfirmEndSession : ( ) => void ;
@@ -42,6 +42,8 @@ type CollabContextType = {
4242 compilerResult : CompilerResult [ ] ;
4343 setCompilerResult : React . Dispatch < React . SetStateAction < CompilerResult [ ] > > ;
4444 isEndSessionModalOpen : boolean ;
45+ time : number ;
46+ resetCollab : ( ) => void ;
4547} ;
4648
4749const CollabContext = createContext < CollabContextType | null > ( null ) ;
@@ -66,6 +68,17 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
6668 qnHistoryId,
6769 } = match ;
6870
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+
6982 // eslint-disable-next-line
7083 const [ _qnHistoryState , qnHistoryDispatch ] = useReducer (
7184 qnHistoryReducer ,
@@ -76,7 +89,7 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
7689 const [ isEndSessionModalOpen , setIsEndSessionModalOpen ] =
7790 useState < boolean > ( false ) ;
7891
79- const handleSubmitSessionClick = async ( time : number ) => {
92+ const handleSubmitSessionClick = async ( ) => {
8093 try {
8194 const res = await codeExecutionClient . post ( "/" , {
8295 questionId,
@@ -124,9 +137,26 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
124137 setIsEndSessionModalOpen ( false ) ;
125138 } ;
126139
127- const handleConfirmEndSession = ( ) => {
140+ const handleConfirmEndSession = async ( ) => {
128141 setIsEndSessionModalOpen ( false ) ;
129142
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+
130160 // Leave collaboration room
131161 leave ( matchUser ?. id as string , getMatchId ( ) as string , true ) ;
132162 leave ( partner ?. id as string , getMatchId ( ) as string , true ) ;
@@ -137,6 +167,9 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
137167 // Delete match data
138168 stopMatch ( ) ;
139169 appNavigate ( "/home" ) ;
170+
171+ // Reset collab state
172+ resetCollab ( ) ;
140173 } ;
141174
142175 const checkPartnerStatus = ( ) => {
@@ -148,6 +181,11 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
148181 } ) ;
149182 } ;
150183
184+ const resetCollab = ( ) => {
185+ setCompilerResult ( [ ] ) ;
186+ setTime ( 0 ) ;
187+ } ;
188+
151189 return (
152190 < CollabContext . Provider
153191 value = { {
@@ -160,6 +198,8 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
160198 compilerResult,
161199 setCompilerResult,
162200 isEndSessionModalOpen,
201+ time,
202+ resetCollab,
163203 } }
164204 >
165205 { children }
0 commit comments