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,15 +33,16 @@ export type CompilerResult = {
3333} ;
3434
3535type CollabContextType = {
36- handleSubmitSessionClick : ( time : number ) => void ;
36+ handleSubmitSessionClick : ( ) => void ;
3737 handleEndSessionClick : ( ) => void ;
3838 handleRejectEndSession : ( ) => void ;
3939 handleConfirmEndSession : ( ) => void ;
4040 checkPartnerStatus : ( ) => void ;
4141 setCode : React . Dispatch < React . SetStateAction < string > > ;
4242 compilerResult : CompilerResult [ ] ;
43- setCompilerResult : React . Dispatch < React . SetStateAction < CompilerResult [ ] > > ;
4443 isEndSessionModalOpen : boolean ;
44+ time : number ;
45+ resetCollab : ( ) => void ;
4546} ;
4647
4748const CollabContext = createContext < CollabContextType | null > ( null ) ;
@@ -66,6 +67,17 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
6667 qnHistoryId,
6768 } = match ;
6869
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+
6981 // eslint-disable-next-line
7082 const [ _qnHistoryState , qnHistoryDispatch ] = useReducer (
7183 qnHistoryReducer ,
@@ -76,7 +88,7 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
7688 const [ isEndSessionModalOpen , setIsEndSessionModalOpen ] =
7789 useState < boolean > ( false ) ;
7890
79- const handleSubmitSessionClick = async ( time : number ) => {
91+ const handleSubmitSessionClick = async ( ) => {
8092 try {
8193 const res = await codeExecutionClient . post ( "/" , {
8294 questionId,
@@ -124,9 +136,26 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
124136 setIsEndSessionModalOpen ( false ) ;
125137 } ;
126138
127- const handleConfirmEndSession = ( ) => {
139+ const handleConfirmEndSession = async ( ) => {
128140 setIsEndSessionModalOpen ( false ) ;
129141
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+
130159 // Leave collaboration room
131160 leave ( matchUser ?. id as string , getMatchId ( ) as string , true ) ;
132161 leave ( partner ?. id as string , getMatchId ( ) as string , true ) ;
@@ -137,6 +166,9 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
137166 // Delete match data
138167 stopMatch ( ) ;
139168 appNavigate ( "/home" ) ;
169+
170+ // Reset collab state
171+ resetCollab ( ) ;
140172 } ;
141173
142174 const checkPartnerStatus = ( ) => {
@@ -148,6 +180,11 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
148180 } ) ;
149181 } ;
150182
183+ const resetCollab = ( ) => {
184+ setCompilerResult ( [ ] ) ;
185+ setTime ( 0 ) ;
186+ } ;
187+
151188 return (
152189 < CollabContext . Provider
153190 value = { {
@@ -158,8 +195,9 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
158195 checkPartnerStatus,
159196 setCode,
160197 compilerResult,
161- setCompilerResult,
162198 isEndSessionModalOpen,
199+ time,
200+ resetCollab,
163201 } }
164202 >
165203 { children }
0 commit comments