import { useState, useEffect } from 'react' import { Radio, RadioGroup } from '@sajari/react-components'; import './App.css' function App() { useEffect(() => {getSurvey()},[]); const [questions,setQuestions] = useState([]); const [survey,setSurvey] = useState(); function getSurvey() { const url = window.location.href; if(url.indexOf('#') > 0) { let parts = url.split('#'); let id = parts[1]; fetch("http://localhost:8085/surveys?id="+id).then(response=>response.json()).then(response=>{setSurvey(response);getQuestions(id);}); } } function getQuestions(id) { fetch("http://localhost:8085/questions?survey="+id).then(response=>response.json()).then(response=>setQuestions(response)); } function uploadAnswers() { questions.map(q => uploadResponse(q)); alert("Your answers have been recorded.") } function uploadResponse(q) { let response = {survey:q.survey,question:q.idquestion,response:q.response}; fetch('http://localhost:8085/responses', { method: 'POST', body: JSON.stringify(response), headers: { 'Content-type': 'application/json; charset=UTF-8' } }); } if(survey) return (

{survey.title}

{survey.prompt}

{questions.map((q,i) => {return(); })}
) else return (

No survey

No survey to display

) } function Question({question}) { let handleChange = (e) => {question.response = e.target.value;}; if(question.responses=='') { return (

{question.question}

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

{question.question}

{options.map((o) => {return({o})})}
) } } export default App