2023-04-19 20:22:06 +03:00
|
|
|
import React, { useEffect, useState } from 'react'
|
|
|
|
import { useNavigate } from "react-router-dom"
|
|
|
|
import { useSelector, useDispatch } from 'react-redux'
|
|
|
|
import questionIcon from "../../../images/question.png"
|
|
|
|
import { Progressbar } from './ProgressbarQuiz'
|
|
|
|
import { GetOptionTask } from './GetOptionTask'
|
2023-01-16 19:12:44 +03:00
|
|
|
import {
|
2023-04-19 20:22:06 +03:00
|
|
|
fetchUserAnswersMany, fetchUserAnswerOne, fetchGetAnswers, answersSelector, selectedTest, questionsSelector, setAnswers, setCompleteTest
|
2023-01-16 19:12:44 +03:00
|
|
|
} from './../../../redux/quizSlice'
|
2022-03-18 16:39:13 +03:00
|
|
|
|
2023-01-13 13:02:48 +03:00
|
|
|
import './quiz.scss'
|
2023-04-19 20:22:06 +03:00
|
|
|
import { apiRequest } from "../../../api/request";
|
|
|
|
import { HeaderQuiz } from './HeaderQuiz'
|
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-04-19 20:22:06 +03:00
|
|
|
const answers = useSelector(answersSelector);
|
|
|
|
const questions = useSelector(questionsSelector);
|
|
|
|
|
2023-01-16 15:24:08 +03:00
|
|
|
|
2023-04-19 20:22:06 +03:00
|
|
|
const dataTest = useSelector(selectedTest);
|
2023-01-16 15:24:08 +03:00
|
|
|
const [index, setIndex] = useState(0);
|
|
|
|
const [checkedValues, setCheckedValues] = useState([]);
|
2023-04-19 20:22:06 +03:00
|
|
|
//const [stripValue, setStripValue] = useState(0);
|
2023-01-16 15:24:08 +03:00
|
|
|
const [inputValue, setInputValue] = useState('');
|
2023-04-19 20:22:06 +03:00
|
|
|
|
2023-01-16 15:24:08 +03:00
|
|
|
|
|
|
|
|
|
|
|
const id = localStorage.getItem('id');
|
2022-03-18 16:39:13 +03:00
|
|
|
|
2023-01-16 15:24:08 +03:00
|
|
|
useEffect(() => {
|
2023-04-19 20:22:06 +03:00
|
|
|
// fetch('https://itguild.info/api/user-questionnaire/questionnaires-list?user_id=110').then(response => response.json())
|
|
|
|
// .then(json => console.log(json))
|
2023-01-16 15:24:08 +03:00
|
|
|
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
|
|
|
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();
|
|
|
|
//Проверка на валидацию ответов
|
2023-04-19 20:22:06 +03:00
|
|
|
if (!(checkedValues.length || inputValue)) {
|
|
|
|
alert("Вы не ответили на вопрос")
|
|
|
|
return
|
|
|
|
}
|
2022-03-18 16:39:13 +03:00
|
|
|
|
2023-04-19 20:22:06 +03:00
|
|
|
//отправка ответов на сервер
|
|
|
|
if (questions[index].question_type_id != 3) {
|
|
|
|
//dispatch(fetchUserAnswerOne(checkedValues));
|
2023-01-16 15:24:08 +03:00
|
|
|
} else {
|
2023-04-19 20:22:06 +03:00
|
|
|
console.log(checkedValues);
|
|
|
|
// dispatch(fetchUserAnswersMany(checkedValues));
|
2023-01-16 15:24:08 +03:00
|
|
|
}
|
2023-04-19 20:22:06 +03:00
|
|
|
|
|
|
|
//Проверка на окончание теста
|
|
|
|
if (!(index < questions.length - 1)) {
|
|
|
|
dispatch(setCompleteTest())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch(fetchGetAnswers(questions[index + 1].id));
|
|
|
|
setIndex(prev => prev + 1);
|
|
|
|
setCheckedValues([]);
|
|
|
|
setInputValue('')
|
2023-01-16 15:24:08 +03:00
|
|
|
};
|
|
|
|
|
2023-04-19 20:22:06 +03:00
|
|
|
|
2023-01-16 15:24:08 +03:00
|
|
|
const handleChange = (e) => {
|
|
|
|
const checked = e.target.checked;
|
2023-04-19 20:22:06 +03:00
|
|
|
|
|
|
|
if (questions[index].question_type_id != 3) {
|
|
|
|
setCheckedValues([{
|
|
|
|
user_id: id,
|
|
|
|
user_questionnaire_uuid: dataTest.uuid,
|
|
|
|
question_id: questions[index].id,
|
|
|
|
response_body: e.target.value
|
|
|
|
}])
|
|
|
|
return
|
2023-01-16 15:24:08 +03:00
|
|
|
}
|
2023-04-19 20:22:06 +03:00
|
|
|
|
|
|
|
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)]);
|
2023-01-16 15:24:08 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-04-19 20:22:06 +03:00
|
|
|
console.log('render task');
|
|
|
|
|
|
|
|
|
2023-01-16 15:24:08 +03:00
|
|
|
return (
|
2023-04-19 20:22:06 +03:00
|
|
|
<div className="task">
|
|
|
|
{
|
|
|
|
<div className="task__container">
|
|
|
|
<div className='task__header'>
|
|
|
|
<img src={questionIcon} alt="" />
|
|
|
|
<h3 className="task__title quiz-title_h3">{questions[index].question_body}</h3>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="task__body">
|
|
|
|
<form className='task__form form-task' onSubmit={nextQuestion}>
|
|
|
|
{
|
|
|
|
|
|
|
|
answers.map((answer) => (
|
|
|
|
<GetOptionTask
|
|
|
|
key={answer.id}
|
|
|
|
type={questions[index].question_type_id}
|
|
|
|
handleChange={handleChange}
|
|
|
|
answer={answer}
|
|
|
|
/>
|
|
|
|
))
|
|
|
|
}
|
|
|
|
<div className="form-task__buttons">
|
|
|
|
{/* {
|
|
|
|
index != 0 && <button type='submit' className='form-task__btn quiz-btn quiz-btn_back'
|
|
|
|
onClick={prevQuestion}>Назад</button>
|
|
|
|
} */}
|
|
|
|
{
|
|
|
|
index != questions.length && <button onClick={nextQuestion}
|
|
|
|
className='form-task__btn quiz-btn'>Далее</button>
|
|
|
|
}
|
2023-01-16 15:24:08 +03:00
|
|
|
</div>
|
2023-04-19 20:22:06 +03:00
|
|
|
|
|
|
|
</form>
|
|
|
|
</div>
|
2022-03-18 16:39:13 +03:00
|
|
|
</div>
|
2023-04-19 20:22:06 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
|
2023-01-16 15:24:08 +03:00
|
|
|
)
|
2023-01-13 13:02:48 +03:00
|
|
|
};
|