From 43fa15768754889a14268065aec02d7a1df82270 Mon Sep 17 00:00:00 2001 From: juanpabloacosta Date: Thu, 30 Nov 2023 23:33:35 -0500 Subject: [PATCH] Laid out the verbal question exercise, but incomplete (needs text to speech). --- .../components/VerbalQuestionsExercise.tsx | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 frontend/src/components/VerbalQuestionsExercise.tsx diff --git a/frontend/src/components/VerbalQuestionsExercise.tsx b/frontend/src/components/VerbalQuestionsExercise.tsx new file mode 100644 index 0000000..1d8d01f --- /dev/null +++ b/frontend/src/components/VerbalQuestionsExercise.tsx @@ -0,0 +1,117 @@ +import { useState } from 'react'; +import { getAIMarking } from '../logic/sdk'; +import ClipLoader from "react-spinners/ClipLoader"; +import { Icon } from '@iconify/react'; + +interface VerbalQuestionProps { + question: string; + wordBank: string[]; + expected: string; + chapter: string; + language: string; + onQuestionSubmit: Function; +} + +export default function VerbalQuestionsExercise({question, wordBank, expected, chapter, language, onQuestionSubmit}: VerbalQuestionProps) { + 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 [isListening, setListening] = useState(false); + + const handleWordBankClick = (word: string) => { + setSelectedWords([...selectedWords, word]); + setRemainingWords(remainingWords.filter((w) => w !== word)); + } + + const handleSelectedWordClick = (word: string) => { + setRemainingWords([...remainingWords, word]); + setSelectedWords(selectedWords.filter((w) => w !== word)); + } + + const handleSubmit = () => { + const userAnswer = selectedWords.join(" ") + lastPunctuation; + 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) => { + if (!answered) { + return ( +
+
+ {remainingWords.map((word, index) => ( + handleWordBankClick(word)}> + {word} + + ))} +
+ +
+ + ) + } else { + return ( +
+

{correct}

+

{reason}

+ +
+ ) + } + } + + const handleListenToQuestion = () => { + // TODO: Play static file that holds the audio for the current + console.log("Heard you"); + } + + return ( +
+

What do you hear?

+
+ +
+
+ {selectedWords.map((word, index) => ( + handleSelectedWordClick(word)}> + {word} + + ))} +
+ {ResponseSection(correct, reason)} +
+ )}