-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from Datawheel/nextjs-test
refactors homepage for performance
- Loading branch information
Showing
2 changed files
with
111 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
"use client"; | ||
|
||
import {useState} from "react"; | ||
import HomeGrid from "/components/home/Grid"; | ||
import Spinner from "/components/Spinner"; | ||
import Select from "/components/common/Select"; | ||
|
||
const LangSelector = ({handleLanguageChange, trendingLangEdition}) => ( | ||
<Select | ||
label="" | ||
className="home-select" | ||
fontSize="sm" | ||
onChange={handleLanguageChange} | ||
value={trendingLangEdition} | ||
> | ||
<option value="ar">Arabic</option> | ||
<option value="zh">Chinese</option> | ||
<option value="nl">Dutch</option> | ||
<option value="en">English</option> | ||
<option value="fr">French</option> | ||
<option value="de">German</option> | ||
<option value="it">Italian</option> | ||
<option value="ja">Japanese</option> | ||
<option value="pt">Portuguese</option> | ||
<option value="ru">Russian</option> | ||
<option value="es">Spanish</option> | ||
</Select> | ||
); | ||
|
||
export default function TrendingGrid({initialTrendingAll, defaultLang}) { | ||
const [trendingAll, setTrendingAll] = useState(initialTrendingAll); | ||
const [trendingLangEdition, setTrendingLangEdition] = useState(defaultLang); | ||
const [loading, setLoading] = useState(false); | ||
|
||
const fetchTrendingData = async lang => { | ||
setLoading(true); | ||
try { | ||
const res = await fetch(`/api/wikiTrends?lang=${lang}&limit=12`, { | ||
cache: "force-cache", // Use browser cache if available | ||
}); | ||
const data = await res.json(); | ||
setTrendingAll(data); | ||
} catch (error) { | ||
console.error("Error fetching trending data:", error); | ||
} | ||
setLoading(false); | ||
}; | ||
|
||
// Handle language change dynamically | ||
const handleLanguageChange = e => { | ||
const selectedLang = e.target.value; | ||
setTrendingLangEdition(selectedLang); | ||
fetchTrendingData(selectedLang); | ||
}; | ||
|
||
return ( | ||
<div className="profile-grid"> | ||
<div className="grid-title-container"> | ||
<h3 className="grid-title">Trending Profiles Today</h3> | ||
<p className="grid-subtitle"> | ||
<span className="grid-select-label"> | ||
Top profiles by pageviews for the{" "} | ||
</span> | ||
<LangSelector | ||
handleLanguageChange={handleLanguageChange} | ||
trendingLangEdition={trendingLangEdition} | ||
/> | ||
<span className="grid-select-label"> wikipedia edition</span> | ||
</p> | ||
</div> | ||
{!loading ? ( | ||
<HomeGrid | ||
bios={trendingAll.sort((a, b) => a.rank - b.rank).slice(0, 16)} | ||
/> | ||
) : ( | ||
<div className="loading-trends"> | ||
<Spinner /> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
} |