1
1
import React , { Component } from "react" ;
2
+ import { formatDistance } from "date-fns" ;
2
3
import Overview from "./Overview" ;
3
4
import { withStyles } from "@material-ui/styles" ;
4
5
import colors from "../constants/colors" ;
@@ -7,14 +8,18 @@ import DisplayTable from "./DisplayTable";
7
8
import styles from "../styles/CovidAppStyles" ;
8
9
import axios from "axios" ;
9
10
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome" ;
10
- import { faSyncAlt } from "@fortawesome/free-solid-svg-icons" ;
11
+ import {
12
+ faSyncAlt ,
13
+ faBell ,
14
+ faBellSlash ,
15
+ } from "@fortawesome/free-solid-svg-icons" ;
11
16
import "../styles/DarkModeButton.css" ;
12
17
import MapSection from "./MapSection" ;
13
18
import Barchart from "./Barchart" ;
14
19
import stateCodes from "../constants/stateCodes" ;
15
20
import Lottie from "react-lottie" ;
16
21
import * as animationData from "../assets/loading.json" ;
17
- import FadeIn from "react-fade-in" ;
22
+ // import FadeIn from "react-fade-in";
18
23
import Footer from "./Footer" ;
19
24
20
25
const defaultOptions = {
@@ -26,6 +31,21 @@ const defaultOptions = {
26
31
} ,
27
32
} ;
28
33
34
+ const months = {
35
+ "01" : "Jan" ,
36
+ "02" : "Feb" ,
37
+ "03" : "Mar" ,
38
+ "04" : "Apr" ,
39
+ "05" : "May" ,
40
+ "06" : "Jun" ,
41
+ "07" : "Jul" ,
42
+ "08" : "Aug" ,
43
+ "09" : "Sep" ,
44
+ "10" : "Oct" ,
45
+ "11" : "Nov" ,
46
+ "12" : "Dec" ,
47
+ } ;
48
+
29
49
class CovidApp extends Component {
30
50
constructor ( props ) {
31
51
super ( props ) ;
@@ -42,6 +62,7 @@ class CovidApp extends Component {
42
62
this . formatData = this . formatData . bind ( this ) ;
43
63
this . findId = this . findId . bind ( this ) ;
44
64
this . handleFormat = this . handleFormat . bind ( this ) ;
65
+ this . handleNotification = this . handleNotification . bind ( this ) ;
45
66
}
46
67
47
68
componentDidMount ( ) {
@@ -57,12 +78,18 @@ class CovidApp extends Component {
57
78
const stateChanges = axios . get (
58
79
"https://api.covid19india.org/states_daily.json"
59
80
) ;
81
+ const updates = axios . get (
82
+ "https://api.covid19india.org/updatelog/log.json"
83
+ ) ;
60
84
61
- axios . all ( [ countryData , districtLevel , stateChanges ] ) . then (
85
+ axios . all ( [ countryData , districtLevel , stateChanges , updates ] ) . then (
62
86
axios . spread ( ( ...responses ) => {
63
87
const countryData = responses [ 0 ] . data ;
64
88
const districtLevel = responses [ 1 ] . data ;
65
89
// const stateChanges = responses[2].data;
90
+ const updates = responses [ 3 ] . data ;
91
+
92
+ // console.log(countryData.statewise[0].lastupdatedtime);
66
93
67
94
const [ todayData ] = countryData . statewise . slice ( 0 , 1 ) ;
68
95
const casesTimeline = countryData . cases_time_series ;
@@ -75,6 +102,8 @@ class CovidApp extends Component {
75
102
todayData : todayData ,
76
103
casesTimeline : casesTimeline ,
77
104
districtLevel : districtLevel ,
105
+ updates : updates ,
106
+ expanded : false ,
78
107
} ,
79
108
this . handleFormat
80
109
) ;
@@ -109,9 +138,29 @@ class CovidApp extends Component {
109
138
this . setState ( { mapData : newdata } ) ;
110
139
}
111
140
141
+ handleNotification ( ) {
142
+ this . setState ( { expanded : ! this . state . expanded } ) ;
143
+ }
144
+
145
+ formatDate ( date ) {
146
+ try {
147
+ const day = date . slice ( 0 , 2 ) ;
148
+ const month = date . slice ( 3 , 5 ) ;
149
+ const time = date . slice ( 11 ) ;
150
+ return `${ day } ${ months [ month ] } , ${ time . slice ( 0 , 5 ) } IST` ;
151
+ } catch ( err ) { }
152
+ }
153
+
112
154
render ( ) {
113
155
const { classes, setDarkMode, isDarkMode } = this . props ;
114
- const { mapData, isLoading, data, districtLevel } = this . state ;
156
+ const {
157
+ mapData,
158
+ isLoading,
159
+ data,
160
+ districtLevel,
161
+ expanded,
162
+ updates,
163
+ } = this . state ;
115
164
116
165
if ( isLoading ) {
117
166
return (
@@ -120,9 +169,34 @@ class CovidApp extends Component {
120
169
</ div >
121
170
) ;
122
171
}
172
+ let displayUpdates ;
173
+ try {
174
+ displayUpdates = updates
175
+ . slice ( - 5 )
176
+ . reverse ( )
177
+ . map ( ( { update, timestamp } , i ) => {
178
+ update = update . replace ( "\n" , "<br/>" ) ;
179
+ return (
180
+ < div className = { classes . updateBox } key = { i } >
181
+ < h5 className = { classes . updateHeading } >
182
+ { `${ formatDistance (
183
+ new Date ( timestamp * 1000 ) ,
184
+ new Date ( )
185
+ ) } ago`}
186
+ </ h5 >
187
+ < h4
188
+ className = { classes . updateText }
189
+ dangerouslySetInnerHTML = { {
190
+ __html : update ,
191
+ } }
192
+ > </ h4 >
193
+ </ div >
194
+ ) ;
195
+ } ) ;
196
+ } catch ( err ) { }
123
197
124
198
return (
125
- < FadeIn >
199
+ < >
126
200
< div className = { classes . header } >
127
201
< h1 className = { classes . heading } >
128
202
< span > Covid-19</ span > India Trend
@@ -134,6 +208,28 @@ class CovidApp extends Component {
134
208
onClick = { this . fetchData }
135
209
/>
136
210
</ div >
211
+ < div className = { classes . lastUpdatedTime } >
212
+ Last Updated:{ " " }
213
+ { this . formatDate ( this . state . todayData . lastupdatedtime ) }
214
+ </ div >
215
+ < div className = { classes . updates } >
216
+ < div className = { classes . notification } >
217
+ { expanded ? (
218
+ < FontAwesomeIcon
219
+ icon = { faBellSlash }
220
+ onClick = { this . handleNotification }
221
+ />
222
+ ) : (
223
+ < div className = { classes . notificationBell } >
224
+ < FontAwesomeIcon
225
+ icon = { faBell }
226
+ onClick = { this . handleNotification }
227
+ />
228
+ </ div >
229
+ ) }
230
+ </ div >
231
+ { expanded && < div className = { classes . update } > { displayUpdates } </ div > }
232
+ </ div >
137
233
< div className = "darkModeButton" >
138
234
< label className = "switch" >
139
235
< input
@@ -145,11 +241,13 @@ class CovidApp extends Component {
145
241
</ label >
146
242
</ div >
147
243
</ div >
148
- < Overview
149
- isDarkMode = { isDarkMode }
150
- data = { this . state . todayData }
151
- loadingStatus = { this . loadingStatus }
152
- />
244
+ < div >
245
+ < Overview
246
+ isDarkMode = { isDarkMode }
247
+ data = { this . state . todayData }
248
+ loadingStatus = { this . loadingStatus }
249
+ />
250
+ </ div >
153
251
< div className = { classes . content } >
154
252
< div className = { classes . contentArea } >
155
253
< div className = { classes . mapArea } >
@@ -238,7 +336,7 @@ class CovidApp extends Component {
238
336
</ div >
239
337
</ div >
240
338
< Footer />
241
- </ FadeIn >
339
+ </ >
242
340
) ;
243
341
}
244
342
}
0 commit comments