From 57d6335e16da924270a31ca4d038ff7abbfd3991 Mon Sep 17 00:00:00 2001 From: demuths1 Date: Thu, 25 Apr 2024 15:18:22 -0400 Subject: [PATCH 1/2] added multiple choice selector, return questions with answers --- trivia-forge/src/Pages/TriviaGenPage.jsx | 22 +++++++++++++++++---- trivia-forge/src/Pages/TriviaReviewPage.jsx | 2 ++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/trivia-forge/src/Pages/TriviaGenPage.jsx b/trivia-forge/src/Pages/TriviaGenPage.jsx index ebdcadb4..93a04aac 100644 --- a/trivia-forge/src/Pages/TriviaGenPage.jsx +++ b/trivia-forge/src/Pages/TriviaGenPage.jsx @@ -14,6 +14,7 @@ function TriviaGenPage() { // state hooks for managaing number of questions and catergory input by user const [numberOfQuestions, setNumberOfQuestions] = useState(''); const [category, setCategory] = useState(''); + const [isMultipleChoice, setIsMultipleChoice] = useState(false); const navigate = useNavigate(); @@ -21,9 +22,15 @@ function TriviaGenPage() { const handleSubmit = async (event) => { event.preventDefault(); // prevent default form submission behavior(browser reload) + let prompt = `Generate ${numberOfQuestions} trivia questions about ${category}.`; + if (isMultipleChoice) { + prompt += " with four multiple-choice answers each followed by the correct answer"; + } else { + prompt += " with the answers for each"; + } + // api call try { - const prompt = `Generate ${numberOfQuestions} trivia questions about ${category}.`; // API call to OpenAI const completion = await openai.chat.completions.create({ @@ -37,8 +44,9 @@ function TriviaGenPage() { }); const questions = completion.choices[0].message.content.split('\n'); // store trivia questions + // state property to pass data as object to new route navigate('/review', { state: { questions } }); - //console.log(completion.choices[0]); + //console.log(completion.choices[0].message); } catch (error) { console.error('Error calling OpenAI API:', error); } @@ -74,8 +82,14 @@ function TriviaGenPage() { />
- - + + setIsMultipleChoice(e.target.value)} + //className="form-control" + id="multipleChoice" + />
diff --git a/trivia-forge/src/Pages/TriviaReviewPage.jsx b/trivia-forge/src/Pages/TriviaReviewPage.jsx index f8ed6f66..a15fa198 100644 --- a/trivia-forge/src/Pages/TriviaReviewPage.jsx +++ b/trivia-forge/src/Pages/TriviaReviewPage.jsx @@ -2,6 +2,8 @@ import React from 'react'; import { useLocation } from 'react-router-dom'; // used to access passed state function TriviaReviewPage() { + // Reference: https://reactrouter.com/en/main/hooks/use-location + // pulls object from state property in TriviaGenPage const location = useLocation(); const { questions } = location.state; From b11a86856f1eaab7691293caf1d2735be600f32b Mon Sep 17 00:00:00 2001 From: demuths1 Date: Thu, 25 Apr 2024 16:06:39 -0400 Subject: [PATCH 2/2] added fun fact feature --- trivia-forge/src/Pages/TriviaGenPage.jsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/trivia-forge/src/Pages/TriviaGenPage.jsx b/trivia-forge/src/Pages/TriviaGenPage.jsx index 93a04aac..4d53adfb 100644 --- a/trivia-forge/src/Pages/TriviaGenPage.jsx +++ b/trivia-forge/src/Pages/TriviaGenPage.jsx @@ -24,9 +24,9 @@ function TriviaGenPage() { let prompt = `Generate ${numberOfQuestions} trivia questions about ${category}.`; if (isMultipleChoice) { - prompt += " with four multiple-choice answers each followed by the correct answer"; + prompt += "Each question should include four multiple-choice options, the correct answer, and a fun fact. Separate each component with a newline."; } else { - prompt += " with the answers for each"; + prompt += "Each question should come with its answer and a fun fact. Separate each component with a newline."; } // api call