Skip to content

cyishere/react-covid19-tracker

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
src
 
 
 
 
 
 
 
 

COVID-19 Data Tracker

A React app to show the data of Covid-19 by each country/region.

screenshot-01

screenshot-02

notes

API:

components/modules

  • chart.js
  • react-chartjs-2
  • leaflet
  • react-leaflet
  • axios
  • react-countup

NOTE: Need to give .leaflet-container a height & a width, otherwise it won't show up. And also need to add the default style of leaflet - import "leaflet/dist/leaflet.css";.

materials


I used this API https://covid19.mathdro.id/api at the beginning which made creating the 'Cases by Country/Region' list quite inconvenient, but because of this I learned a lot of Promise. Here's the code:

import axios from "axios";

const url = "https://covid19.mathdro.id/api";

// Fetch data by region
const FetchByRegion = async (name) => {
  try {
    const {
      data: { confirmed, recovered, deaths },
    } = await axios.get(`${url}/countries/${name}`);
    return {
      name,
      confirmed,
      recovered,
      deaths,
    };
  } catch (error) {
    console.log("error", error.message);
  }
};

// Fetch data for regions
const fetchRegionData = async () => {
  try {
    const {
      data: { countries },
    } = await axios.get(`${url}/countries`);

    const regionNames = countries
      .filter((country) => country.name !== "Gambia")
      .map((country) => country.name);

    // let regionDatas = [];

    const regionDatas = regionNames.map(async (name) => {
      return await FetchByRegion(name);
    });

    return Promise.all(regionDatas);
  } catch (error) {
    console.log("error", error.message);
  }
};

export { fetchRegionData };