import React, { useState, useEffect, FormEvent } from 'react'; import { Button, Modal, Spin } from 'antd'; interface QuizModule { moduleId: string; moduleName: string; } const App: React.FC = () => { const [question, setQuestion] = useState(''); const [option1, setOption1] = useState(''); const [option2, setOption2 ] = useState(''); const [option3, setOption3 ] = useState(''); const [option4, setOption4 ] = useState(''); const [moduleId, setModuleId] = useState(''); const [correctAnswer, setCorrectAnswer] = useState(''); let moduleList = [ { moduleId : "1", moduleName : "Module Name - 1" }, { moduleId : "2", moduleName : "Module Name - 2" }, { moduleId : "3", moduleName : "Module Name - 3" }, { moduleId : "4", moduleName : "Module Name - 4" }, { moduleId : "5", moduleName : "Module Name - 5" } ] // const [moduleList, setModuleList] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [open, setOpen] = useState(false); const [submitLoading, setSubmitLoading] = useState(false); useEffect(() => { fetch(`http://localhost:5174/api/quiz-module-list`) .then((res) => { if (!res.ok) { throw new Error('Network response was not ok'); } return res.json(); }) .then((data) => { console.log(data); // setModuleList(data); setLoading(false); }) .catch((error) => { console.error('An error occurred', error); setError(error); setLoading(false); }); }, []); const handleModuleSubmit = async (e: FormEvent) => { e.preventDefault(); const data = { question, option1, option2, option3, option4, correctAnswer, moduleId }; setSubmitLoading(true); try { const response = await fetch(`http://localhost:5174/api/create-question`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(data), }); if (!response.ok) { throw new Error('Network Response not ok'); } const result = await response.json(); // console.log('Success', result); setOpen(false); // Close the modal on successful submission // Optionally, refresh the quiz list here } catch (error) { // console.error('Error', error); } finally { setSubmitLoading(false); } }; const showLoading = () => { setOpen(true); }; if (loading) { return ; } if (error) { return
Error: {error.message}
; } return ( <> setOpen(false)}> {submitLoading ? ( ) : (
setQuestion(e.target.value)} type="text" name='question' id='question'/>
setOption1(e.target.value)} type="text" name='option1' id='option1'/>
setOption2(e.target.value)} type="text" name='option2' id='option2'/>
setOption3(e.target.value)} type="text" name='option3' id='option3'/>
setOption4(e.target.value)} type="text" name='option4' id='option4'/>

)}
); }; export default App; // quizList