Open Septimus4 opened 2 weeks ago
Okay @Septimus4 So which endpoint do I use for the mocking? If I'm using this const mockTrendingResponse = { data: { topics: ['data science', 'biology', 'ai', 'longevity', 'trading'] } };
Will I still need the axios?
@Emmanuel-Darko Yes, even with mock data, Axios is still helpful here to maintain the structure for future backend integration. By setting up Axios now, we’ll be able to swap in the actual API response easily without changing the logic.
Here’s how we can structure it:
Here’s how the submitQuery
function would look:
import axios from 'axios';
const submitQuery = async () => {
loading.value = true;
try {
const response = await axios.get('/mock-endpoint'); // Placeholder for actual endpoint
const topics = response?.data?.topics || mockTrendingResponse.data.topics;
searchOptions.value = topics.filter((topic) =>
topic.toLowerCase().includes(query.value?.toLowerCase()?.trim() || "")
);
error.value = searchOptions.value.length ? '' : 'No relevant topics are discussed on this server';
} catch (e) {
error.value = 'Failed to fetch topics from the server.';
} finally {
loading.value = false;
}
};
In this setup:
/mock-endpoint
is a placeholder for the actual endpoint we’ll use later.But of course if you prefer any other http library or dependency to do that, you are free to use it instead, what's matters is the ability swap in the actual API response easily without changing the logic
Objective:
Set up placeholders for API calls within the
useTopics
function to prepare for future backend integration. For now, use mock data for trending topics and query filtering while keeping the structure ready for actual API requests.Tasks
Set Up Axios
axios
to standardize API call structure for future integration.npm install axios
oryarn add axios
Configure Mock Data & API Call Structure
submitQuery
function to handle API calls.Replace Dummy Trending Topics with Mock Data in
submitQuery
searchOptions
with the filtered results based onquery
.Implement Error and Loading States
Example of
submitQuery
Function with Mock DataAcceptance Criteria
submitQuery
is structured to handle an API call with mock data in place.query
, and error/loading states are displayed correctly.This will allow the frontend to function with mock data, while the structure is ready for actual API requests once the backend is live.