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
36 changes: 34 additions & 2 deletions src/components/App.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,49 @@
import React, { useState } from "react";
import React, { useEffect, useState } from "react";
import AdminNavBar from "./AdminNavBar";
import QuestionForm from "./QuestionForm";
import QuestionList from "./QuestionList";

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

useEffect(() => {
fetch("http://localhost:4000/questions")
.then((response) => response.json())
.then((questionsData) => setQuestions(questionsData));
}, []);

function handleAddQuestion(newQuestion) {
setQuestions([...questions, newQuestion]);
}

function handleDeleteQuestion(id) {
const updatedQuestions = questions.filter((questions) => questions.id !== id)
setQuestions(updatedQuestions)
}

function handleAnswerChange(updatedQuestion) {
const updateQuestions = questions.map((question) => {
return question.id === updatedQuestion.id ? updatedQuestion : question
})
setQuestions(updateQuestions)
}

return (
<main>
<AdminNavBar onChangePage={setPage} />
{page === "Form" ? <QuestionForm /> : <QuestionList />}
{page === "Form" ? (
<QuestionForm onAddQuestions={handleAddQuestion} />
) : (
<QuestionList
questions={questions}
onDeleteQuestion={handleDeleteQuestion}
onUpdateAnswer={handleAnswerChange}
/>
)}
Comment on lines +35 to +43

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

</main>
);
}

export default App;

22 changes: 20 additions & 2 deletions src/components/QuestionForm.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState } from "react";

function QuestionForm(props) {
function QuestionForm({ onAddQuestions }) {
const [formData, setFormData] = useState({
prompt: "",
answer1: "",
Expand All @@ -10,6 +10,7 @@ function QuestionForm(props) {
correctIndex: 0,
});


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

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

function handleSubmit(event) {
event.preventDefault();
console.log(formData);
fetch("http://localhost:4000/questions", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
prompt: formData.prompt,
answers: [
formData.answer1,
formData.answer2,
formData.answer3,
formData.answer4
],
correctIndex: formData.correctIndex
})
})
.then((response) => response.json())
.then((newQuestionData) => onAddQuestions(newQuestionData))
}

return (
Expand Down
27 changes: 24 additions & 3 deletions src/components/QuestionItem.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";

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

const options = answers.map((answer, index) => (
Expand All @@ -9,15 +9,36 @@ function QuestionItem({ question }) {
</option>
));

function handleDelete() {
fetch(`http://localhost:4000/questions/${id}`, {
method: "DELETE",
})
.then(() => onDeleteQuestion(id))
.catch((error) => console.error("Error:", error));
}

function handleAnswerChange(event) {
const newCorrectIndex = (event.target.value)
fetch(`http://localhost:4000/questions/${id}`, {
method: "PATCH",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({correctIndex: newCorrectIndex})
})
.then((response) => response.json())
.then((newAnswerData) => onUpdateAnswer(newAnswerData))
}

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

function QuestionList({ questions, onDeleteQuestion, onUpdateAnswer }) {
const questionItems = questions.map((question) => {
return (
<QuestionItem
key={question.id}
question={question}
onDeleteQuestion={onDeleteQuestion}
onUpdateAnswer={onUpdateAnswer}
/>
)
})

function QuestionList() {
return (
<section>
<h1>Quiz Questions</h1>
<ul>{/* display QuestionItem components here after fetching */}</ul>
<ul>{questionItems}</ul>
</section>
);
}
Expand Down