import React from "react" ; import { IQuestion , IQuestionData } from "../../Backend/model/Question-model" ; import { useQuestion } from "../../context/QuestionContext" ; import { RouterMapTopicName } from "../../routes" ; export function useHook ( ) { const { allQuestions , totalSolvedQuestion , selectedTopic , selectedTopicQuestions , dispatch , dismiss , } = useQuestion ( ) ; const pathname = decodeURI ( window . location . pathname ) . split ( "/" ) [ 1 ] ; const [ searchText , setSearchText ] = React . useState < string > ( "" ) ; async function feedSelectQuestionData ( abortController ) { const resp = await ( await fetch ( `/proxy/api/questions/topic/${ ( RouterMapTopicName as never ) [ pathname ] }` , { credentials : "include" , signal : abortController . signal , } ) ) . json ( ) ; if ( resp . error ) { dispatch ( { type : "ERROR" , payload : resp . error . message } ) ; } else { dispatch ( { type : "SELECT_QUESTION_TOPIC" , payload : { index : resp . questions [ 0 ] . position , questionTopic : { ... resp . questions [ 0 ] } , } , } ) ; } } React . useEffect ( ( ) => { const abortController = new AbortController ( ) ; if ( selectedTopic === null ) feedSelectQuestionData ( abortController ) ; return ( ) => abortController . abort ( ) ; } , [ ] ) ; function whenPressedCheckBox ( question , quesIndex ) { return async function ( ) { if ( selectedTopicQuestions !== null && selectedTopic !== null ) { let doneCount = question . Done ? - 1 : 1 ; const updatedQuestion = { ... question , Done : ! question . Done , } ; const totalQuestionSolved = totalSolvedQuestion + doneCount ; const updatedSelectedQuestionList = selectedTopicQuestions ?. map ( ( selectedQuestion , sIdx ) => { if ( selectedQuestion . Done ) doneCount ++ ; if ( sIdx === quesIndex ) { return updatedQuestion ; } else { return selectedQuestion ; } } ) ; const finalPayload = allQuestions . map ( ( questionTopic ) => { if ( selectedTopic === questionTopic . topicName ) { return { ... questionTopic , questions : [ ... updatedSelectedQuestionList ] , doneQuestions : doneCount , started : doneCount > 0 ? true : false , } ; } else { return questionTopic ; } } ) ; const resp = await ( await fetch ( `/proxy/api/questions/update-progress` , { method : "PATCH" , headers : { "Content-Type" : "application/json" , } , credentials : "include" , body : JSON . stringify ( finalPayload ) , } ) ) . json ( ) ; if ( resp . error ) { dispatch ( { type : "ERROR" , payload : resp . error . message } ) ; dismiss ( ( ) => dispatch ( { type : "RESET" } ) , 3 ) ; if ( resp . error . status === 401 ) window . location . reload ( ) ; } else { dispatch ( { type : "UPDATE_PROGRESS" , payload : { message : resp . message , allQuestions : finalPayload , selectedTopicQuestions : updatedSelectedQuestionList , totalQuestionSolved , } , } ) ; dismiss ( ( ) => dispatch ( { type : "RESET" } ) , 3 ) ; } } } ; } function getOnChangeSearch ( ) { return ( e ) => setSearchText ( e . target . value . toLowerCase ( ) ) ; } function getFilteredQuestionList ( ) { return selectedTopicQuestions ?. filter ( ( ques ) => ques . Problem . toLowerCase ( ) . includes ( searchText ) ) ; } return { getFilteredQuestionList , getOnChangeSearch , whenPressedCheckBox , feedSelectQuestionData , searchText , setSearchText , allQuestions , totalSolvedQuestion , selectedTopic , selectedTopicQuestions , dispatch , dismiss , pathname , } ; }