Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted over 1 year ago

REST Countries API with color theme switcher

next, react, tailwind-css, shadcn
Syed Abdul Haseeb•150
@abdul-haseeb123
A solution to the REST Countries API with color theme switcher challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

Solution retrospective


What are you most proud of, and what would you do differently next time?

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

Code
Loading...

Please log in to post a comment

Log in with GitHub

Community feedback

No feedback yet. Be the first to give feedback on Syed Abdul Haseeb's solution.

Join 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

Stay up to datewith new challenges, featured solutions, selected articles, and our latest news

Frontend Mentor

  • Unlock Pro
  • Contact us
  • FAQs
  • Become a partner
  • Use cases

Explore

  • Learning paths
  • Challenges
  • Solutions
  • Articles

Community

  • Discord
  • Guidelines

For companies

  • Hire developers
  • Train developers
© Frontend Mentor 2019 - 2025
  • Terms
  • Cookie Policy
  • Privacy Policy
  • License