Projects/Covid19 Tracker
Initial Set up & Covid19 API_20210428 _Day1
Nomad Kim
2021. 4. 29. 00:17
Framework
[ADD]Make Category function and Data table
- Make Coronavirus/Recovered/Deaths Info Boxes
- Get data from disease.sh(API) and Structure through Material UI
Covid19 API(Fetch Data)
import React, { useState, useEffect } from 'react'
import './App.css';
import {
MenuItem,
FormControl,
Select,
Card,
CardContent
} from "@material-ui/core"
import InfoBox from './Infobox'
import Map from './Map'
function App() {
//๋ชจ๋ ๊ตญ๊ฐ์ { ๊ตญ๊ฐ๋ช
, ๊ตญ๊ฐ์ฝ๋ } ๋ฐ์ดํฐ๋ฅผ ์นดํ
๊ณ ๋ฆฌ ๋ชฉ๋ก์์ ์ฌ์ฉ
const [countries, setContries] = useState([]);
//์ ํ๋ ๊ตญ๊ฐ์ ๊ตญ๊ฐ์ฝ๋ ๋ฅผ fetch ์ ์ฌ์ฉ
const [country, setCountry] = useState("worldwide");
//์ ํ๋ ๊ตญ๊ฐ์ ๋ชจ๋ ๋ฐ์ดํฐ
const [countryInfo, setCountryInfo] = useState({});
//default ๋ฐ์ดํฐ๋ก ์ ์ธ๊ณ ์ฝ๋ก๋ ์ํฉ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅ ๋ฐ ์ฌ์ฉ
useEffect(()=>{
fetch('https://disease.sh/v3/covid-19/all')
.then(res => res.json())
.then(data => {
setCountryInfo(data);
})
},[])
//์ ์ธ๊ณ ๊ตญ๊ฐ๋ช
๋ฐ ๊ตญ๊ฐ์ฝ๋ ๋ฐ์ดํฐ๋ก ์นดํ
๊ณ ๋ฆฌ ์์ฑ(๊ตญ๊ฐ์ฝ๋๋ฅผ value ๊ฐ์ผ๋ก ์ด์ฉ)
useEffect(() => {
const getCountriesData = async () => {
await fetch("https://disease.sh/v3/covid-19/countries")
.then(res => res.json())
.then((data) =>{
const countries = data.map((country)=> (
{
name:country.country,
value: country.countryInfo.iso2
}
))
setContries(countries);
})
}
getCountriesData();
}, [])
//์ ํ๋ ๊ตญ๊ฐ์ ๊ตญ๊ฐ์ฝ๋๋ฅผ ์ด์ฉํ์ฌ ์ฝ๋ก๋ ๋ฐ์ดํฐ fetch
const onCountryChange = async (e) => {
const countryCode = e.target.value
setCountry(countryCode);
const url = countryCode === "worldwide" ? 'https://disease.sh/v3/covid-19/all' : `https://disease.sh/v3/covid-19/countries/${countryCode}`
await fetch(url)
.then(res => res.json())
.then(data => {
setCountry(countryCode)
setCountryInfo(data);
})
//https://disease.sh/v3/covid-19/all
//https://disease.sh/v3/covid-19/countries/[COUNTRY_CODE]
}
return (
<div className="app">
<div className="app__left">
<div className="app__header">
<h1>Let's build a COVID 19 TRACKER!</h1>
<FormControl className="app__dropdown">
<Select value={country} variant="outlined" onChange={onCountryChange}>
<MenuItem value="worldwide">Worldwide</MenuItem>
{/* Loop through all the countries and show a drop down */}
{countries.map((country) => (
<MenuItem value={country.value}>{country.name}</MenuItem>
))}
</Select>
</FormControl>
</div>
<div className="app__stats">
<InfoBox title="Coronavirus" cases={countryInfo.todayCases} total={countryInfo.cases}/>
<InfoBox title="Recovered" cases={countryInfo.todayRecovered} total={countryInfo.recovered}/>
<InfoBox title="Deaths" cases={countryInfo.todayDeaths} total={countryInfo.deaths}/>
</div>
<Map/>
</div>
<Card className="app__right">
<CardContent>
<h3>Live Cases by Country</h3>
<h3>Worldwide new cases</h3>
</CardContent>
{/* Table */}
{/* Graph */}
</Card>
</div>
);
}
export default App;
Source from: disease.sh/docs/
์นดํ ๊ณ ๋ฆฌ์ ๋ฐ๋ฅธ API ์์ฒญ ํ Infobox ์ ๋ ๋๋ง
onChange => setCountryInfo => Infobox
<FormControl className="app__dropdown">
<Select value={country} variant="outlined" onChange={onCountryChange}>
<MenuItem value="worldwide">Worldwide</MenuItem>
{/* Loop through all the countries and show a drop down */}
{countries.map((country) => (
<MenuItem value={country.value}>{country.name}</MenuItem>
))}
</Select>
</FormControl>
const onCountryChange = async (e) => {
const countryCode = e.target.value
setCountry(countryCode);
const url = countryCode === "worldwide" ? 'https://disease.sh/v3/covid-19/all' : `https://disease.sh/v3/covid-19/countries/${countryCode}`
await fetch(url)
.then(res => res.json())
.then(data => {
setCountry(countryCode)
setCountryInfo(data);
if(countryCode === 'worldwide'){
setMapCenter({ lat: 37, lng: 127.5});
setMapZoom(3);
} else {
setMapCenter([data.countryInfo.lat, data.countryInfo.long]);
setMapZoom(5);
}
})
//https://disease.sh/v3/covid-19/all
//https://disease.sh/v3/covid-19/countries/[COUNTRY_CODE]
}
return (
<InfoBox
isRed
active={casesType === 'cases'}
onClick={e => setCasesType('cases')}
title="Coronavirus"
cases={prettyPrintStat(countryInfo.todayCases)}
total={prettyPrintStat(countryInfo.cases)}
/>
)
//util.js
export const prettyPrintStat = (stat) => (
stat ? `+${numeral(stat).format("0,0a")}`: "+0"
)
import React from 'react'
import { Card, CardContent, Typography } from "@material-ui/core"
import './InfoBox.css'
function Infobox({ title, cases, total, active, isRed, ...props }) { //...props passes onClick event
// console.log(active, isRed)
return (
<Card onClick={props.onClick} className={`infoBox ${active && 'infoBox--selected'} ${isRed && 'infoBox--red'}`}>
<CardContent>
<Typography className="infoBox__title" color="textSecondary">
{title}
</Typography>
<h2 className={`infoBox__cases ${!isRed && "infoBox__cases--green"}`}>{cases}</h2>
<Typography className="infoBox__total" color="textSecondary">{total} Total</Typography>
</CardContent>
</Card>
)
}
export default Infobox
Material UI
๋งค์ฐ ํธ๋ฆฌํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ. ์นดํ ๊ณ ๋ฆฌ ๋ฒํผ์ ํด๋ฆญํ์ ๋ ์ ๋๋ฉ์ด์ ๊น์ง ์ ์ฉ๋์ด ์๋ค. ๋ ์ฌ์ฉํด ๋ด์ผ๊ฒ ๋ค.
Data source from disease.sh/