-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
172 lines (160 loc) · 4.71 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import React, { useState, useEffect } from "react";
import "./App.css";
import {
MenuItem,
FormControl,
Select,
Card,
CardContent,
TextField,
} from "@material-ui/core";
import Stats from "./Stats";
import CasesMap from "./CasesMap";
import { makeStyles } from "@material-ui/core/styles";
import Table from "./Table";
import { sortData } from "./sort";
import LineGraph from "./Graph";
import "leaflet/dist/leaflet.css";
import { statStyle } from "./statStyle";
const useStyles = makeStyles({
root: {
background: "#90caf9",
border: 0,
borderRadius: 3,
color: "#27313A",
height: 80,
width: 280,
borderRadius: 6,
boxShadow: " -5px 3px 82px -6px rgba( 144, 202, 249,0.2);",
},
});
function App() {
const [countries, setCountries] = useState([]);
const [country, setCountry] = useState("worldwide");
const [mapCountries, setMapCountries] = useState([]);
const [countryInfo, setCountryInfo] = useState({});
const [tableData, setTableData] = useState([]);
const [mapCenter, setMapCenter] = useState({ lat: 40.3, lng: 10 });
const [mapZoom, setMapZoom] = useState(2.4);
const [caseType, setCaseType] = useState("cases");
useEffect(() => {
fetch("https://disease.sh/v3/covid-19/all")
.then((resp) => resp.json())
.then((data) => {
setCountryInfo(data);
});
}, []);
useEffect(() => {
const getCountriesData = async () => {
await fetch("https://disease.sh/v3/covid-19/countries")
.then((resp) => resp.json())
.then((data) => {
const countries = data.map((country) => ({
name: country.country,
value: country.countryInfo.iso2,
}));
let sortedData = sortData(data);
setTableData(sortedData);
setMapCountries(data);
setCountries(countries);
});
};
getCountriesData();
}, []);
const onCountryChange = async (e) => {
const countryCode = e.target.value;
const url =
countryCode === "worldwide"
? "https://disease.sh/v3/covid-19/all"
: `https://disease.sh/v3/covid-19/countries/${countryCode}`;
await fetch(url)
.then((resp) => resp.json())
.then((data) => {
setCountry(countryCode);
setCountryInfo(data);
setMapCenter([data.countryInfo.lat, data.countryInfo.long]);
setMapZoom(5);
});
};
const classes = useStyles();
return (
<>
<div className="App">
<header className="App-header">
<h1 style={{ color: "#90caf9" }} s>
COVID-19 Tracker
</h1>
<FormControl className="App-dropdown">
<TextField
select
label="Select"
variant="filled"
onChange={onCountryChange}
className={classes.root}
helperText="Select Your Country"
>
<MenuItem value="worldwide">Worldwide</MenuItem>
{countries.map((country) => (
<MenuItem value={country.value}>{country.name}</MenuItem>
))}
</TextField>
</FormControl>
</header>
</div>
<div className="stats">
<Stats
isGrey
active={caseType === "cases"}
onClick={(e) => setCaseType("cases")}
title="Coronavirus Cases"
cases={statStyle(countryInfo.todayCases)}
total={statStyle(countryInfo.cases)}
/>
<Stats
isGreen
active={caseType === "recovered"}
onClick={(e) => setCaseType("recovered")}
title="Coronavirus Recovered"
cases={statStyle(countryInfo.todayRecovered)}
total={statStyle(countryInfo.recovered)}
/>
<Stats
isRed
active={caseType === "deaths"}
onClick={(e) => setCaseType("deaths")}
title="Coronavirus Deaths"
cases={statStyle(countryInfo.todayDeaths)}
total={statStyle(countryInfo.deaths)}
/>
</div>
<CasesMap
center={mapCenter}
zoom={mapZoom}
countries={mapCountries}
caseType={caseType}
/>
<div className="live">
<Card className="live-card">
<CardContent>
<h3>Live Cases by Country</h3>
<Table countries={tableData} />
</CardContent>
</Card>
<div className="container">
<div className="card-info">
<h3 style={{ textTransform: `capitalize` }}>
Worldwide New {caseType}
</h3>
<LineGraph className="graph-fill" casesType={caseType} />
<br />
<br />
</div>
</div>
<div>
<br />
</div>
</div>
</>
);
}
export default App;