From 2eab4e97afc5ef4c15f86c1a21918018bb753e33 Mon Sep 17 00:00:00 2001 From: juanpabloacosta Date: Thu, 30 Nov 2023 07:22:13 -0500 Subject: [PATCH] Wrote component for a WrittenQuestion type exercise. --- .../components/WrittenQuestionExercise.tsx | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 frontend/src/components/WrittenQuestionExercise.tsx diff --git a/frontend/src/components/WrittenQuestionExercise.tsx b/frontend/src/components/WrittenQuestionExercise.tsx new file mode 100644 index 0000000..7a910d2 --- /dev/null +++ b/frontend/src/components/WrittenQuestionExercise.tsx @@ -0,0 +1,103 @@ +import ChatBoxBorder from '../assets/img/chatbox-border.svg'; +import { useState } from 'react'; +import { getAIMarking } from '../logic/sdk'; + +interface WrittenQuestionProps { + question: string; + wordBank: string[]; + expected: string; + chapter: string; + language: string; + setCurrQuestion: Function; +} + +export default function WrittenQuestionExercise({question, wordBank, expected, chapter, language}: 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 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; + getAIMarking( + question, + userAnswer, + expected, + chapter, + language + ).then((res) => { + setAnswered(true); + setCorrect(res.correct); + setReason(res.reason); + }) + } + + const ResponseSection = (correct: string | null, reason: string | null) => { + if (!answered) { + return ( +
+
+ {remainingWords.map((word, index) => ( + handleWordBankClick(word)}> + {word} + + ))} +
+ +
+ + ) + } else { + return ( +
+

{correct}

+

{reason}

+
+ ) + } + } + + return ( +
+
+ {question} +
+
+ {selectedWords.map((word, index) => ( + handleSelectedWordClick(word)}> + {word} + + ))} +
+ {ResponseSection(correct, reason)} + {/*
+ {remainingWords.map((word, index) => ( + handleWordBankClick(word)}> + {word} + + ))} +
+ */} +
+ )}