diff --git a/backend/src/main/java/com/rit/portal/controller/CommunityQuestionController.java b/backend/src/main/java/com/rit/portal/controller/CommunityQuestionController.java index 3167ef1..dbfb839 100644 --- a/backend/src/main/java/com/rit/portal/controller/CommunityQuestionController.java +++ b/backend/src/main/java/com/rit/portal/controller/CommunityQuestionController.java @@ -28,7 +28,7 @@ public class CommunityQuestionController { @GetMapping public List getAllQuestions() { - return questionRepository.findAll(); + return questionRepository.findByIsAnsweredTrue(); } @PostMapping diff --git a/backend/src/main/java/com/rit/portal/repository/CommunityQuestionRepository.java b/backend/src/main/java/com/rit/portal/repository/CommunityQuestionRepository.java index 72dd96f..2635288 100644 --- a/backend/src/main/java/com/rit/portal/repository/CommunityQuestionRepository.java +++ b/backend/src/main/java/com/rit/portal/repository/CommunityQuestionRepository.java @@ -3,7 +3,9 @@ package com.rit.portal.repository; import com.rit.portal.entity.CommunityQuestion; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.List; @Repository public interface CommunityQuestionRepository extends JpaRepository { + List findByIsAnsweredTrue(); } diff --git a/src/pages/Community/Community.tsx b/src/pages/Community/Community.tsx index 11a815f..24cd6cc 100644 --- a/src/pages/Community/Community.tsx +++ b/src/pages/Community/Community.tsx @@ -76,6 +76,8 @@ export default function Community() { const [currentPage, setCurrentPage] = useState(0); const [questions, setQuestions] = useState([]); const [loading, setLoading] = useState(false); + const [showSuccessBanner, setShowSuccessBanner] = useState(false); + const [postError, setPostError] = useState(null); const toggleAnswers = (id: string) => { setExpandedQuestionIds((prev) => { @@ -229,48 +231,33 @@ export default function Community() { ); const finalTags = detectedTags.length > 0 ? detectedTags : ['fresher', 'general']; - const newQuestion = { - title: questionText.split('\n')[0].substring(0, 100) || "Q&A Question", - body: questionText, - author: displayAuthor, - tags: finalTags, - upvotes: 0, - isAnswered: false, - createdAt: new Date().toISOString(), - answers: [] - }; + setPostError(null); + setShowSuccessBanner(false); try { const response = await fetch(getBackendUrl('/api/questions'), { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ - title: newQuestion.title, - body: newQuestion.body, - author: newQuestion.author, - tags: newQuestion.tags + title: questionText.split('\n')[0].substring(0, 100) || "Q&A Question", + body: questionText, + author: displayAuthor, + tags: finalTags }) }); if (response.ok) { - const saved = await response.json(); - setQuestions(prev => { - // Prepend saved question and filter out duplicate placeholders - const filtered = prev.filter(q => q.id.toString() !== saved.id.toString()); - return [saved, ...filtered]; - }); + setShowSuccessBanner(true); setQuestionText(''); setAuthorName(''); + // Hide success banner after 6 seconds + setTimeout(() => setShowSuccessBanner(false), 6000); } else { - setQuestions(prev => [{ ...newQuestion, id: String(Date.now()) }, ...prev]); - setQuestionText(''); - setAuthorName(''); + setPostError("Failed to submit your question. Please try again later."); } } catch (error) { console.error("Error saving question:", error); - setQuestions(prev => [{ ...newQuestion, id: String(Date.now()) }, ...prev]); - setQuestionText(''); - setAuthorName(''); + setPostError("Unable to connect to the server. Please check your connection."); } }; @@ -293,6 +280,29 @@ export default function Community() {
{/* Ask Question Box */}

Ask a Question

+ + {showSuccessBanner && ( + + 🎉 Question submitted! Your question has been forwarded to senior volunteers. Once they review and answer it, it will be published here on the community Q&A board. + + )} + + {postError && ( + + ⚠️ {postError} + + )} +