|
1 | 1 | export default { |
2 | 2 | SET_DATA: (state, data) => { |
3 | 3 | const { confirmed, deaths, recovered } = data |
| 4 | + const dataCollection = { |
| 5 | + type: 'FeatureCollection', |
| 6 | + features: [] |
| 7 | + } |
4 | 8 | const timeSince = (date) => { |
5 | 9 | let seconds = Math.floor((new Date() - date) / 1000) |
6 | 10 | let interval = Math.floor(seconds / 31536000) |
@@ -36,49 +40,60 @@ export default { |
36 | 40 | ) |
37 | 41 | return timeSince(last) + ' ago' |
38 | 42 | } |
39 | | - const cases = confirmed.locations |
40 | | - .reduce((result, currentValue) => { |
41 | | - if (currentValue.latest) { |
42 | | - result.push(currentValue) |
43 | | - } |
44 | | - return result |
45 | | - }, []) |
46 | | - .map((location, index) => { |
47 | | - const sortDate = dates => { |
48 | | - const sorted_date = {} |
49 | | - Object.keys(dates) |
50 | | - .sort((a, b) => { |
51 | | - return new Date(a) - new Date(b) |
52 | | - }) |
53 | | - .forEach(key => { |
54 | | - sorted_date[key] = dates[key] |
55 | | - }) |
56 | | - return sorted_date |
57 | | - } |
58 | | - return { |
| 43 | + const sortDate = dates => { |
| 44 | + const sorted_date = {} |
| 45 | + Object.keys(dates) |
| 46 | + .sort((a, b) => { |
| 47 | + return new Date(a) - new Date(b) |
| 48 | + }) |
| 49 | + .forEach(key => { |
| 50 | + sorted_date[key] = dates[key] |
| 51 | + }) |
| 52 | + return sorted_date |
| 53 | + } |
| 54 | + confirmed.locations.forEach((location, index) => { |
| 55 | + const recovered_locations = recovered.locations[index] |
| 56 | + const dead_locations = deaths.locations[index] |
| 57 | + let recovered_count = 0 |
| 58 | + let dead_count = 0 |
| 59 | + if ( |
| 60 | + location.coordinates.long === recovered_locations.coordinates.long |
| 61 | + && location.coordinates.lat === recovered_locations.coordinates.lat |
| 62 | + ) { |
| 63 | + recovered_count = recovered_locations.latest |
| 64 | + } |
| 65 | + if ( |
| 66 | + location.coordinates.long === dead_locations.coordinates.long |
| 67 | + && location.coordinates.lat === dead_locations.coordinates.lat |
| 68 | + ) { |
| 69 | + dead_count = dead_locations.latest |
| 70 | + } |
| 71 | + if (location.latest || recovered_count || dead_count) { |
| 72 | + dataCollection.features.push({ |
59 | 73 | type: 'Feature', |
60 | 74 | properties: { |
61 | 75 | country: location.country, |
62 | 76 | country_code: location.country_code, |
63 | 77 | province: location.province, |
64 | 78 | confirmed_count: location.latest, |
65 | 79 | confirmed_history: sortDate(location.history), |
66 | | - recovered_count: recovered.locations[index].latest || 0, |
67 | | - recovered_history: sortDate(recovered.locations[index].history), |
68 | | - dead_count: deaths.locations[index].latest || 0, |
69 | | - dead_history: sortDate(deaths.locations[index].history), |
| 80 | + recovered_count: recovered_count, |
| 81 | + recovered_history: sortDate(recovered_locations.history), |
| 82 | + dead_count: dead_count, |
| 83 | + dead_history: sortDate(recovered_locations.history), |
70 | 84 | last_update: lastUpdate(location.history) |
71 | 85 | }, |
72 | 86 | geometry: { |
73 | 87 | type: 'Point', |
74 | | - coordinates: [ location.coordinates.long, location.coordinates.lat ] |
| 88 | + coordinates: [ |
| 89 | + location.coordinates.long, |
| 90 | + location.coordinates.lat |
| 91 | + ] |
75 | 92 | } |
76 | | - } |
77 | | - }) |
78 | | - state.data = { |
79 | | - type: 'FeatureCollection', |
80 | | - features: cases |
81 | | - } |
| 93 | + }) |
| 94 | + } |
| 95 | + }) |
| 96 | + state.data = dataCollection |
82 | 97 | }, |
83 | 98 | SET_LATEST: (state, latest) => { |
84 | 99 | state.latest = latest |
|
0 commit comments