import { useState, useRef } from 'react' import { Radio, RadioGroup } from '@sajari/react-components'; import './App.css' function App() { const [questions,setQuestions] = useState([]); const [index,setIndex] = useState(1); const incrementIndex = () => {setIndex(index+1);}; const [uploadDone,setUploadDone] = useState(false); let uploaded = 0; const incrementUploaded = () => {uploaded++;if(uploaded == questions.length) setUploadDone(true); } const newQuestionAction = (newQ) => { newQ.id = index;setQuestions([...questions,newQ]);incrementIndex();} const removeQuestion = (n) => {setQuestions(questions.filter((q) => {return q.id != n}))} // Helper functions to assist with the process of uploading the survey to the server. function uploadSurvey(toPost) { fetch('http://localhost:8085/surveys', { method: 'POST', body: JSON.stringify(toPost), headers: { 'Content-type': 'application/json; charset=UTF-8' } }).then(response => response.json()).then(response => uploadQuestions(response)); } function uploadQuestions(id) { questions.map(q=>{q.survey = id;uploadQuestion(q);}); } function uploadQuestion(q) { fetch('http://localhost:8085/questions', { method: 'POST', body: JSON.stringify(q), headers: { 'Content-type': 'application/json; charset=UTF-8' } }).then(response=>incrementUploaded()); } if(uploadDone) return (
Survey id is {questions[0].survey}.
{prompt}
{prompt}
Question prompt:
Question choices:
Survey Title:
Survey Prompt: