| 29 | |
| 30 | // Helper function to save a new question |
| 31 | const addQuestion = async (content) => { |
| 32 | const newQuestion = { |
| 33 | content, |
| 34 | answered: false, |
| 35 | answer: '', |
| 36 | }; |
| 37 | |
| 38 | try { |
| 39 | const response = await fetch(`${VITE_SERVER_PORT}/api/discussion/postQuestion`, { |
| 40 | method: 'POST', |
| 41 | headers: { |
| 42 | 'Content-Type': 'application/json', |
| 43 | }, |
| 44 | body: JSON.stringify(newQuestion), |
| 45 | }); |
| 46 | |
| 47 | if (response.ok) { |
| 48 | const savedQuestion = await response.json(); |
| 49 | setQuestions((prevQuestions) => [...prevQuestions, savedQuestion]); |
| 50 | } else { |
| 51 | console.error('Failed to add question'); |
| 52 | } |
| 53 | } catch (error) { |
| 54 | console.error('Error adding question:', error); |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | // Helper function to add an answer to a question |
| 59 | const addAnswer = async (questionId, answerContent) => { |