fixed adding and removing question variants
This commit is contained in:
parent
bf919daaf8
commit
00d49635d8
@ -102,6 +102,9 @@ const AdminTestsPage = () => {
|
|||||||
>
|
>
|
||||||
View
|
View
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
|
{(user?.type === "admin" || user?.id == test.author_id) && (
|
||||||
|
<>
|
||||||
<Button
|
<Button
|
||||||
size="small"
|
size="small"
|
||||||
variant="outlined"
|
variant="outlined"
|
||||||
@ -111,7 +114,6 @@ const AdminTestsPage = () => {
|
|||||||
>
|
>
|
||||||
Update
|
Update
|
||||||
</Button>
|
</Button>
|
||||||
{(user?.type === "admin" || user?.id == test.author_id) && (
|
|
||||||
<Button
|
<Button
|
||||||
size="small"
|
size="small"
|
||||||
variant="outlined"
|
variant="outlined"
|
||||||
@ -120,6 +122,7 @@ const AdminTestsPage = () => {
|
|||||||
>
|
>
|
||||||
Delete
|
Delete
|
||||||
</Button>
|
</Button>
|
||||||
|
</>
|
||||||
)}
|
)}
|
||||||
</TableCell>
|
</TableCell>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
|
|||||||
@ -34,8 +34,10 @@ const QuestionForm = () => {
|
|||||||
const [type, setType] = useState<"single" | "multiple" | "text">("single");
|
const [type, setType] = useState<"single" | "multiple" | "text">("single");
|
||||||
const [difficulty, setDifficulty] = useState(1);
|
const [difficulty, setDifficulty] = useState(1);
|
||||||
const [categoryId, setCategoryId] = useState<number | "">("");
|
const [categoryId, setCategoryId] = useState<number | "">("");
|
||||||
|
|
||||||
const [variants, setVariants] = useState<Variant[]>([{ id: 1, text: "" }]);
|
const [variants, setVariants] = useState<Variant[]>([{ id: 1, text: "" }]);
|
||||||
const [correctAnswers, setCorrectAnswers] = useState<number[]>([]);
|
const [correctAnswers, setCorrectAnswers] = useState<number[]>([]);
|
||||||
|
|
||||||
const [textAnswer, setTextAnswer] = useState("");
|
const [textAnswer, setTextAnswer] = useState("");
|
||||||
const [openAiModal, setOpenAiModal] = useState(false);
|
const [openAiModal, setOpenAiModal] = useState(false);
|
||||||
const [generatedQuestion, setGeneratedQuestion] =
|
const [generatedQuestion, setGeneratedQuestion] =
|
||||||
@ -52,7 +54,6 @@ const QuestionForm = () => {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (question || generatedQuestion) {
|
if (question || generatedQuestion) {
|
||||||
const q = question ?? generatedQuestion;
|
const q = question ?? generatedQuestion;
|
||||||
console.log(q);
|
|
||||||
setTitle(q.title);
|
setTitle(q.title);
|
||||||
setDescription(q.description ?? "");
|
setDescription(q.description ?? "");
|
||||||
setType(q.type);
|
setType(q.type);
|
||||||
@ -63,11 +64,7 @@ const QuestionForm = () => {
|
|||||||
} else {
|
} else {
|
||||||
setVariants(q.variants.length ? q.variants : [{ id: 1, text: "" }]);
|
setVariants(q.variants.length ? q.variants : [{ id: 1, text: "" }]);
|
||||||
|
|
||||||
setCorrectAnswers(
|
setCorrectAnswers(q.correct_answers);
|
||||||
q.correct_answers.map((i: number) =>
|
|
||||||
typeof i === "number" ? i - 1 : 0
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [question, generatedQuestion]);
|
}, [question, generatedQuestion]);
|
||||||
@ -81,23 +78,30 @@ const QuestionForm = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const addVariant = () => {
|
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 removeVariant = (index: number) => {
|
||||||
const newVariants = variants.filter((_, i) => i !== index);
|
const removedId = variants[index].id;
|
||||||
setVariants(newVariants);
|
|
||||||
setCorrectAnswers((prev) => prev.filter((i) => i !== index));
|
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") {
|
if (type === "single") {
|
||||||
setCorrectAnswers([index]);
|
setCorrectAnswers([variantId]);
|
||||||
} else {
|
} else {
|
||||||
setCorrectAnswers((prev) =>
|
setCorrectAnswers((prev) =>
|
||||||
prev.includes(index)
|
prev.includes(variantId)
|
||||||
? prev.filter((i) => i !== index)
|
? prev.filter((id) => id !== variantId)
|
||||||
: [...prev, index]
|
: [...prev, variantId]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -126,8 +130,7 @@ const QuestionForm = () => {
|
|||||||
difficulty,
|
difficulty,
|
||||||
category_id: Number(categoryId),
|
category_id: Number(categoryId),
|
||||||
variants: type === "text" ? [] : variants,
|
variants: type === "text" ? [] : variants,
|
||||||
correct_answers:
|
correct_answers: correctAnswers,
|
||||||
type === "text" ? [textAnswer] : correctAnswers.map((i) => i + 1),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (isUpdate && id) {
|
if (isUpdate && id) {
|
||||||
@ -224,7 +227,7 @@ const QuestionForm = () => {
|
|||||||
</Typography>
|
</Typography>
|
||||||
{variants.map((v, i) => (
|
{variants.map((v, i) => (
|
||||||
<Paper
|
<Paper
|
||||||
key={i}
|
key={v.id}
|
||||||
sx={{
|
sx={{
|
||||||
display: "flex",
|
display: "flex",
|
||||||
alignItems: "center",
|
alignItems: "center",
|
||||||
@ -241,8 +244,8 @@ const QuestionForm = () => {
|
|||||||
<FormControlLabel
|
<FormControlLabel
|
||||||
control={
|
control={
|
||||||
<Checkbox
|
<Checkbox
|
||||||
checked={correctAnswers.includes(i)}
|
checked={correctAnswers.includes(v.id)}
|
||||||
onChange={() => toggleCorrectAnswer(i)}
|
onChange={() => toggleCorrectAnswer(v.id)}
|
||||||
/>
|
/>
|
||||||
}
|
}
|
||||||
label="Correct"
|
label="Correct"
|
||||||
@ -264,7 +267,6 @@ const QuestionForm = () => {
|
|||||||
</Button>
|
</Button>
|
||||||
{!isUpdate && (
|
{!isUpdate && (
|
||||||
<Button
|
<Button
|
||||||
|
|
||||||
variant="outlined"
|
variant="outlined"
|
||||||
sx={{ ml: 2 }}
|
sx={{ ml: 2 }}
|
||||||
onClick={() => setOpenAiModal(true)}
|
onClick={() => setOpenAiModal(true)}
|
||||||
|
|||||||
@ -104,6 +104,9 @@ const QuestionsPage = () => {
|
|||||||
View
|
View
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
|
{(user?.type === "admin" ||
|
||||||
|
user?.id == question.author.id) && (
|
||||||
|
<>
|
||||||
<Button
|
<Button
|
||||||
variant="outlined"
|
variant="outlined"
|
||||||
color="primary"
|
color="primary"
|
||||||
@ -113,8 +116,6 @@ const QuestionsPage = () => {
|
|||||||
>
|
>
|
||||||
Update
|
Update
|
||||||
</Button>
|
</Button>
|
||||||
{(user?.type === "admin" ||
|
|
||||||
user?.id == question.author.id) && (
|
|
||||||
<Button
|
<Button
|
||||||
variant="outlined"
|
variant="outlined"
|
||||||
color="error"
|
color="error"
|
||||||
@ -123,6 +124,7 @@ const QuestionsPage = () => {
|
|||||||
>
|
>
|
||||||
Delete
|
Delete
|
||||||
</Button>
|
</Button>
|
||||||
|
</>
|
||||||
)}
|
)}
|
||||||
</TableCell>
|
</TableCell>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user