Giter Site home page Giter Site logo

countries-api-with-color-theme-switcher's Introduction

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 (optional)

Screenshot

Links

My process

Built with

  • Semantic HTML5 markup
  • CSS custom properties
  • Flexbox
  • Vanilla Javascript
  • Icons - For fontawesome icons
  • font family - For Google fonts

What I learned

Use this section to recap over some of your major learnings while working through this project. Writing these out and providing code samples of areas you want to highlight is a great way to reinforce your own knowledge.

To see how you can add code snippets, see below:

<!-- country api cards -->
    <section id="countries" class="light-mode dark-mode">
        <div class="container countries-flex">
            <!-- card info constructed in Javacript -->
        </div>
    </section>
document.addEventListener('DOMContentLoaded', () => {
  // search for the country name that has been passed into the url from the previous page, on clicking the cards
  const urlParamsCountry = new URLSearchParams(window.location.search);
  const countryName = urlParamsCountry.get('country');

  // grab the theme passed into the url from the home page
  const urlParamsTheme = new URLSearchParams(window.location.search);
  const currentTheme = urlParamsTheme.get('theme');

  // set the theme of the detail page based on the theme received from the home page
  if (currentTheme === 'dark-mode') {
      switchDarkMode();
  } else {
      switchLightMode();
  }

  // prints the country name as the dom title
  document.title = `${countryName} | Frontend Mentor`;

  // Load the details of the selected country
  detailUI.countriesDetailInfo(countryName);
});


// function to set the event listener on the countries to load another page containing the information of the borders
setBorderEventListener(countryDetails) {
  // loop through the borders to perform the function
  Array.from(countryDetails).forEach(data => {
    // listen for a click event on the border buttons
    data.addEventListener('click', (e) => {
      const borderCountry = e.target.textContent;

      // call the api to load the countries
      countries.loadCountriesAPI()
      .then(countryData => {
        countryData.forEach(country => {
          // validate that the border country selected is the same as the country alphacode to return the country details
          if (borderCountry === country.alpha3Code) {
            // Navigate to the another detail page with the selected country name in the URL and the theme
            const countryName = country.name;
            const currentTheme = localStorage.getItem('theme');
            window.location.href = `detail.html?country=${encodeURIComponent(countryName)}&theme=${currentTheme}`;
          }
        })
      })
    })
  })
}

Continued development

I would like to research better and more efficient ways of setting color themes(light and dark) and ways to save them into the local storage better. Using react would be a better and easier way to complete this project, so i would love to repeat this using reactJS.

Author

countries-api-with-color-theme-switcher's People

Contributors

trayshmhirk avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.