Skip to content

Instantly share code, notes, and snippets.

@sabesansathananthan
Created June 16, 2020 15:40
Show Gist options
  • Save sabesansathananthan/135e8e8181bd0a8f03802452889f5685 to your computer and use it in GitHub Desktop.
Save sabesansathananthan/135e8e8181bd0a8f03802452889f5685 to your computer and use it in GitHub Desktop.
Fetch Data from API for Create a COVID-19 Tracker using React.js
import axios from "axios";
const url = "https://covid19.mathdro.id/api";
export const fetchData = async (country) => {
let changeableUrl = url;
if (country) {
changeableUrl = `${url}/countries/${country}`;
}
try {
const {
data: { confirmed, recovered, deaths, lastUpdate },
} = await axios.get(changeableUrl);
return {
confirmed,
recovered,
deaths,
lastUpdate,
};
} catch (error) {
console.log(error);
}
};
export const fetchDailyData = async () => {
try {
const { data } = await axios.get(`${url}/daily`);
const modifiedData = data.map((dailyData) => ({
confirmed: dailyData.confirmed.total,
deaths: dailyData.deaths.total,
date: dailyData.reportDate,
}));
return modifiedData;
} catch (error) {}
};
export const fetchCountries = async () => {
try {
const {
data: { countries },
} = await axios.get(`${url}/countries`);
return countries.map((country) => country.name);
} catch (error) {
console.log(error);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment