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
43 changes: 31 additions & 12 deletions db.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,59 @@
{
"questions": [
{
"id": 1,
"prompt": "What special prop should always be included for lists of elements?",
"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
}
]
}
}
3 changes: 2 additions & 1 deletion src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import QuestionList from "./QuestionList";

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

return (
<main>
<AdminNavBar onChangePage={setPage} />
{page === "Form" ? <QuestionForm /> : <QuestionList />}
{page === "Form" ? <QuestionForm /> : <QuestionList allQuestions={allQuestions} setAllQuestions={setAllQuestions}/>}
</main>
);
}
Expand Down
25 changes: 22 additions & 3 deletions src/components/QuestionForm.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState } from "react";
import React, { useEffect, useState } from "react";

function QuestionForm(props) {
function QuestionForm() {
const [formData, setFormData] = useState({
prompt: "",
answer1: "",
Expand All @@ -18,8 +18,27 @@ function QuestionForm(props) {
}

function handleSubmit(event) {
const {prompt, answer1, answer2, answer3, answer4, correctIndex} = formData
const newQuestionInfo = {
'prompt': prompt,
"answers": [answer1, answer2, answer3, answer4],
'correctIndex': correctIndex
}

event.preventDefault();
console.log(formData);
const options = {
method: 'POST',
headers:
{
"Content-Type": "application/json",
Accept: "application/json"
},
body: JSON.stringify(newQuestionInfo)
}

fetch('http://localhost:4000/questions', options)
.then(r => r.json())
.then(r => console.log(r));

Choose a reason for hiding this comment

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

Any reason for the console.log?

}

return (
Expand Down
16 changes: 15 additions & 1 deletion src/components/QuestionItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ function QuestionItem({ question }) {
</option>
));

const handleDelete = (e) => {
const options = {
method: 'DELETE',
headers:
{
"Content-Type": "application/json",
Accept: "application/json"
}
}
fetch(`http://localhost:4000/questions/${id}`, options)
.then(_resp => e.target.parentElement.remove())

Choose a reason for hiding this comment

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

Instead of, removing from the DOM with vanilla JS, you should be removing the object from state. If you were to create a new question, the "deleted" question would reappear I bet.

}


return (
<li>
<h4>Question {id}</h4>
Expand All @@ -17,7 +31,7 @@ function QuestionItem({ question }) {
Correct Answer:
<select defaultValue={correctIndex}>{options}</select>
</label>
<button>Delete Question</button>
<button onClick={handleDelete}>Delete Question</button>
</li>
);
}
Expand Down
20 changes: 17 additions & 3 deletions src/components/QuestionList.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import React from "react";
import React, {useEffect} from "react";
import QuestionItem from "./QuestionItem";

function QuestionList({allQuestions, setAllQuestions}) {

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

const displayQuestions = () => {
return(
allQuestions.map(question => <QuestionItem key={question.id} question={question} />)
)
}

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