Skip to content
Open
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
11 changes: 10 additions & 1 deletion src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@ import QuestionList from "./QuestionList";

function App() {
const [page, setPage] = useState("List");
const [questions, setQuestions] = useState([]);

const handleNewQuestion = (newQuestion) => {
setQuestions([...questions, newQuestion]);
};

return (
<main>
<AdminNavBar onChangePage={setPage} />
{page === "Form" ? <QuestionForm /> : <QuestionList />}
{page === "Form" ? (
<QuestionForm handleNewQuestion={handleNewQuestion} />
) : (
<QuestionList questions={questions} setQuestions={setQuestions} />
)}
</main>
);
}
Expand Down
32 changes: 28 additions & 4 deletions src/components/QuestionForm.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import React, { useState } from "react";

function QuestionForm(props) {
const [formData, setFormData] = useState({
function QuestionForm({ handleNewQuestion }) {
const emptyForm = {
prompt: "",
answer1: "",
answer2: "",
answer3: "",
answer4: "",
correctIndex: 0,
});
};
const [formData, setFormData] = useState(emptyForm);

function handleChange(event) {
setFormData({
Expand All @@ -19,7 +20,30 @@ function QuestionForm(props) {

function handleSubmit(event) {
event.preventDefault();
console.log(formData);

const newQuestion = {
prompt: formData.prompt,
answers: [
formData.answer1,
formData.answer2,
formData.answer3,
formData.answer4,
],
correctIndex: formData.correctIndex,
};

fetch("http://localhost:4000/questions", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(newQuestion),
})
.then((r) => r.json())
.then((questionObj) => {
handleNewQuestion(questionObj);
setFormData(emptyForm);
});
}

return (
Expand Down
31 changes: 26 additions & 5 deletions src/components/QuestionItem.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,44 @@
import React from "react";

function QuestionItem({ question }) {
const { id, prompt, answers, correctIndex } = question;

function QuestionItem({
id,
prompt,
answers,
correctIndex,
onDelete,
onAnswerChange,
}) {
const options = answers.map((answer, index) => (
<option key={index} value={index}>
{answer}
</option>
));

const changeAnswer = (e) => {
const updatedAnswer = {
correctIndex: e.target.value,
};

fetch(`http://localhost:4000/questions/${id}`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(updatedAnswer),
})
.then((r) => r.json())
.then((updatedQuestion) => onAnswerChange(updatedQuestion));
};

return (
<li>
<h4>Question {id}</h4>
<h5>Prompt: {prompt}</h5>
<label>
Correct Answer:
<select defaultValue={correctIndex}>{options}</select>
<select defaultValue={correctIndex} onChange={changeAnswer}>
{options}
</select>
</label>
<button>Delete Question</button>
<button onClick={() => onDelete(id)}>Delete Question</button>
</li>
);
}
Expand Down
40 changes: 37 additions & 3 deletions src/components/QuestionList.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,44 @@
import React from "react";
import React, { useEffect } from "react";
import QuestionItem from "./QuestionItem";

function QuestionList({ questions, setQuestions }) {
useEffect(() => {
fetch("http://localhost:4000/questions")
.then((r) => r.json())
.then((data) => {
setQuestions(data);
});
}, []);

const handleDelete = (id) => {
fetch(`http://localhost:4000/questions/${id}`, {
method: "DELETE",
});
setQuestions(questions.filter((q) => q.id !== id));
};

const handleAnswerChange = (updatedQuestion) => {
setQuestions(
questions.map((q) => (q.id === updatedQuestion.id ? updatedQuestion : q))
);
};

function QuestionList() {
return (
<section>
<h1>Quiz Questions</h1>
<ul>{/* display QuestionItem components here after fetching */}</ul>
<ul>
{questions.map((q) => (
<QuestionItem
key={q.id}
id={q.id}
prompt={q.prompt}
answers={q.answers}
correctIndex={q.correctIndex}
onDelete={handleDelete}
onAnswerChange={handleAnswerChange}
/>
))}
</ul>
</section>
);
}
Expand Down