149 lines
4.3 KiB
JavaScript
Raw Normal View History

2023-05-25 16:42:37 +03:00
import React, { useEffect, useState } from "react";
2023-05-31 08:36:15 +03:00
import { useDispatch, useSelector } from "react-redux";
2022-03-18 16:39:13 +03:00
2023-05-25 16:42:37 +03:00
import {
answersSelector,
2023-05-31 11:24:46 +03:00
fetchGetAnswers, // fetchUserAnswerOne,
// fetchUserAnswersMany,
2023-05-25 16:42:37 +03:00
questionsSelector,
2023-05-31 11:24:46 +03:00
selectedTest, // setAnswers,
2023-05-25 16:42:37 +03:00
setCompleteTest,
2023-05-30 10:10:34 +03:00
} from "@redux/quizSlice";
2023-05-25 16:42:37 +03:00
2023-05-31 08:36:15 +03:00
import { apiRequest } from "@api/request";
2023-05-30 10:10:34 +03:00
import questionIcon from "assets/images/question.png";
2022-03-18 16:39:13 +03:00
2023-05-31 08:36:15 +03:00
import { GetOptionTask } from "./GetOptionTask";
2023-05-31 11:24:46 +03:00
// import { HeaderQuiz } from "./HeaderQuiz";
// import { Progressbar } from "./ProgressbarQuiz";
2023-05-25 16:42:37 +03:00
import "./quiz.scss";
export const TaskQuiz = () => {
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);
const dataTest = useSelector(selectedTest);
const [index, setIndex] = useState(0);
const [checkedValues, setCheckedValues] = useState([]);
2023-04-19 20:22:06 +03:00
//const [stripValue, setStripValue] = useState(0);
2023-05-25 16:42:37 +03:00
const [inputValue, setInputValue] = useState("");
2023-05-25 16:42:37 +03:00
const id = localStorage.getItem("id");
2022-03-18 16:39:13 +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-05-25 16:42:37 +03:00
apiRequest(`/question/get-questions?uuid=${dataTest.uuid}`).then(
(response) => {
dispatch(fetchGetAnswers(response[0].id));
setStripValue(((+index + 1) * 100) / response.length);
2023-12-04 18:01:04 +03:00
},
2023-05-25 16:42:37 +03:00
);
}, [dispatch]);
const nextQuestion = async (e) => {
e.preventDefault();
//Проверка на валидацию ответов
2023-04-19 20:22:06 +03:00
if (!(checkedValues.length || inputValue)) {
2023-05-25 16:42:37 +03:00
alert("Вы не ответили на вопрос");
return;
2023-04-19 20:22:06 +03:00
}
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));
} else {
2023-05-25 16:42:37 +03:00
console.log(checkedValues);
2023-04-19 20:22:06 +03:00
// dispatch(fetchUserAnswersMany(checkedValues));
}
2023-04-19 20:22:06 +03:00
//Проверка на окончание теста
if (!(index < questions.length - 1)) {
2023-05-25 16:42:37 +03:00
dispatch(setCompleteTest());
return;
2023-04-19 20:22:06 +03:00
}
dispatch(fetchGetAnswers(questions[index + 1].id));
2023-05-25 16:42:37 +03:00
setIndex((prev) => prev + 1);
2023-04-19 20:22:06 +03:00
setCheckedValues([]);
2023-05-25 16:42:37 +03:00
setInputValue("");
};
const handleChange = (e) => {
const checked = e.target.checked;
2023-04-19 20:22:06 +03:00
if (questions[index].question_type_id != 3) {
2023-05-25 16:42:37 +03:00
setCheckedValues([
{
user_id: id,
user_questionnaire_uuid: dataTest.uuid,
question_id: questions[index].id,
response_body: e.target.value,
},
]);
return;
}
2023-04-19 20:22:06 +03:00
2023-05-25 16:42:37 +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-05-25 16:42:37 +03:00
console.log("render task");
2023-04-19 20:22:06 +03:00
return (
2023-04-19 20:22:06 +03:00
<div className="task">
{
<div className="task__container">
2023-05-25 16:42:37 +03:00
<div className="task__header">
2023-04-19 20:22:06 +03:00
<img src={questionIcon} alt="" />
2023-05-25 16:42:37 +03:00
<h3 className="task__title quiz-title_h3">
{questions[index].question_body}
</h3>
2023-04-19 20:22:06 +03:00
</div>
<div className="task__body">
2023-05-25 16:42:37 +03:00
<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}
/>
))}
2023-04-19 20:22:06 +03:00
<div className="form-task__buttons">
{/* {
index != 0 && <button type='submit' className='form-task__btn quiz-btn quiz-btn_back'
onClick={prevQuestion}>Назад</button>
} */}
2023-05-25 16:42:37 +03:00
{index != questions.length && (
<button
onClick={nextQuestion}
className="form-task__btn quiz-btn"
>
Далее
</button>
)}
</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-05-25 16:42:37 +03:00
);
};