(props)
| 2 | import { useState, useEffect } from 'react'; |
| 3 | |
| 4 | const DiscussionForum = (props) => { |
| 5 | const VITE_SERVER_PORT = import.meta.env.VITE_SERVER_PORT || "https://bitbox-uxbo.onrender.com"; |
| 6 | |
| 7 | const [questions, setQuestions] = useState([]); |
| 8 | |
| 9 | // Fetch questions from the backend API |
| 10 | useEffect(() => { |
| 11 | const fetchQuestions = async () => { |
| 12 | try { |
| 13 | const response = await fetch(`${VITE_SERVER_PORT}/api/discussion/getQuestion`); |
| 14 | if (response.ok) { |
| 15 | const data = await response.json(); |
| 16 | setQuestions(data); |
| 17 | } else { |
| 18 | console.error('Failed to fetch questions'); |
| 19 | } |
| 20 | } catch (error) { |
| 21 | console.error('Error fetching questions:', error); |
| 22 | } |
| 23 | }; |
| 24 | |
| 25 | fetchQuestions(); |
| 26 | |
| 27 | // eslint-disable-next-line |
| 28 | }, []); |
| 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) => { |
| 60 | try { |
| 61 | const response = await fetch(`${VITE_SERVER_PORT}/api/discussion/${questionId}/answer`, { |
nothing calls this directly
no test coverage detected