2023-01-13 13:02:48 +03:00
|
|
|
import React, {useEffect, useState} from 'react'
|
2023-01-16 15:24:08 +03:00
|
|
|
import {useNavigate} from "react-router-dom"
|
2023-01-13 13:02:48 +03:00
|
|
|
import {useSelector, useDispatch} from 'react-redux'
|
|
|
|
|
2023-01-16 19:12:44 +03:00
|
|
|
import {useRequest} from "../../../hooks/useRequest";
|
2022-03-18 16:39:13 +03:00
|
|
|
import {Progressbar} from './ProgressbarQuiz'
|
|
|
|
import {GetOptionTask} from './GetOptionTask'
|
2023-01-13 13:02:48 +03:00
|
|
|
|
2023-01-16 19:12:44 +03:00
|
|
|
import {
|
|
|
|
fetchUserAnswersMany, fetchUserAnswerOne, fetchGetAnswers, selectAnswer, selectedTest
|
|
|
|
} from './../../../redux/quizSlice'
|
2022-03-18 16:39:13 +03:00
|
|
|
|
2023-01-13 13:02:48 +03:00
|
|
|
import './quiz.scss'
|
2022-03-18 16:39:13 +03:00
|
|
|
|
|
|
|
export const TaskQuiz = () => {
|
|
|
|
|
2023-01-16 15:24:08 +03:00
|
|
|
const navigate = useNavigate();
|
|
|
|
const dispatch = useDispatch();
|
2022-03-18 16:39:13 +03:00
|
|
|
|
2023-01-16 15:24:08 +03:00
|
|
|
const listAnswers = useSelector(selectAnswer);
|
|
|
|
const dataTest = useSelector(selectedTest);
|
|
|
|
|
|
|
|
const [index, setIndex] = useState(0);
|
|
|
|
const [checkedValues, setCheckedValues] = useState([]);
|
|
|
|
const [stripValue, setStripValue] = useState(0);
|
|
|
|
const [inputValue, setInputValue] = useState('');
|
|
|
|
const [questions, setQuestions] = useState([]);
|
|
|
|
|
|
|
|
const {apiRequest} = useRequest();
|
|
|
|
|
|
|
|
const id = localStorage.getItem('id');
|
2022-03-18 16:39:13 +03:00
|
|
|
|
2023-01-16 15:24:08 +03:00
|
|
|
useEffect(() => {
|
|
|
|
apiRequest(`/question/get-questions?uuid=${dataTest.uuid}`)
|
|
|
|
.then((response) => {
|
2023-01-16 19:12:44 +03:00
|
|
|
console.log(response)
|
2023-01-16 15:24:08 +03:00
|
|
|
setQuestions(response);
|
|
|
|
dispatch(fetchGetAnswers(response[0].id));
|
|
|
|
setStripValue((+index + 1) * 100 / response.length)
|
|
|
|
})
|
2023-01-16 19:57:55 +03:00
|
|
|
}, [dispatch]);
|
2023-01-16 15:24:08 +03:00
|
|
|
|
|
|
|
const nextQuestion = async (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
//Проверка на валидацию ответов
|
|
|
|
if (checkedValues.length || inputValue) {
|
2022-03-18 16:39:13 +03:00
|
|
|
switch (questions[index].question_type_id) {
|
2023-01-16 15:24:08 +03:00
|
|
|
case '3':
|
|
|
|
dispatch(fetchUserAnswersMany(checkedValues));
|
|
|
|
break;
|
|
|
|
case '2':
|
|
|
|
case '1':
|
|
|
|
case '4':
|
|
|
|
dispatch(fetchUserAnswerOne(checkedValues));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2022-03-18 16:39:13 +03:00
|
|
|
}
|
|
|
|
|
2023-01-16 15:24:08 +03:00
|
|
|
//Проверка на существование следующего запроса
|
|
|
|
if (index < questions.length - 1) {
|
|
|
|
await dispatch(fetchGetAnswers(questions[index + 1].id));
|
|
|
|
setIndex(prev => prev >= questions.length - 1 ? prev : prev + 1);
|
|
|
|
setStripValue((prev => prev + (100 / questions.length)));
|
|
|
|
setCheckedValues([]);
|
|
|
|
setInputValue('')
|
|
|
|
} else {
|
2023-01-16 19:12:44 +03:00
|
|
|
navigate(`/quiz/result`);
|
2023-01-16 15:24:08 +03:00
|
|
|
alert("Тест пройден!")
|
|
|
|
}
|
2022-03-18 16:39:13 +03:00
|
|
|
|
2023-01-16 15:24:08 +03:00
|
|
|
} else {
|
|
|
|
alert("Вы не ответили на вопрос")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleChange = (e) => {
|
|
|
|
const checked = e.target.checked;
|
|
|
|
switch (questions[index].question_type_id) {
|
|
|
|
case '3':
|
|
|
|
checked ? setCheckedValues(prev => [...prev, {
|
|
|
|
user_id: id,
|
|
|
|
user_questionnaire_uuid: dataTest.uuid,
|
|
|
|
question_id: questions[index].id,
|
|
|
|
response_body: e.target.value
|
|
|
|
}]) :
|
|
|
|
setCheckedValues(prev => [...prev.filter(item => item.response_body !== e.target.value)]);
|
|
|
|
break;
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '4':
|
|
|
|
setCheckedValues([{
|
|
|
|
user_id: id,
|
|
|
|
user_questionnaire_uuid: dataTest.uuid,
|
|
|
|
question_id: questions[index].id,
|
|
|
|
response_body: e.target.value
|
|
|
|
}])
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
<React.StrictMode>
|
2022-03-18 16:39:13 +03:00
|
|
|
<Progressbar indexQuestion={index + 1} width={stripValue}/>
|
|
|
|
<div className="task">
|
2023-01-16 15:24:08 +03:00
|
|
|
{!questions.length || !stripValue || !listAnswers.length ?
|
|
|
|
<h1 className={'_container'} style={{display: "block"}}>Loading....</h1>
|
|
|
|
:
|
|
|
|
<div className="task__container">
|
2022-03-18 16:39:13 +03:00
|
|
|
<div className="task__code code">
|
2023-01-16 15:24:08 +03:00
|
|
|
{/* <CodeSnippetlighter /> */}
|
2022-03-18 16:39:13 +03:00
|
|
|
</div>
|
|
|
|
<h3 className="task__title quiz-title_h3">{questions[index].question_body}</h3>
|
|
|
|
<div className="task__body">
|
2023-01-16 15:24:08 +03:00
|
|
|
<form className='task__form form-task'>
|
|
|
|
{
|
|
|
|
questions[index].question_type_id === 1 ?
|
|
|
|
<GetOptionTask
|
|
|
|
type={1}
|
|
|
|
inputValue={checkedValues.length ? checkedValues[0].response_body : ''}
|
|
|
|
handleChange={handleChange}
|
|
|
|
/>
|
|
|
|
:
|
|
|
|
listAnswers.map((answer) => (
|
|
|
|
<GetOptionTask
|
|
|
|
key={answer.id}
|
|
|
|
type={questions[index].question_type_id}
|
|
|
|
handleChange={handleChange}
|
|
|
|
answer={answer}
|
|
|
|
/>
|
|
|
|
))
|
|
|
|
}
|
|
|
|
<div className="form-task__buttons">
|
|
|
|
{questions.length !== index + 1 &&
|
|
|
|
<button type='submit' className='quiz-btn'
|
|
|
|
onClick={(e) => nextQuestion(e)}>Далее</button>}
|
|
|
|
{questions.length === index + 1 &&
|
|
|
|
<button onClick={(e) => nextQuestion(e)}
|
|
|
|
className='quiz-btn quiz-btn_dark-green'>Завершить</button>}
|
|
|
|
</div>
|
|
|
|
</form>
|
2022-03-18 16:39:13 +03:00
|
|
|
</div>
|
2023-01-16 15:24:08 +03:00
|
|
|
</div>
|
|
|
|
}
|
2022-03-18 16:39:13 +03:00
|
|
|
</div>
|
2023-01-16 15:24:08 +03:00
|
|
|
</React.StrictMode>
|
|
|
|
)
|
2023-01-13 13:02:48 +03:00
|
|
|
};
|