File size: 1,468 Bytes
ebab432
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
import { useCallback, useMemo } from 'react'

export default function useTestProgress({
  questions,
  answers,
  current,
  currentQuestion,
  setCurrent,
  commitNAT,
  setMarked,
  setVisited,
}) {
  const saveAndNext = useCallback(() => {
    if (currentQuestion?.question_type === 'nat') commitNAT()
    if (current < questions.length - 1) setCurrent(c => c + 1)
  }, [commitNAT, current, currentQuestion, questions.length, setCurrent])

  const markAndNext = useCallback(() => {
    if (!currentQuestion) return
    setMarked(m => {
      const n = new Set(m)
      n.has(currentQuestion.id) ? n.delete(currentQuestion.id) : n.add(currentQuestion.id)
      return n
    })
    if (current < questions.length - 1) setCurrent(c => c + 1)
  }, [current, currentQuestion, questions.length, setMarked, setCurrent])

  const questionGroups = useMemo(() => {
    const map = new Map()
    questions.forEach((q, idx) => {
      const subject = q.subject || 'General'
      if (!map.has(subject)) map.set(subject, [])
      map.get(subject).push({ question: q, index: idx })
    })
    return map
  }, [questions])

  const subjects = useMemo(() => [...questionGroups.keys()], [questionGroups])

  const answered = useMemo(() => {
    return Object.values(answers).filter(Boolean).length
  }, [answers])

  const notAnswered = questions.length - answered

  return {
    saveAndNext,
    markAndNext,
    questionGroups,
    subjects,
    answered,
    notAnswered,
  }
}