diff --git a/client/src/component/DiscussionForum.jsx b/client/src/component/DiscussionForum.jsx index 3da0732..115bb92 100644 --- a/client/src/component/DiscussionForum.jsx +++ b/client/src/component/DiscussionForum.jsx @@ -3,59 +3,95 @@ import { useState, useEffect } from 'react'; const DiscussionForum = (props) => { const [questions, setQuestions] = useState([]); - // Fetch questions from localStorage + // Fetch questions from the backend API useEffect(() => { - const storedQuestions = localStorage.getItem('questions'); - if (storedQuestions) { - setQuestions(JSON.parse(storedQuestions)); - } - }, []); + const fetchQuestions = async () => { + try { + const response = await fetch('http://localhost:5000/api/discussion/getQuestion'); + if (response.ok) { + const data = await response.json(); + setQuestions(data); + } else { + console.error('Failed to fetch questions'); + } + } catch (error) { + console.error('Error fetching questions:', error); + } + }; - // Save questions to localStorage - const saveQuestions = (updatedQuestions) => { - setQuestions(updatedQuestions); - localStorage.setItem('questions', JSON.stringify(updatedQuestions)); - }; + fetchQuestions(); + }, []); // Helper function to save a new question - const addQuestion = (content) => { + const addQuestion = async (content) => { const newQuestion = { - id: Date.now().toString(), content, answered: false, answer: '', }; - const updatedQuestions = [...questions, newQuestion]; - saveQuestions(updatedQuestions); + try { + const response = await fetch('http://localhost:5000/api/discussion/postQuestion', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(newQuestion), + }); + + if (response.ok) { + const savedQuestion = await response.json(); + setQuestions((prevQuestions) => [...prevQuestions, savedQuestion]); + } else { + console.error('Failed to add question'); + } + } catch (error) { + console.error('Error adding question:', error); + } }; // Helper function to add an answer to a question - const addAnswer = (questionId, answerContent) => { - const updatedQuestions = questions.map((question) => - question.id === questionId - ? { ...question, answered: true, answer: answerContent } - : question - ); - saveQuestions(updatedQuestions); + const addAnswer = async (questionId, answerContent) => { + try { + const response = await fetch(`http://localhost:5000/api/discussion/${questionId}/answer`, { + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ answer: answerContent }), + }); + + if (response.ok) { + const updatedQuestion = await response.json(); + setQuestions((prevQuestions) => + prevQuestions.map((question) => + question._id === questionId ? { ...question, ...updatedQuestion } : question + ) + ); + } else { + console.error('Failed to add answer'); + } + } catch (error) { + console.error('Error adding answer:', error); + } }; // Function to render the Question Card const renderQuestionCard = (question) => { return (
{question.content}
{question.answered ? ( -{question.answer}