diff --git a/src/components/App.js b/src/components/App.js
index 4d33c1320..95f471487 100644
--- a/src/components/App.js
+++ b/src/components/App.js
@@ -1,17 +1,39 @@
-import React, { useState } from "react";
-import AdminNavBar from "./AdminNavBar";
-import QuestionForm from "./QuestionForm";
-import QuestionList from "./QuestionList";
+import React, { useEffect, useState } from "react"
+
+import { fetchQuestions } from "../utils/fetchers"
+
+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([])
+
+ const handleAddQuestion = (newQuestion) => {
+ setQuestions([...questions, newQuestion])
+ }
+
+ const handleDelete = (deletedQuestion) => {
+ const updatedQuestions = questions.filter(
+ (question) => question !== deletedQuestion
+ )
+
+ setQuestions(updatedQuestions)
+ }
+
+ useEffect(() => fetchQuestions(setQuestions), [])
return (
- {page === "Form" ? : }
+ {page === "Form" ? (
+
+ ) : (
+
+ )}
- );
+ )
}
-export default App;
+export default App
diff --git a/src/components/QuestionForm.js b/src/components/QuestionForm.js
index f02af04e7..e78d9dbbc 100644
--- a/src/components/QuestionForm.js
+++ b/src/components/QuestionForm.js
@@ -1,25 +1,41 @@
-import React, { useState } from "react";
+import React, { useState } from "react"
-function QuestionForm(props) {
- const [formData, setFormData] = useState({
- prompt: "",
- answer1: "",
- answer2: "",
- answer3: "",
- answer4: "",
- correctIndex: 0,
- });
+import { createQuestion } from "../utils/fetchers"
+
+const initialState = {
+ prompt: "",
+ answer1: "",
+ answer2: "",
+ answer3: "",
+ answer4: "",
+ correctIndex: 0,
+}
+
+function QuestionForm({ onAddQuestion }) {
+ const [formData, setFormData] = useState(initialState)
+ const { prompt, answer1, answer2, answer3, answer4, correctIndex } = formData
+
+ const resetForm = () => setFormData(initialState)
function handleChange(event) {
setFormData({
...formData,
[event.target.name]: event.target.value,
- });
+ })
}
+ const sanitizeInput = () => ({
+ prompt: prompt,
+ answers: [answer1, answer2, answer3, answer4],
+ correctIndex: correctIndex,
+ })
+
function handleSubmit(event) {
- event.preventDefault();
- console.log(formData);
+ event.preventDefault()
+
+ const questionData = sanitizeInput()
+
+ createQuestion(questionData).then(onAddQuestion).then(resetForm)
}
return (
@@ -87,7 +103,7 @@ function QuestionForm(props) {
- );
+ )
}
-export default QuestionForm;
+export default QuestionForm
diff --git a/src/components/QuestionItem.js b/src/components/QuestionItem.js
index ab5e8e820..a2588883a 100644
--- a/src/components/QuestionItem.js
+++ b/src/components/QuestionItem.js
@@ -1,13 +1,24 @@
-import React from "react";
+import React from "react"
+import { deleteQuestion, updateQuestion } from "../utils/fetchers"
-function QuestionItem({ question }) {
- const { id, prompt, answers, correctIndex } = question;
+function QuestionItem({ question, onDelete }) {
+ const { id, prompt, answers, correctIndex } = question
const options = answers.map((answer, index) => (
- ));
+ ))
+
+ const handleChange = (event) => {
+ const correctIndex = event.target.value
+
+ updateQuestion(question.id, { correctIndex })
+ }
+
+ const handleDelete = () => {
+ deleteQuestion(question.id).then(() => onDelete(question))
+ }
return (
@@ -15,11 +26,13 @@ function QuestionItem({ question }) {
Prompt: {prompt}
-
+
- );
+ )
}
-export default QuestionItem;
+export default QuestionItem
diff --git a/src/components/QuestionList.js b/src/components/QuestionList.js
index 4b3701bb2..9cb6a7597 100644
--- a/src/components/QuestionList.js
+++ b/src/components/QuestionList.js
@@ -1,12 +1,21 @@
-import React from "react";
+import React from "react"
+import QuestionItem from "./QuestionItem"
-function QuestionList() {
+function QuestionList({ handleDelete, questions }) {
return (
Quiz Questions
- {/* display QuestionItem components here after fetching */}
+
+ {questions.map((question) => (
+
+ ))}
+
- );
+ )
}
-export default QuestionList;
+export default QuestionList
diff --git a/src/utils/fetchers.js b/src/utils/fetchers.js
new file mode 100644
index 000000000..abc6b0a2e
--- /dev/null
+++ b/src/utils/fetchers.js
@@ -0,0 +1,31 @@
+const questionsUrl = "http://localhost:4000/questions/"
+
+export const fetchQuestions = (setQuestions) => {
+ fetch(questionsUrl)
+ .then((response) => response.json())
+ .then(setQuestions)
+}
+
+export const createQuestion = (questionData) => {
+ return fetch(questionsUrl, {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify(questionData),
+ }).then((response) => response.json())
+}
+
+export const deleteQuestion = (questionId) => {
+ return fetch(questionsUrl + questionId, { method: "Delete" })
+}
+
+export const updateQuestion = (questionId, questionData) => {
+ return fetch(questionsUrl + questionId, {
+ method: "PATCH",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify(questionData),
+ }).then((response) => response.json())
+}