Skip to content

Commit

Permalink
Refactor trend retrieval in SearchScreen; update API endpoint and dat…
Browse files Browse the repository at this point in the history
…a structure for trends
  • Loading branch information
NateIsern committed Dec 12, 2024
1 parent caf3b41 commit 4d3069c
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions app/explore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ const searchResults = [
// Add more search results
];

const trends = sampleTrends.map((trend, index) => ({
id: (index + 1).toString(),
topic: trend.hashtag,
countTotal: trend.count.toString(),
}));

type SearchResult = {
id: string;
user: {
Expand Down Expand Up @@ -159,10 +153,17 @@ export default function SearchScreen() {

const retrieveTrendsFromAPI = async () => {
try {
const data = await fetchData("trends");
const data = await fetchData("hashtags");
console.log("Fetched trends:", data);
if (data) {
await storeData("trends", data);
setTrends(data);
const trends = data.map((trend: any) => ({
id: trend.id,
topic: trend.hashtag,
countTotal: trend.score.toString(),
}));
console.log("Trends:", trends);
await storeData("trends", trends);
setTrends(trends);
} else {
console.warn("No trends data returned from API");
}
Expand All @@ -173,12 +174,7 @@ export default function SearchScreen() {

useEffect(() => {
const fetchTrends = async () => {
const storedTrends = await getData("trends");
if (storedTrends) {
setTrends(storedTrends);
} else {
retrieveTrendsFromAPI();
}
await retrieveTrendsFromAPI();
};

fetchTrends();
Expand All @@ -188,6 +184,7 @@ export default function SearchScreen() {
const fetchAndSetPosts = async () => {
const fetchedPosts = await fetchPosts();
setPosts(fetchedPosts);
await storeData("posts", fetchedPosts);
};

fetchAndSetPosts();
Expand Down

0 comments on commit 4d3069c

Please sign in to comment.