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 Uploaded

Survey id is {questions[0].survey}.

) else return (
<> {questions.map((e)=>{return ()})}

) } function Remover({id,children,doRemove}) { const action = () => doRemove(id); return ( <> {children} ) } function QuestionPreview({prompt,choices}) { if(choices=='') { return (

{prompt}

) } else { const options = choices.split(','); return (

{prompt}

{options.map((o) => {return({o})})}
) } } function QuestionMaker({handleNew}) { let promptInput = useRef(); let choicesInput = useRef(); const action = (e) => { handleNew({question:promptInput.current.value,responses:choicesInput.current.value}); } return (

Question prompt:

Question choices:

) } function SurveyPoster({handlePost}) { let titleInput = useRef(); let promptInput = useRef(); const action = (e) => { handlePost({title:titleInput.current.value,prompt:promptInput.current.value}); } return (

Survey Title:

Survey Prompt:

) } export default App