Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3520 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3481 stevensc 1
import React, { useState } from 'react'
2
import { connect } from 'react-redux'
3
 
4
import { startQuiz } from '@app/services/micro-learning'
5
import { addNotification } from '@app/redux/notification/notification.actions'
6
 
7
import Widget from '@app/components/UI/Widget'
8
import QuizSimpleQuestion from './quiz-simple-question'
9
import QuizMultipleQuestion from './quiz-multiple-question'
10
import SlideProgress from './slide-progress'
11
import SlideSuccessFeedback from './slide-success-feedback'
12
import SlideQuizStart from './slide-quiz-start'
13
import QuizFailureFeedback from './quiz-failure-feedback'
14
 
15
function SlideQuiz({
16
  quiz,
17
  slide,
18
  onSync,
19
  startUrl,
20
  completed,
21
  addNotification
22
}) {
23
  const [currentQuestion, setCurrentQuestion] = useState(0)
24
  const [showQuestions, setShowQuestions] = useState(false)
25
  const [completedQuiz, setCompletedQuiz] = useState(false)
26
  const [failureQuiz, setFailureQuiz] = useState(false)
27
  const [totalPoints, setTotalPoints] = useState(0)
28
  const [answer, setAnswers] = useState([])
29
 
30
  const { name, questions, minimum_points_required } = quiz
31
 
32
  const handleStart = async () => {
33
    try {
34
      const start = await startQuiz(startUrl)
35
      if (start) setShowQuestions(true)
36
    } catch (error) {
37
      addNotification({ style: 'danger', msg: error.message })
38
    }
39
  }
40
 
41
  const onError = () => {
42
    setFailureQuiz(true)
43
  }
44
 
45
  const restartQuiz = () => {
46
    setCurrentQuestion(0)
47
    setTotalPoints(0)
48
    setFailureQuiz(false)
49
    setCompletedQuiz(false)
50
    setShowQuestions(false)
51
  }
52
 
53
  const onSimpleConfirm = (question, answerId) => {
54
    const answer = question.answers.find((q) => q.uuid === answerId)
55
    const newAnswer = {
56
      question: question.uuid,
57
      answers: [answer]
58
    }
59
    setAnswers((prevAnswers) => [...prevAnswers, newAnswer])
60
 
61
    const isCorrect = answer.correct === 'y'
62
    isCorrect && setTotalPoints(totalPoints + parseInt(question.points))
63
 
64
    if (currentQuestion !== questions.length - 1) {
65
      setCurrentQuestion(currentQuestion + 1)
66
      return
67
    }
68
 
69
    if (totalPoints >= parseInt(minimum_points_required)) {
70
      setCompletedQuiz(true)
71
    } else {
72
      onError()
73
    }
74
  }
75
 
76
  const onMultipleConfirm = (question, answersIds) => {
77
    const answers = question.answers.filter((answer) =>
78
      answersIds.includes(answer.uuid)
79
    )
80
    const newAnswer = {
81
      question: question.uuid,
82
      answers
83
    }
84
    setAnswers((prevAnswers) => [...prevAnswers, newAnswer])
85
 
86
    const points = answers.reduce(
87
      (acc, answer) => acc + parseInt(answer.points),
88
 
89
    )
90
 
91
    setTotalPoints(totalPoints + points)
92
 
93
    if (currentQuestion !== questions.length - 1) {
94
      setCurrentQuestion(currentQuestion + 1)
95
      return
96
    }
97
 
98
    if (totalPoints >= parseInt(minimum_points_required)) {
99
      setCompletedQuiz(true)
100
    } else {
101
      onError()
102
    }
103
  }
104
 
105
  if (completedQuiz) {
106
    return <SlideSuccessFeedback onConfirm={onSync} />
107
  }
108
 
109
  if (failureQuiz) {
110
    return <QuizFailureFeedback onConfirm={restartQuiz} />
111
  }
112
 
113
  if (!showQuestions) {
114
    return (
115
      <SlideQuizStart
116
        onStart={handleStart}
117
        completed={completed}
118
        slide={slide}
119
      />
120
    )
121
  }
122
 
123
  return (
124
    <Widget>
125
      <SlideProgress
126
        points={totalPoints}
127
        time={quiz.max_time}
128
        currentQuestion={currentQuestion + 1}
129
        totalQuestions={questions.length}
130
        onTimeEnd={onError}
131
      />
132
 
133
      <Widget.Header title={`Cuestionario sobre: ${name}`} />
134
 
135
      <Widget.Body>
136
        {questions.map((question, index) => {
137
          const isLastQuestion = index === questions.length - 1
138
          const buttonLabel = isLastQuestion ? 'Finalizar' : 'Siguiente'
139
 
140
          if (question.type === 's') {
141
            return (
142
              <QuizSimpleQuestion
143
                key={question.uuid}
144
                question={question}
145
                onConfirm={(answer) => onSimpleConfirm(question, answer)}
146
                buttonLabel={buttonLabel}
147
                isCurrent={currentQuestion === index}
148
              />
149
            )
150
          }
151
 
152
          if (question.type === 'm') {
153
            return (
154
              <QuizMultipleQuestion
155
                key={question.uuid}
156
                question={question}
157
                onConfirm={(answers) => onMultipleConfirm(question, answers)}
158
                buttonLabel={buttonLabel}
159
                isCurrent={currentQuestion === index}
160
              />
161
            )
162
          }
163
 
164
          return null
165
        })}
166
      </Widget.Body>
167
    </Widget>
168
  )
169
}
170
 
171
const mapDispatchToProps = {
172
  addNotification: (notification) => addNotification(notification)
173
}
174
 
175
export default connect(null, mapDispatchToProps)(SlideQuiz)