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";
4
4
import { withStyles } from "@material-ui/styles" ;
5
5
import colors from "../colors" ;
6
6
import "./CodeApp.css" ;
7
+ import Charts from "./Charts" ;
7
8
8
9
const styles = {
9
10
root : {
10
11
display : "flex" ,
11
- height : "100vh" ,
12
+ minHeight : "100vh" ,
12
13
} ,
13
14
navBar : {
14
15
flex : "0 0 10%" ,
@@ -44,7 +45,22 @@ class CovidApp extends Component {
44
45
constructor ( props ) {
45
46
super ( props ) ;
46
47
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 } ) ;
48
64
}
49
65
50
66
render ( ) {
@@ -67,7 +83,15 @@ class CovidApp extends Component {
67
83
</ label >
68
84
</ div >
69
85
</ 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
+ />
71
95
</ div >
72
96
</ div >
73
97
) ;
Original file line number Diff line number Diff line change @@ -100,6 +100,7 @@ class Overview extends Component {
100
100
currentDay : { } ,
101
101
previousDay : { } ,
102
102
dataChanges : { } ,
103
+ completeData : { } ,
103
104
isLoading : false ,
104
105
} ;
105
106
this . calculateChange = this . calculateChange . bind ( this ) ;
@@ -111,7 +112,10 @@ class Overview extends Component {
111
112
}
112
113
113
114
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
+ ) ;
115
119
const response = await axios . get (
116
120
"https://api.rootnet.in/covid19-in/stats/history"
117
121
) ;
@@ -134,6 +138,7 @@ class Overview extends Component {
134
138
activeCases :
135
139
currentDay . total - ( currentDay . discharged + currentDay . deaths ) ,
136
140
} ,
141
+ completeData : response . data . data ,
137
142
} ,
138
143
this . calculateChange
139
144
) ;
@@ -145,7 +150,17 @@ class Overview extends Component {
145
150
a [ k ] = currentDay [ k ] - previousDay [ k ] ;
146
151
return a ;
147
152
} , { } ) ;
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
+ ) ;
149
164
}
150
165
151
166
render ( ) {
@@ -192,8 +207,7 @@ class Overview extends Component {
192
207
</ div >
193
208
194
209
< button className = { classes . button } onClick = { this . fetchData } >
195
- { /* <FontAwesomeIcon icon={faSyncAlt} className={classes.refreshIcon} /> */ }
196
- Update Results
210
+ Update
197
211
</ button >
198
212
</ div >
199
213
) ;
You can’t perform that action at this time.
0 commit comments