REST Countries API with color theme switcher
Design comparison
Solution retrospective
I am very much proud of how I integrated shadcn ui select component which is using Radix UI Select Primitive, Although I am thinking of creating my own select component using Radix UI next time.
What challenges did you encounter, and how did you overcome them?The challenge that I first encounter was how to handle json data and use typescript to only fetch the data that will be displaying on the screen and filter out other unnecessary properties from the data. Here is how I tackle this problem.
import data from "./data.json"; const countries: CountryList[] = data.map((country) => { return { name: country.name, population: country.population, region: country.region as Region, capital: country.capital, flags: country.flags, }; }); ```ts **As for country detail page, here is how get the data:** ```ts export const getCountry = (name: string): CountryDetail => { const country = data.find((c) => c.name.toLowerCase() == name.toLowerCase()); if (!country) throw new Error(`Country with name ${name} does not exists`); let borderCountries = country.borders?.map((border) => getCountryByAlpha3Code(border) ); return { name: country.name, nativeName: country.nativeName, topLevelDomain: country.topLevelDomain, population: country.population, currencies: country.currencies, region: country.region, subregion: country.subregion, languages: country.languages, capital: country.capital, borderCountries: borderCountries, flags: country.flags, }; }; ```ts
Community feedback
- @ZahirHaniche-devPosted 3 months ago
Fantastic project, excellently executed and completed. Kudos to you, my friend.
I have a crucial observation regarding the data reloading with each action on the site. I recommend you check out my project to see that the only loading I perform is fetching the list when we first access the application. In your case, you make API calls every time you trigger or use a feature, such as the filter or the detail page.
In my approach, I use a reducer with a getData function that I call once. Then, for filtering and other operations, I manipulate the array that I retrieve as a state.
Marked as helpful1
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord