diff --git a/db.json b/db.json index 73ed95d1a..e14bfe31f 100644 --- a/db.json +++ b/db.json @@ -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 } ] -} +} \ No newline at end of file diff --git a/src/components/App.js b/src/components/App.js index 4d33c1320..21bdc7944 100644 --- a/src/components/App.js +++ b/src/components/App.js @@ -5,11 +5,12 @@ import QuestionList from "./QuestionList"; function App() { const [page, setPage] = useState("List"); + const [allQuestions, setAllQuestions] = useState([]) return (
- {page === "Form" ? : } + {page === "Form" ? : }
); } diff --git a/src/components/QuestionForm.js b/src/components/QuestionForm.js index f02af04e7..e48e7dd14 100644 --- a/src/components/QuestionForm.js +++ b/src/components/QuestionForm.js @@ -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: "", @@ -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)); } return ( diff --git a/src/components/QuestionItem.js b/src/components/QuestionItem.js index ab5e8e820..78676bec4 100644 --- a/src/components/QuestionItem.js +++ b/src/components/QuestionItem.js @@ -9,6 +9,20 @@ function QuestionItem({ question }) { )); + 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()) + } + + return (
  • Question {id}

    @@ -17,7 +31,7 @@ function QuestionItem({ question }) { Correct Answer: - +
  • ); } diff --git a/src/components/QuestionList.js b/src/components/QuestionList.js index 4b3701bb2..140dd7cd6 100644 --- a/src/components/QuestionList.js +++ b/src/components/QuestionList.js @@ -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 => ) + ) + } -function QuestionList() { return (

    Quiz Questions

    - +
    ); }