diff --git a/frontend/src/components/WrittenQuestionExercise.tsx b/frontend/src/components/WrittenQuestionExercise.tsx index 7a910d2..6ffc8e9 100644 --- a/frontend/src/components/WrittenQuestionExercise.tsx +++ b/frontend/src/components/WrittenQuestionExercise.tsx @@ -1,6 +1,6 @@ -import ChatBoxBorder from '../assets/img/chatbox-border.svg'; import { useState } from 'react'; import { getAIMarking } from '../logic/sdk'; +import ClipLoader from "react-spinners/ClipLoader"; interface WrittenQuestionProps { question: string; @@ -8,16 +8,17 @@ interface WrittenQuestionProps { expected: string; chapter: string; language: string; - setCurrQuestion: Function; + onQuestionSubmit: Function; } -export default function WrittenQuestionExercise({question, wordBank, expected, chapter, language}: WrittenQuestionProps) { +export default function WrittenQuestionExercise({question, wordBank, expected, chapter, language, onQuestionSubmit}: WrittenQuestionProps) { const [selectedWords, setSelectedWords] = useState([]); const [remainingWords, setRemainingWords] = useState(wordBank); const lastPunctuation = question[question.length - 1]; const [answered, setAnswered] = useState(false); const [correct, setCorrect] = useState(""); const [reason, setReason] = useState(""); + const [loading, setLoading] = useState(false); const handleWordBankClick = (word: string) => { setSelectedWords([...selectedWords, word]); @@ -31,17 +32,25 @@ export default function WrittenQuestionExercise({question, wordBank, expected, c const handleSubmit = () => { const userAnswer = selectedWords.join(" ") + lastPunctuation; - getAIMarking( - question, - userAnswer, - expected, - chapter, - language - ).then((res) => { - setAnswered(true); - setCorrect(res.correct); - setReason(res.reason); - }) + if (answered) { + setAnswered(false); + onQuestionSubmit(); + } else { + setLoading(true); + getAIMarking( + question, + userAnswer.toLowerCase(), + expected.toLowerCase(), + chapter, + language + ).then((res) => { + setLoading(false); + setAnswered(true); + setCorrect(res.correct); + setReason(res.reason); + }).catch((e) => console.error(e)); + } + } const ResponseSection = (correct: string | null, reason: string | null) => { @@ -58,15 +67,25 @@ export default function WrittenQuestionExercise({question, wordBank, expected, c ))} - + ) } else { return (
-

{correct}

-

{reason}

+
+

{correct}

+

{reason}

+ +
) } @@ -88,16 +107,5 @@ export default function WrittenQuestionExercise({question, wordBank, expected, c ))} {ResponseSection(correct, reason)} - {/*
- {remainingWords.map((word, index) => ( - handleWordBankClick(word)}> - {word} - - ))} -
- */} )}