Compare commits

..

No commits in common. "dcf0bbbe689fc6eb02098ed5ef116b093107b4bd" and "2588a72b02df491bb15276d800826de0896188fa" have entirely different histories.

2 changed files with 66 additions and 63 deletions

View File

@ -1,7 +1,7 @@
import moment from "moment"; import moment from "moment";
import React, { useState } from "react"; import React, { useState } from "react";
import { useSelector } from "react-redux"; import { useSelector } from "react-redux";
import { useNavigate, useParams } from "react-router-dom"; import { useParams } from "react-router-dom";
import { questionsSelector } from "@redux/quizSlice"; import { questionsSelector } from "@redux/quizSlice";
@ -18,16 +18,15 @@ import { GetOptionTask } from "./GetOptionTask";
import "./quiz.scss"; import "./quiz.scss";
export const TaskQuiz = ({ timer }) => { export const TaskQuiz = ({ timer }) => {
const navigate = useNavigate();
const { restart } = timer; const { restart } = timer;
const { uuid } = useParams(); const { uuid } = useParams();
const userId = localStorage.getItem("id");
const questions = useSelector(questionsSelector); const questions = useSelector(questionsSelector);
const [index, setIndex] = useState(0); const [index, setIndex] = useState(0);
const [isLoadingSendAnswers] = useState(false); const [isLoadingSendAnswers, setLoadingSendAnswers] = useState(false);
const { showNotification } = useNotification(); const { showNotification } = useNotification();
const { userResponses, handleChange } = useHandlerFieldTest({ const { values, handleChange, setValues } = useHandlerFieldTest({
uuid,
questions, questions,
indexQuestion: index, indexQuestion: index,
}); });
@ -35,57 +34,49 @@ export const TaskQuiz = ({ timer }) => {
const nextQuestion = async (e) => { const nextQuestion = async (e) => {
e.preventDefault(); e.preventDefault();
//Проверка на существование овтетов //Проверка на существование овтетов
if (!userResponses[index]) { if (!values.length) {
alert("Вы не ответили на вопрос"); alert("Вы не ответили на вопрос");
return; return;
} }
// setLoadingSendAnswers(true); //отправка ответов на сервер
// .finally(() => setLoadingSendAnswers(false)); setLoadingSendAnswers(true);
await apiRequest(`/user-response/set-responses`, {
method: "POST",
data: values,
})
.then((res) => {
if (String(res?.status)[0] !== "2") {
showNotification({
show: true,
text: res?.message || "",
type: "error",
});
return;
}
// отправка ответов на сервер if (index === questions.length - 1) return;
if (questions.length === userResponses.length) {
await apiRequest(`/user-response/set-responses`, { //установка таймера на вопрос если он существует
method: "POST", if (questions[index + 1]?.time_limit !== "00:00:00") setValueTimer();
data: {
user_id: userId, // очищение полей и переход на следующий вопрос
user_questionnaire_uuid: uuid, setIndex((prev) => prev + 1);
userResponses: JSON.stringify(userResponses), setValues([]);
}, })
}).then(() => { .catch((e) => {
showNotification({ showNotification({
show: true, show: true,
text: "Тест успешно пройден", text: e?.message || "",
type: "success", type: "error",
}); });
navigate("/quiz"); })
// if (String(res?.status)[0] !== "2") { .finally(() => setLoadingSendAnswers(false));
// showNotification({
// show: true,
// text: res?.message || "",
// type: "error",
// });
// return;
// }
});
// .catch((e) => {
// showNotification({
// show: true,
// text: e?.message || "",
// type: "error",
// });
// })
}
//установка таймера на вопрос если он существует
if (questions[index + 1]?.time_limit !== "00:00:00") setValueTimer();
// переход на следующий вопрос
setIndex((prev) => (questions[prev + 1] ? prev + 1 : prev));
}; };
const complete = (e) => { const complete = (e) => {
e.preventDefault(); e.preventDefault();
console.log(values);
}; };
const setValueTimer = () => { const setValueTimer = () => {
@ -98,6 +89,7 @@ export const TaskQuiz = ({ timer }) => {
); );
}; };
console.log(questions);
return ( return (
<div className="task"> <div className="task">
{questions ? ( {questions ? (
@ -119,6 +111,7 @@ export const TaskQuiz = ({ timer }) => {
<div className="form-task__group"> <div className="form-task__group">
<textarea <textarea
className="form-task__field" className="form-task__field"
value={values[0]?.response_body}
onChange={handleChange} onChange={handleChange}
/> />
</div> </div>
@ -135,7 +128,7 @@ export const TaskQuiz = ({ timer }) => {
<div className="form-task__buttons"> <div className="form-task__buttons">
<button <button
onClick={nextQuestion} onClick={nextQuestion}
// disabled={isLoadingSendAnswers} disabled={isLoadingSendAnswers}
className="form-task__btn quiz-btn" className="form-task__btn quiz-btn"
> >
{isLoadingSendAnswers ? ( {isLoadingSendAnswers ? (

View File

@ -1,32 +1,42 @@
import { useState } from "react"; import { useState } from "react";
export const useHandlerFieldTest = ({ questions, indexQuestion }) => { export const useHandlerFieldTest = ({ uuid, questions, indexQuestion }) => {
const [userResponses, setUserResponses] = useState([]); const [values, setValues] = useState([]);
const id = localStorage.getItem("id");
const handleChangeCheckbox = (e) => { const handleChangeCheckbox = (e) => {
setUserResponses((prev) => [ if (!e.target.checked) {
...prev.filter( setValues((prev) => [
(item) => item.question_id !== questions[indexQuestion].id ...prev.filter((item) => item.response_body !== e.target.value),
), ]);
return;
}
setValues((prev) => [
...prev,
{ {
user_id: id,
user_questionnaire_uuid: uuid,
question_id: questions[indexQuestion].id, question_id: questions[indexQuestion].id,
response_body: e.target.value, response_body: e.target.value,
answer_id: e.target.id ? e.target.id : questions[indexQuestion].id,
}, },
]); ]);
}; };
// const handleFieldsForm = (e) => { const handleFieldsForm = (e) => {
// setValues([ setValues([
// { {
// user_id: id, user_id: id,
// user_questionnaire_uuid: uuid, user_questionnaire_uuid: uuid,
// question_id: questions[indexQuestion].id, question_id: questions[indexQuestion].id,
// response_body: e.target.value, response_body: e.target.value,
// }, },
// ]); ]);
// }; };
const handleChange = (e) => { const handleChange = (e) => {
if (+questions[indexQuestion].question_type_id !== 3) {
handleFieldsForm(e);
return;
}
handleChangeCheckbox(e); handleChangeCheckbox(e);
}; };
return { handleChange, userResponses }; return { handleChange, values, setValues };
}; };