diff --git a/frontend/src/api/questionApi.js b/frontend/src/api/questionApi.js index b99b848..30fbcff 100644 --- a/frontend/src/api/questionApi.js +++ b/frontend/src/api/questionApi.js @@ -181,6 +181,41 @@ export const getQuestionUpvotes = async (id) => { * Searches in questionText, company, and topic fields */ + +// Mock data for demo purposes +const MOCK_QUESTIONS = [ + { + _id: '1', + questionText: 'What is React?', + company: 'Facebook', + topic: 'Frontend', + role: 'Developer', + difficulty: 'Easy', + upvotes: 10, + createdAt: '2025-10-01T12:00:00Z', + }, + { + _id: '2', + questionText: 'Explain useEffect hook.', + company: 'Google', + topic: 'Frontend', + role: 'Engineer', + difficulty: 'Medium', + upvotes: 7, + createdAt: '2025-09-28T12:00:00Z', + }, + { + _id: '3', + questionText: 'How does Node.js handle async?', + company: 'Amazon', + topic: 'Backend', + role: 'Backend Developer', + difficulty: 'Hard', + upvotes: 15, + createdAt: '2025-09-20T12:00:00Z', + }, +]; + export const searchQuestions = async (keyword) => { try { const response = await api.get(`/questions/search?q=${encodeURIComponent(keyword)}`); diff --git a/frontend/src/pages/Questions.jsx b/frontend/src/pages/Questions.jsx index 48250f3..2685d3b 100644 --- a/frontend/src/pages/Questions.jsx +++ b/frontend/src/pages/Questions.jsx @@ -107,26 +107,26 @@ const Questions = () => { if (filters.difficulty) queryParams.difficulty = filters.difficulty; if (filters.sort) queryParams.sort = filters.sort; - const questionData = await getAllQuestions(queryParams); + const questionData = await getAllQuestions(queryParams); - setQuestions(questionData.questions || questionData || []) - } - catch (err) { - setError('Failed to load questions. Please try again later.'); - console.error('Error fetching questions:', err); - } - finally { - setLoading(false); - } + setQuestions(questionData.questions || questionData || []) + } + catch (err) { + setError('Failed to load questions. Please try again later.'); + console.error('Error fetching questions:', err); + } + finally { + setLoading(false); } - - fetchQuestions(); }, [filters]); // Fetch questions on mount and when filters change useEffect(() => { - fetchQuestions(); - }, [fetchQuestions]); + // Only fetch if not searching + if (!searchQuery) { + fetchQuestions(); + } + }, [fetchQuestions, searchQuery]); //Fetch categories function for filters const fetchCategories = async () => { @@ -268,7 +268,6 @@ const Questions = () => { - {/* Error Message */} {error && (
{error}