Skip to content

Questions #17

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
44 changes: 37 additions & 7 deletions db.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,68 @@
{
"id": 1,
"prompt": "What special prop should always be included for lists of elements?",
"answers": ["id", "name", "key", "prop"],
"answers": [
"id",
"name",
"key",
"prop"
],
"correctIndex": 2
},
{
"id": 2,
"prompt": "A React component is a function that returns ______.",
"answers": ["HTML", "JSX", "props", "state"],
"answers": [
"HTML",
"JSX",
"props",
"state"
],
"correctIndex": 1
},
{
"id": 3,
"prompt": "Which event handler will run when a user is finished filling out a form?",
"answers": ["onSubmit", "onClick", "onChange", "onForm"],
"answers": [
"onSubmit",
"onClick",
"onChange",
"onForm"
],
"correctIndex": 0
},
{
"id": 4,
"prompt": "______ is a tool that transpiles JSX into valid JavaScript.",
"answers": ["React", "Babel", "Webpack", "ES6"],
"answers": [
"React",
"Babel",
"Webpack",
"ES6"
],
"correctIndex": 1
},
{
"id": 5,
"prompt": "What syntax makes it possible to unpack values from arrays, or properties from objects, into distinct variables?",
"answers": ["spread", "key-value", "coalescing", "destructuring"],
"answers": [
"spread",
"key-value",
"coalescing",
"destructuring"
],
"correctIndex": 3
},
{
"id": 6,
"prompt": "Returning different elements from a component depending on the state of your application is known as _____ rendering.",
"answers": ["voodoo", "conditional", "reactive", "controlled"],
"answers": [
"voodoo",
"conditional",
"reactive",
"controlled"
],
"correctIndex": 1
}
]
}
}
49 changes: 41 additions & 8 deletions src/components/App.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,50 @@
import React, { useState } from "react";
import AdminNavBar from "./AdminNavBar";
import QuestionForm from "./QuestionForm";
import QuestionList from "./QuestionList";
import React, {useState, useEffect} from "react"
import AdminNavBar from "./AdminNavBar"
import QuestionForm from "./QuestionForm"
import QuestionList from "./QuestionList"

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

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

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

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

function handleChangeAnswer(updatedQuestion) {
const updatedQuestions = questions.map((question) =>
question.id === updatedQuestion ? updatedQuestion : question
)
setQuestions(updatedQuestions)
}

return (
<main>
<AdminNavBar onChangePage={setPage} />
{page === "Form" ? <QuestionForm /> : <QuestionList />}
{page === "Form" ? (
<QuestionForm onAddQuestion={handleAddQuestion} />
) : (
<QuestionList
questions={questions}
onDeleteQuestion={handleDeleteQuestion}
onAnswer={handleChangeAnswer}
/>
)}
</main>
);
)
}

export default App;
export default App
33 changes: 25 additions & 8 deletions src/components/QuestionForm.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
import React, { useState } from "react";
import React, {useState} from "react"

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

function handleChange(event) {
setFormData({
...formData,
[event.target.name]: event.target.value,
});
})
}

function handleSubmit(event) {
event.preventDefault();
console.log(formData);
event.preventDefault()
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((r) => r.json())
.then((newQuestion) => onAddQuestion(newQuestion))
}

return (
Expand Down Expand Up @@ -87,7 +104,7 @@ function QuestionForm(props) {
<button type="submit">Add Question</button>
</form>
</section>
);
)
}

export default QuestionForm;
export default QuestionForm
41 changes: 33 additions & 8 deletions src/components/QuestionItem.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,50 @@
import React from "react";
import React from "react"

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

const options = answers.map((answer, index) => (
<option key={index} value={index}>
{answer}
</option>
));
))

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

function handleDeleteClick() {
fetch(`http://localhost:4000/questions/${question.id}`, {
method: "DELETE",
})
.then((r) => r.json())
.then(() => onDeleteQuestion(question))
}

return (
<li>
<h4>Question {id}</h4>
<h5>Prompt: {prompt}</h5>
<label>
Correct Answer:
<select defaultValue={correctIndex}>{options}</select>
<select onChange={handleAnswerSelect} defaultValue={correctIndex}>
{options}
</select>
</label>
<button>Delete Question</button>
<button onClick={handleDeleteClick}>Delete Question</button>
</li>
);
)
}

export default QuestionItem;
export default QuestionItem
20 changes: 15 additions & 5 deletions src/components/QuestionList.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import React from "react";
import React from "react"
import QuestionItem from "./QuestionItem"

function QuestionList() {
function QuestionList({questions, onDeleteQuestion, onAnswer}) {
return (
<section>
<h1>Quiz Questions</h1>
<ul>{/* display QuestionItem components here after fetching */}</ul>
<ul>
{questions.map((question) => (
<QuestionItem
key={question.id}
question={question}
onDeleteQuestion={onDeleteQuestion}
onAnswer={onAnswer}
/>
))}
</ul>
</section>
);
)
}

export default QuestionList;
export default QuestionList