File tree Expand file tree Collapse file tree 4 files changed +116
-7
lines changed
Expand file tree Collapse file tree 4 files changed +116
-7
lines changed Original file line number Diff line number Diff line change 1+ .charts {
2+ margin-top : 5rem ;
3+ display : flex;
4+ justify-content : center;
5+ align-items : center
6+ }
Original file line number Diff line number Diff line change 1+ import React , { Component } from "react" ;
2+ import {
3+ LineChart ,
4+ Line ,
5+ XAxis ,
6+ YAxis ,
7+ CartesianGrid ,
8+ Tooltip ,
9+ Legend ,
10+ } from "recharts" ;
11+ import colors from "../colors" ;
12+ import "./Charts.css" ;
13+
14+ export default class Charts extends Component {
15+ render ( ) {
16+ const { data, isLoading } = this . props ;
17+ const result = data . map ( ( dataItem ) => {
18+ return {
19+ date : dataItem . day ,
20+ ...dataItem . summary ,
21+ confirmed : dataItem . summary . total ,
22+ active :
23+ dataItem . summary . total -
24+ ( dataItem . summary . discharged + dataItem . summary . deaths ) ,
25+ } ;
26+ } ) ;
27+ return (
28+ < div className = "charts" >
29+ { ! isLoading && (
30+ < LineChart width = { 700 } height = { 500 } data = { result } >
31+ < XAxis dataKey = "date" />
32+ < YAxis />
33+ < CartesianGrid strokeDasharray = "3 3" />
34+ < Tooltip />
35+ < Legend />
36+ < Line
37+ type = "monotone"
38+ dataKey = "confirmed"
39+ stroke = { colors . red }
40+ activeDot = { { r : 8 } }
41+ />
42+ < Line
43+ type = "monotone"
44+ dataKey = "active"
45+ stroke = { colors . orange }
46+ activeDot = { { r : 8 } }
47+ />
48+ < Line
49+ type = "monotone"
50+ dataKey = "discharged"
51+ stroke = { colors . green }
52+ activeDot = { { r : 8 } }
53+ />
54+ < Line
55+ type = "monotone"
56+ dataKey = "deaths"
57+ stroke = { colors . purple }
58+ activeDot = { { r : 8 } }
59+ />
60+ </ LineChart >
61+ ) }
62+ </ div >
63+ ) ;
64+ }
65+ }
Original file line number Diff line number Diff line change @@ -4,11 +4,12 @@ import Navbar from "./Navbar";
44import { withStyles } from "@material-ui/styles" ;
55import colors from "../colors" ;
66import "./CodeApp.css" ;
7+ import Charts from "./Charts" ;
78
89const styles = {
910 root : {
1011 display : "flex" ,
11- height : "100vh" ,
12+ minHeight : "100vh" ,
1213 } ,
1314 navBar : {
1415 flex : "0 0 10%" ,
@@ -44,7 +45,22 @@ class CovidApp extends Component {
4445 constructor ( props ) {
4546 super ( props ) ;
4647
47- this . state = { } ;
48+ this . state = {
49+ completeData : [ ] ,
50+ isLoading : false ,
51+ } ;
52+ this . getData = this . getData . bind ( this ) ;
53+ this . loadingStatus = this . loadingStatus . bind ( this ) ;
54+ }
55+
56+ getData ( data , isLoading ) {
57+ console . log ( "isLoading" , isLoading ) ;
58+ this . setState ( { completeData : data , isLoading : isLoading } ) ;
59+ }
60+
61+ loadingStatus ( loadingStatus ) {
62+ console . log ( "loadingstatus" , loadingStatus ) ;
63+ this . setState ( { isLoading : loadingStatus } ) ;
4864 }
4965
5066 render ( ) {
@@ -67,7 +83,15 @@ class CovidApp extends Component {
6783 </ label >
6884 </ div >
6985 </ div >
70- < Overview isDarkMode = { isDarkMode } />
86+ < Overview
87+ isDarkMode = { isDarkMode }
88+ getData = { this . getData }
89+ loadingStatus = { this . loadingStatus }
90+ />
91+ < Charts
92+ data = { this . state . completeData }
93+ isLoading = { this . state . isLoading }
94+ />
7195 </ div >
7296 </ div >
7397 ) ;
Original file line number Diff line number Diff line change @@ -100,6 +100,7 @@ class Overview extends Component {
100100 currentDay : { } ,
101101 previousDay : { } ,
102102 dataChanges : { } ,
103+ completeData : { } ,
103104 isLoading : false ,
104105 } ;
105106 this . calculateChange = this . calculateChange . bind ( this ) ;
@@ -111,7 +112,10 @@ class Overview extends Component {
111112 }
112113
113114 async fetchData ( ) {
114- this . setState ( { isLoading : ! this . state . isLoading } ) ;
115+ this . setState (
116+ { isLoading : ! this . state . isLoading } ,
117+ this . props . loadingStatus ( ! this . state . isLoading )
118+ ) ;
115119 const response = await axios . get (
116120 "https://api.rootnet.in/covid19-in/stats/history"
117121 ) ;
@@ -134,6 +138,7 @@ class Overview extends Component {
134138 activeCases :
135139 currentDay . total - ( currentDay . discharged + currentDay . deaths ) ,
136140 } ,
141+ completeData : response . data . data ,
137142 } ,
138143 this . calculateChange
139144 ) ;
@@ -145,7 +150,17 @@ class Overview extends Component {
145150 a [ k ] = currentDay [ k ] - previousDay [ k ] ;
146151 return a ;
147152 } , { } ) ;
148- this . setState ( { dataChanges : newObj , isLoading : ! isLoading } ) ;
153+ const newState = {
154+ ...this . state ,
155+ dataChanges : newObj ,
156+ isLoading : ! isLoading ,
157+ } ;
158+ this . setState (
159+ {
160+ ...newState ,
161+ } ,
162+ this . props . getData ( newState . completeData , newState . isLoading )
163+ ) ;
149164 }
150165
151166 render ( ) {
@@ -192,8 +207,7 @@ class Overview extends Component {
192207 </ div >
193208
194209 < button className = { classes . button } onClick = { this . fetchData } >
195- { /* <FontAwesomeIcon icon={faSyncAlt} className={classes.refreshIcon} /> */ }
196- Update Results
210+ Update
197211 </ button >
198212 </ div >
199213 ) ;
You can’t perform that action at this time.
0 commit comments