Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted about 3 years ago

REST countries api redo second time.

Duyen Nguyen•950
@Duyen-codes
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


I got things to work but my approach of fetching everytime on filter and search is the worst thing to do as I believe. I failed to fetch all countries once and store in a variable so I could use in the rest of my codes but it was always undefined. Please share your solution so I can improve my codes. I am still beginner with JS but wanted to challenge myself so I gave this challenge a try. Please be as critical as possible so I can improve faster. Any help is greatly appreciated!

Code
Select a file

Please log in to post a comment

Log in with GitHub

Community feedback

  • Prabhash Ranjan•2,540
    @besttlookk
    Posted about 3 years ago

    I have made two version for this challenge, first with vanilla js and then using Reactjs.(I will add link to both at the bottom). With react it is quite easy to do stuff especially if creating markup dynamically. I dont know if you know react yet.

    When i made it with vanilla js i forgot to implememt both search and filter together which i added in react version. This is how i did in react. Im sure you can the same way in Vanilla JS too.

    I made two variable "countries" & "filteredContries". After initial network request , i put same result to both the variable. "countries" variable act as a backup, to get back initial result even after filtering and search. And then i made a function to update "filteredCountries" variable based on different scenario of filtering and searching together.

    In DOM im looping through filteredCountries and not through "countries" variable.

    There is what i did in that function:

    if (option === "All" && searchInput === "") setFilteredCountries(countries);
        else if (option === "All" && searchInput !== "") {
          setFilteredCountries(
            countries.filter((country) =>
              country.name.toLowerCase().includes(searchInput)
            )
          );
        } else if (option !== "All" && searchInput === "") {
          setFilteredCountries(
            countries.filter((country) => country.region === option)
          );
        } else {
          setFilteredCountries(
            countries.filter((country) => {
              return (
                country.name.toLowerCase().includes(searchInput) &&
                country.region === option
              );
            })
          );
        }
    

    I hope this helped you out. If you do not understand any path feel free to write back. I am happy to help. I am a self-tought dev i know the importance of helpin ppl out. Im no expert.

    Keep solving these problems and you will be pro in no time..

    Repo for Vanilla JS: https://github.com/besttlookk/FEM-REST_countries_with_theme_switch (look into dist folder)

    Repo for React version: https://github.com/besttlookk/FEM-rest-countries-react-version (look into "src/pages/Home"

    Good luck

    Marked as helpful
  • Prabhash Ranjan•2,540
    @besttlookk
    Posted about 3 years ago

    Hi, There are few issues i like to point out.

    1. Filter and search does not work at the same time. What if i first search something and then try to use filter?
    2. I guess you are making network request each times user search or filter. Why bother, when you already have complete data on first re-load. Try to searcha and filter from that.
    3. When search result is few, card becomes huge.
    4. You dark mode toggle is not persistent, it chages to after refresh.

    and finally a suggestion.

    1. Add some hover effect to inhance intractivity.
    2. When there is no result. let user know that. Black screen doen not look good.

    and again here is mine: https://rest-countries-fem-v2.herokuapp.com/

    Good luck

    #happyCoding

    Marked as helpful

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
Frontend Mentor logo

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

Frontend Mentor

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

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

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

How does the accessibility report work?

When a solution is submitted, we use axe-core to run an automated audit of your code.

This picks out common accessibility issues like not using semantic HTML and not having proper heading hierarchies, among others.

This automated audit is fairly surface level, so we encourage to you review the project and code in more detail with accessibility best practices in mind.

How does the CSS report work?

When a solution is submitted, we use stylelint to run an automated check on the CSS code.

We've added some of our own linting rules based on recommended best practices. These rules are prefixed with frontend-mentor/ which you'll see at the top of each issue in the report.

The report will audit 1st-party linked stylesheets, and styles within <style> tags.

How does the HTML validation report work?

When a solution is submitted, we use html-validate to run an automated check on the HTML code.

The report picks out common HTML issues such as not using headings within section elements and incorrect nesting of elements, among others.

Note that the report can pick up “invalid” attributes, which some frameworks automatically add to the HTML. These attributes are crucial for how the frameworks function, although they’re technically not valid HTML. As such, some projects can show up with many HTML validation errors, which are benign and are a necessary part of the framework.

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub