Skip to content

A page that displays data of countries that is gathered from an API. The user can browse around countries, filter them by name or region and change from light mode to dark mode.

Notifications You must be signed in to change notification settings

ViniciusLCLima/rest-countries-api-front-end-challenge

Repository files navigation

Frontend Mentor - REST Countries API with color theme switcher solution

This is a solution to the REST Countries API with color theme switcher challenge on Frontend Mentor. Frontend Mentor challenges help you improve your coding skills by building realistic projects.

Table of contents

Overview

The challenge

Users should be able to:

  • See all countries from the API on the homepage
  • Search for a country using an input field
  • Filter countries by region
  • Click on a country to see more detailed information on a separate page
  • Click through to the border countries on the detail page
  • Toggle the color scheme between light and dark mode

Screenshot

App screenshot. Contains a header saying "Where in the world" with a button to change the mode from light to dark and, below it, country cards with country flags and information in a grid.

Links

My process

Built with

  • Semantic HTML5 markup
  • CSS custom properties
  • Flexbox
  • CSS Grid
  • Mobile-first workflow
  • JavaScript

What I learned

I've found it challenging to implement the back button of the app because this return should occur without the page reloading, so that the API data does not have to be requested again, worsening performance. I've implemented it by storing the pages already visited by the user in that session and, when they click the back button, instead of simply returning 1 page through the history.back method, I use the history.replaceState method, which changes the current url withouth redirecting the page and then I call the app function which executes the app which, in turn, will use the url to render the previous page.

    if (numVisitedInThisSite>0){
        window.history.replaceState({}, '', location.origin + Window.vLCountriesAPI.lastVisitedPages.pop())
        app(true)
        return
    }

It was also a difficult task to architecture the code so that some parts of home page were only tried to be rendered when needed. In order to do that, I've divided the function "renderCards" in 2 functions: "renderCards" and "renderHome", which calls "renderCards". When the user uses the form to filter countries, only renderCards needs to be called because the form is already rendered.

    Window.vLCountriesAPI.filter.name = searchInput.value
    Window.vLCountriesAPI.filter.region = regionFilter
    renderCards(app)

On the other hand, if the user is entering the site for the first time, render home needs to be called.

    switch (true){
        case '/'== decodedUrlPathname:
            renderHome(app)
            break

Continued development

For the future, I'm aiming to work on front-end frameworks and libraries and CSS preprocessors.

Useful resources

Author

About

A page that displays data of countries that is gathered from an API. The user can browse around countries, filter them by name or region and change from light mode to dark mode.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published