Skip to content

Commit

Permalink
Merge branch 'master' of github.com:HackerDom/ructf-2023
Browse files Browse the repository at this point in the history
  • Loading branch information
lololozhkin committed Oct 30, 2023
2 parents bbabe73 + 198af7f commit 4d99912
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
27 changes: 18 additions & 9 deletions services/rustest/front/src/pages/rustestlogic/CreateRustest.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React, { useEffect, useState } from "react";
import { Button, Card, Col, Container, Form, Row } from "react-bootstrap";
import { Button, Card, Col, Container, Form, Row, Alert } from "react-bootstrap";
import axios from "axios";
import { useNavigate } from "react-router-dom";

function CreateRustest() {
const navigate = useNavigate();
const [token, setToken] = useState(localStorage.getItem("jwtToken"));
const [error, setError] = useState("");

useEffect(() => {
setToken(localStorage.getItem("jwtToken"));
Expand All @@ -25,7 +26,7 @@ function CreateRustest() {
{
question: "",
allowed_answers: ["", ""],
correct_idx: 0,
correct_idx: "",
},
],
});
Expand All @@ -45,7 +46,7 @@ function CreateRustest() {
{
question: "",
answers: [{ answer: "", isCorrect: false }],
correct_idx: 0,
correct_idx: "",
},
],
});
Expand All @@ -70,7 +71,7 @@ function CreateRustest() {

const handleCorrectAnswerChange = (questionIndex, answerIndex) => {
const updatedQuestions = [...formData.questions];
updatedQuestions[questionIndex].correct_idx = (parseInt(answerIndex) + 1);
updatedQuestions[questionIndex].correct_idx = answerIndex;
setFormData({ ...formData, questions: updatedQuestions });
};

Expand All @@ -85,9 +86,12 @@ function CreateRustest() {
.then((response) => {
navigate(`/rustest/${response.data.id}/preview`)
})
.catch((error) => {
// Обработка ошибки при создании теста
console.error("Error creating test", error);
.catch((responseError) => {
if (responseError.response && responseError.response.status >= 400) {
setError("Validation error");
} else {
setError("An error occurred.");
}
});
};

Expand Down Expand Up @@ -169,9 +173,9 @@ function CreateRustest() {
<Form.Check
type="radio"
name={`correctAnswer_${questionIndex}`}
label={`Answer ${answerIndex + 1}`}
label={`Answer ${answerIndex}`}
id={`answer_${questionIndex}_${answerIndex}`}
checked={answerIndex === question.correct_idx - 1}
checked={answerIndex === question.correct_idx}
onChange={() => handleCorrectAnswerChange(questionIndex, answerIndex)}
/>
<Form.Control
Expand Down Expand Up @@ -203,6 +207,11 @@ function CreateRustest() {
</Form>
</Card.Body>
</Card>
{error && (
<Alert variant="danger">
{error}
</Alert>
)}
</Container>
);
}
Expand Down
4 changes: 2 additions & 2 deletions services/rustest/front/src/pages/rustestlogic/SolveRustest.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function SolveRustest() {
};

const handleAnswerChange = (event) => {
setFormData({ ...formData, answer: (parseInt(event.target.value) + 1) });
setFormData({ ...formData, answer: event.target.value });
};

return (
Expand Down Expand Up @@ -138,7 +138,7 @@ function SolveRustest() {
id={`answer-${index}`}
label={answer}
value={index}
checked={formData.answer - 1 === index}
checked={formData.answer === index}
onChange={handleAnswerChange}
/>
))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function RustestQuestionsList({ questions }) {
as="li"
style={{
backgroundColor:
answerIndex === question.correct_idx - 1
answerIndex === question.correct_idx
? "lightgreen"
: "transparent",
}}
Expand Down

0 comments on commit 4d99912

Please sign in to comment.