Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Edit and Review Page #7

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions trivia-forge/src/Pages/TriviaGenPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,23 @@ 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();



const handleSubmit = async (event) => {
event.preventDefault(); // prevent default form submission behavior(browser reload)

let prompt = `Generate ${numberOfQuestions} trivia questions about ${category}.`;
if (isMultipleChoice) {
prompt += "Each question should include four multiple-choice options, the correct answer, and a fun fact. Separate each component with a newline.";
} else {
prompt += "Each question should come with its answer and a fun fact. Separate each component with a newline.";
}

// api call
try {
const prompt = `Generate ${numberOfQuestions} trivia questions about ${category}.`;

// API call to OpenAI
const completion = await openai.chat.completions.create({
Expand All @@ -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);
}
Expand Down Expand Up @@ -74,8 +82,14 @@ function TriviaGenPage() {
/>
</div>
<div className="form-group">
<label htmlFor="triviaAnswer">placeholder</label>
<input type="text" className="form-control" id="triviaAnswer" placeholder="Place holder" />
<label htmlFor="multipleChoice">Include Multiple Choice Answers:</label>
<input
type="checkbox"
checked={isMultipleChoice}
onChange={e => setIsMultipleChoice(e.target.value)}
//className="form-control"
id="multipleChoice"
/>
</div>
<button type="submit" className="btn btn-primary">Generate</button>
</form>
Expand Down
2 changes: 2 additions & 0 deletions trivia-forge/src/Pages/TriviaReviewPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Loading