All files / src/components/contexts QuestionsContext.tsx

100% Statements 10/10
100% Branches 0/0
100% Functions 2/2
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 4310x   10x                   10x         10x             4x 4x   4x                           10x  
import { ReactNode, createContext, useContext, useState } from 'react'
 
import { QuestionDispatchAction, useQuestionReducer } from '../../reducers/questionReducer'
import { Question } from 'sqc-core-functions'
 
export type QuestionsContextProps = {
  questions: Question[]
  questionsDispatch: React.Dispatch<QuestionDispatchAction>
  selectedQuestion: number
  setSelectedQuestion: React.Dispatch<React.SetStateAction<number>>
}
 
const QuestionsContext = createContext({} as QuestionsContextProps)
 
/**
 * Hook to access question context
 */
const useQuestionsContext = () => useContext(QuestionsContext)
 
/**
 * Provider element to wrap the other elements to access question context
 * @see {@link useQuestionsContext}
 */
function QuestionsProvider({ children }: { children: ReactNode }) {
  const { questions, questionsDispatch } = useQuestionReducer()
  const [selectedQuestion, setSelectedQuestion] = useState(0)
 
  return (
    <QuestionsContext.Provider
      value={{
        questions,
        questionsDispatch,
        selectedQuestion,
        setSelectedQuestion,
      }}
    >
      {children}
    </QuestionsContext.Provider>
  )
}
 
export { useQuestionsContext, QuestionsProvider }