@@ -44,6 +44,7 @@ type CollabContextType = {
4444 isEndSessionModalOpen : boolean ;
4545 time : number ;
4646 resetCollab : ( ) => void ;
47+ checkDocReady : ( ) => void ;
4748} ;
4849
4950const CollabContext = createContext < CollabContextType | null > ( null ) ;
@@ -65,7 +66,6 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
6566 getMatchId,
6667 stopMatch,
6768 questionId,
68- qnHistoryId,
6969 } = match ;
7070
7171 const [ time , setTime ] = useState < number > ( 0 ) ;
@@ -88,6 +88,8 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
8888 const [ compilerResult , setCompilerResult ] = useState < CompilerResult [ ] > ( [ ] ) ;
8989 const [ isEndSessionModalOpen , setIsEndSessionModalOpen ] =
9090 useState < boolean > ( false ) ;
91+ const [ qnHistoryId , setQnHistoryId ] = useState < string | null > ( null ) ;
92+ let hasSubmitted : boolean = false ;
9193
9294 const handleSubmitSessionClick = async ( ) => {
9395 try {
@@ -97,6 +99,7 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
9799 code : code . replace ( / \t / g, " " . repeat ( 4 ) ) ,
98100 language : matchCriteria ?. language . toLowerCase ( ) ,
99101 } ) ;
102+ hasSubmitted = true ;
100103 console . log ( [ ...res . data . data ] ) ;
101104 setCompilerResult ( [ ...res . data . data ] ) ;
102105
@@ -140,11 +143,8 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
140143 const handleConfirmEndSession = async ( ) => {
141144 setIsEndSessionModalOpen ( false ) ;
142145
143- // Get queston history
144- const data = await qnHistoryClient . get ( qnHistoryId as string ) ;
145-
146146 // Only update question history if it has not been submitted before
147- if ( ! data . data . qnHistory . code ) {
147+ if ( ! hasSubmitted ) {
148148 updateQnHistoryById (
149149 qnHistoryId as string ,
150150 {
@@ -172,6 +172,12 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
172172 resetCollab ( ) ;
173173 } ;
174174
175+ const checkDocReady = ( ) => {
176+ collabSocket . on ( CollabEvents . DOCUMENT_READY , ( qnHistoryId : string ) => {
177+ setQnHistoryId ( qnHistoryId ) ;
178+ } ) ;
179+ } ;
180+
175181 const checkPartnerStatus = ( ) => {
176182 collabSocket . on ( CollabEvents . PARTNER_LEFT , ( ) => {
177183 toast . error ( COLLAB_ENDED_MESSAGE ) ;
@@ -200,6 +206,7 @@ const CollabProvider: React.FC<{ children?: React.ReactNode }> = (props) => {
200206 isEndSessionModalOpen,
201207 time,
202208 resetCollab,
209+ checkDocReady,
203210 } }
204211 >
205212 { children }
0 commit comments