diff --git a/src/pages/AdminTestsPage/AdminTestsPage.tsx b/src/pages/AdminTestsPage/AdminTestsPage.tsx
index 9cbd372..ee89792 100644
--- a/src/pages/AdminTestsPage/AdminTestsPage.tsx
+++ b/src/pages/AdminTestsPage/AdminTestsPage.tsx
@@ -102,24 +102,27 @@ const AdminTestsPage = () => {
>
View
-
+
{(user?.type === "admin" || user?.id == test.author_id) && (
-
+ <>
+
+
+ >
)}
diff --git a/src/pages/QuestionForm/QuestionForm.tsx b/src/pages/QuestionForm/QuestionForm.tsx
index c83244b..c8561e5 100644
--- a/src/pages/QuestionForm/QuestionForm.tsx
+++ b/src/pages/QuestionForm/QuestionForm.tsx
@@ -34,8 +34,10 @@ const QuestionForm = () => {
const [type, setType] = useState<"single" | "multiple" | "text">("single");
const [difficulty, setDifficulty] = useState(1);
const [categoryId, setCategoryId] = useState("");
+
const [variants, setVariants] = useState([{ id: 1, text: "" }]);
const [correctAnswers, setCorrectAnswers] = useState([]);
+
const [textAnswer, setTextAnswer] = useState("");
const [openAiModal, setOpenAiModal] = useState(false);
const [generatedQuestion, setGeneratedQuestion] =
@@ -52,7 +54,6 @@ const QuestionForm = () => {
useEffect(() => {
if (question || generatedQuestion) {
const q = question ?? generatedQuestion;
- console.log(q);
setTitle(q.title);
setDescription(q.description ?? "");
setType(q.type);
@@ -63,11 +64,7 @@ const QuestionForm = () => {
} else {
setVariants(q.variants.length ? q.variants : [{ id: 1, text: "" }]);
- setCorrectAnswers(
- q.correct_answers.map((i: number) =>
- typeof i === "number" ? i - 1 : 0
- )
- );
+ setCorrectAnswers(q.correct_answers);
}
}
}, [question, generatedQuestion]);
@@ -81,23 +78,30 @@ const QuestionForm = () => {
};
const addVariant = () => {
- setVariants([...variants, { id: variants.length + 1, text: "" }]);
+ setVariants((prev) => [
+ ...prev,
+ {
+ id: Math.max(0, ...prev.map((v) => v.id)) + 1,
+ text: "",
+ },
+ ]);
};
const removeVariant = (index: number) => {
- const newVariants = variants.filter((_, i) => i !== index);
- setVariants(newVariants);
- setCorrectAnswers((prev) => prev.filter((i) => i !== index));
+ const removedId = variants[index].id;
+
+ setVariants((prev) => prev.filter((_, i) => i !== index));
+ setCorrectAnswers((prev) => prev.filter((id) => id !== removedId));
};
- const toggleCorrectAnswer = (index: number) => {
+ const toggleCorrectAnswer = (variantId: number) => {
if (type === "single") {
- setCorrectAnswers([index]);
+ setCorrectAnswers([variantId]);
} else {
setCorrectAnswers((prev) =>
- prev.includes(index)
- ? prev.filter((i) => i !== index)
- : [...prev, index]
+ prev.includes(variantId)
+ ? prev.filter((id) => id !== variantId)
+ : [...prev, variantId]
);
}
};
@@ -126,8 +130,7 @@ const QuestionForm = () => {
difficulty,
category_id: Number(categoryId),
variants: type === "text" ? [] : variants,
- correct_answers:
- type === "text" ? [textAnswer] : correctAnswers.map((i) => i + 1),
+ correct_answers: correctAnswers,
};
if (isUpdate && id) {
@@ -224,7 +227,7 @@ const QuestionForm = () => {
{variants.map((v, i) => (
{
toggleCorrectAnswer(i)}
+ checked={correctAnswers.includes(v.id)}
+ onChange={() => toggleCorrectAnswer(v.id)}
/>
}
label="Correct"
@@ -264,7 +267,6 @@ const QuestionForm = () => {
{!isUpdate && (
-
{(user?.type === "admin" ||
user?.id == question.author.id) && (
-
+ <>
+
+
+ >
)}