From d028e86bdd5900639af567a8a73aa87b9c200fe0 Mon Sep 17 00:00:00 2001 From: Shanmuga Krishnan S M Date: Wed, 19 Aug 2026 09:08:20 +0530 Subject: [PATCH] Restore fetchQuestions and handleDeleteQuestion handlers in AdminDashboard.tsx --- src/pages/Admin/AdminDashboard.tsx | 51 ++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/pages/Admin/AdminDashboard.tsx b/src/pages/Admin/AdminDashboard.tsx index b84e815..212b9ac 100644 --- a/src/pages/Admin/AdminDashboard.tsx +++ b/src/pages/Admin/AdminDashboard.tsx @@ -580,6 +580,57 @@ export default function AdminDashboard() { + // ───────────────────────────────────────────────────────────── + // COMMUNITY QUESTIONS + // ───────────────────────────────────────────────────────────── + const fetchQuestions = () => { + setLoadingQuestions(true); + const saved = localStorage.getItem(QUESTIONS_STORAGE_KEY); + if (saved) { + try { + const parsed = JSON.parse(saved); + if (Array.isArray(parsed) && parsed.length > 0) { + setQuestions(parsed); + setLoadingQuestions(false); + return; + } + } catch {} + } + + fetch(getBackendUrl('/api/admin/questions')) + .then((res) => { + if (!res.ok) throw new Error(); + return res.json(); + }) + .then((data) => { + if (Array.isArray(data) && data.length > 0) { + setQuestions(data); + localStorage.setItem(QUESTIONS_STORAGE_KEY, JSON.stringify(data)); + } + }) + .catch(() => { + if (!saved) { + const initialQuestions: QuestionItem[] = [ + { id: 1, title: "When is the fresher's orientation?", content: "I couldn't find the exact date for the CSE orientation.", authorName: "Karthik", authorEmail: "karthik.2024@ritchennai.edu.in", createdAt: new Date().toISOString(), status: "PENDING", tags: ["orientation"], votes: 0 }, + { id: 2, title: "Are laptops mandatory in first year?", content: "Just wondering if we need to bring laptops to college every day.", authorName: "Sneha", authorEmail: "sneha.2024@ritchennai.edu.in", createdAt: new Date(Date.now() - 86400000).toISOString(), status: "APPROVED", tags: ["academics"], votes: 5 } + ]; + setQuestions(initialQuestions); + localStorage.setItem(QUESTIONS_STORAGE_KEY, JSON.stringify(initialQuestions)); + } + }) + .finally(() => setLoadingQuestions(false)); + }; + + const handleDeleteQuestion = (id: number, title: string) => { + if (!confirm(`Delete student question "${title}"?`)) return; + const updated = questions.filter((q) => q.id !== id); + setQuestions(updated); + localStorage.setItem(QUESTIONS_STORAGE_KEY, JSON.stringify(updated)); + showToast('success', `Question "${title}" deleted.`); + + fetch(getBackendUrl(`/api/admin/questions/${id}`), { method: 'DELETE' }).catch(() => {}); + }; + // ───────────────────────────────────────────────────────────── // FACULTY MANAGEMENT HANDLERS // ─────────────────────────────────────────────────────────────