From 42d01df4c81800397d6d38ef980c653080bf1746 Mon Sep 17 00:00:00 2001 From: maxidragon Date: Thu, 21 Sep 2023 20:53:13 +0200 Subject: [PATCH] Add simple quiz validation --- .../src/Components/Forms/EditQuizInfoForm.tsx | 35 ++++++++++++++++++- .../src/Components/Questions/QuestionRow.tsx | 5 +++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/frontend/src/Components/Forms/EditQuizInfoForm.tsx b/frontend/src/Components/Forms/EditQuizInfoForm.tsx index 710535e..68bde05 100644 --- a/frontend/src/Components/Forms/EditQuizInfoForm.tsx +++ b/frontend/src/Components/Forms/EditQuizInfoForm.tsx @@ -4,19 +4,29 @@ import { Checkbox, FormControlLabel, TextField, + Typography, } from "@mui/material"; -import { Quiz } from "../../logic/interfaces"; +import { Answer, Question, Quiz } from "../../logic/interfaces"; import EditIcon from "@mui/icons-material/Edit"; import DeleteIcon from "@mui/icons-material/Delete"; import { deleteQuiz, updateQuiz } from "../../logic/quizzes"; import { enqueueSnackbar } from "notistack"; import { useConfirm } from "material-ui-confirm"; import { useNavigate } from "react-router-dom"; +import { useCallback, useEffect, useState } from "react"; +import { getQuestionsForQuiz } from "../../logic/questions"; const EditQuizInfoForm = (props: { quiz: Quiz; updateQuiz: (quiz: Quiz) => void; }) => { + const [questions, setQuestions] = useState([]); + const [canBePublished, setCanBePublished] = useState(false); + const fetchQuestions = useCallback(async () => { + const response = await getQuestionsForQuiz(props.quiz.id); + setQuestions(response); + }, [props.quiz.id]); + const confirm = useConfirm(); const navigate = useNavigate(); const handleEdit = async () => { @@ -52,8 +62,30 @@ const EditQuizInfoForm = (props: { }); }; + useEffect(() => { + fetchQuestions(); + }, [fetchQuestions]); + + useEffect(() => { + const canBePublishedCheck = + questions.length > 0 && + props.quiz.name && + props.quiz.description && + questions.every((question: Question) => question.answers.length > 0) && + questions.every((question: Question) => + question.answers.some((answer: Answer) => answer.is_correct), + ); + setCanBePublished(canBePublishedCheck || false); + }, [questions, props.quiz.name, props.quiz.description]); return ( <> + {!canBePublished && ( + + This quiz cannot be published yet. Please be sure to fill in all the + fields and add at least one question with at least one answer marked + as correct. + + )} {props.quiz && ( } label="Public" diff --git a/frontend/src/Components/Questions/QuestionRow.tsx b/frontend/src/Components/Questions/QuestionRow.tsx index dd364e4..8f92891 100644 --- a/frontend/src/Components/Questions/QuestionRow.tsx +++ b/frontend/src/Components/Questions/QuestionRow.tsx @@ -127,6 +127,11 @@ const QuestionRow = (props: { + {!row.answers.some((answer) => answer.is_correct) && ( + + This question has no correct answer! + + )}