From bdf8a2769a8d6bd8d006cacb6c3f2936414af934 Mon Sep 17 00:00:00 2001 From: andreyfoggy Date: Wed, 25 Mar 2020 15:41:14 +0200 Subject: [PATCH] stuff refactored --- server.js | 16 ++++------- src/app/app.component.html | 6 ++-- src/app/app.component.ts | 56 ++++++++++++++++++++------------------ src/app/app.constants.ts | 44 +++++++++++++++++++++++------- src/app/app.module.ts | 2 +- 5 files changed, 73 insertions(+), 51 deletions(-) diff --git a/server.js b/server.js index 1555bf3..d0d361e 100644 --- a/server.js +++ b/server.js @@ -3,9 +3,7 @@ const path = require('path'); const request = require('request'); const app = express(); -let regionsINFO = null; -getRegionsData(); initAPI() initAngularApp(); @@ -13,8 +11,12 @@ initAngularApp(); function initAPI() { - app.get('/regions', (req,res) => res.status(200) - .json({ regions: regionsINFO })); + app.get('/regions', (req, serverResponse) => { + request('https://cdn.pravda.com/cdn/covid-19/ukraine.json', { json: true }, (err, res, body) => { + serverResponse.status(200) + .json({ regions: body }); + }); + }); } function initAngularApp() { @@ -25,10 +27,4 @@ function initAngularApp() { }); app.listen(process.env.PORT || 8080); -} - -function getRegionsData(res) { - request('https://cdn.pravda.com/cdn/covid-19/ukraine.json', { json: true }, (err, res, body) => { - regionsINFO = body; - }); } \ No newline at end of file diff --git a/src/app/app.component.html b/src/app/app.component.html index c70a224..8facb9c 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -4,7 +4,7 @@

Поширення COVID-19 в Україні

@@ -15,7 +15,7 @@

Поширення COVID-19 в Україні

{{marker.title}}
захворювань: {{marker.cases}}
-
смертей: {{marker.deaths}}
+
загинуло: {{marker.deaths}}
@@ -32,7 +32,7 @@

Поширення COVID-19 в Україні

Усього випадків виявлено: {{ casesAll }}
-
Усього смертей: {{ deathsAll }}
+
Усього загинуло: {{ deathsAll }}
diff --git a/src/app/app.component.ts b/src/app/app.component.ts index d18488f..0492c9e 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,11 +1,8 @@ -import { Component, ViewChild, OnInit } from '@angular/core'; +import { Component, OnInit } from '@angular/core'; import { MapService } from './services/map.service'; -import { markers } from './app.constants'; +import { regions } from './app.constants'; import { HttpService } from './services/http.service'; -enum IconColors { - orange = 20 -} @Component({ selector: 'app-root', templateUrl: './app.component.html', @@ -18,43 +15,48 @@ export class AppComponent implements OnInit { casesAll = 0; deathsAll = 0; private previous; - constructor(private map: MapService, private http: HttpService) { - } + + constructor(private map: MapService, private http: HttpService) { } ngOnInit() { this.http.getCasesData() - .subscribe((res: any) => { - console.log(res) - const regionCases = res; - - this.markers = markers.map(marker => { - const casesData = regionCases.find(region => region.name === marker.id); - return { - ...marker, - cases: casesData.confirmed, - deaths: casesData.deaths, - icon: this.getIcon(casesData.confirmed, casesData.deaths) - } - }); + .subscribe(res => { + this.setMarkers(res); - this.markers.forEach(marker => { - if (Number(marker.cases)) { this.casesAll += marker.cases; } - if (Number(marker.deaths)) { this.deathsAll += marker.deaths; } - }); + this.setSummary(); }) } - private getIcon(cases, deaths) { + private setMarkers(regionCasesCollection: Array) { + this.markers = regions.map(region => { + const regionCasesElem = regionCasesCollection.find(item => item.name === region.name); + return { + ...region, + cases: regionCasesElem.confirmed, + deaths: regionCasesElem.deaths, + icon: this.getIcon(regionCasesElem.confirmed, regionCasesElem.deaths) + } + }); + } + + private setSummary() { + this.markers.forEach(marker => { + if (Number(marker.cases)) { this.casesAll += marker.cases; } + if (Number(marker.deaths)) { this.deathsAll += marker.deaths; } + }); + } + + private getIcon(cases: number, deaths: number) { const shape = deaths ? 'diamond' : 'circle'; const color = cases < 20 ? 'orange' : cases < 100 ? 'red' : 'purple'; return `assets/icons/${shape}-${color}.png`; } public onMarkerClick(e, infowindow) { - this.closePrevious(infowindow); + this.closePreviousInfoWindow(infowindow); } - private closePrevious(infowindow) { + private closePreviousInfoWindow(infowindow) { if (this.previous && this.previous.close) { this.previous.close(); } diff --git a/src/app/app.constants.ts b/src/app/app.constants.ts index 4978598..0dbd585 100644 --- a/src/app/app.constants.ts +++ b/src/app/app.constants.ts @@ -1,56 +1,80 @@ -export const markers = [ +export const regions = [ { - id: 'kiev', + name: 'kiev', title: 'Київська область', lat: 50.27, lng: 30.45, }, { - id: 'zhytomyr', + name: 'zhytomyr', title: 'Житомирська область', lat: 50.05, lng: 28.63, }, { - id: 'chernivtsi', + name: 'chernivtsi', title: 'Чернівецька область', lat: 48.12, lng: 26.05, }, { - id: 'donetsk', + name: 'donetsk', title: 'Донецька область', lat: 47.85, lng: 37.73, }, { - id: 'dnipropetrovsk', + name: 'dnipropetrovsk', title: 'Дніпропетровська область', lat: 48.30, lng: 35.05, }, { - id: 'ivanofrankivsk', + name: 'ivanofrankivsk', title: 'Івано-Франковська область', lat: 48.76, lng: 24.55, }, { - id: 'lviv', + name: 'lviv', title: 'Львівська область', lat: 49.71, lng: 24.02, }, { - id: 'ternopil', + name: 'ternopil', title: 'Тернопільска область', lat: 49.34, lng: 25.60, }, { - id: 'cherkasy', + name: 'cherkasy', title: 'Черкаська область', lat: 49.27, lng: 32.00, + }, + { + name: 'odessa', + title: 'Одеська область', + lat: 46.27, + lng: 30.65, + }, + { + name: 'luhansk', + title: 'Луганська область', + lat: 48.35, + lng: 39.15, + }, + { + name: 'zaporizhzhya', + title: 'Запорізька область', + lat: 47.68, + lng: 35.15, + }, + { + name: 'volyn', + title: 'Волинська область', + lat: 50.75, + lng: 25.15, } ]; diff --git a/src/app/app.module.ts b/src/app/app.module.ts index fb757e6..39efece 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -22,7 +22,7 @@ import { HttpClient, HttpClientModule } from '@angular/common/http'; apiKey: 'AIzaSyCfvM1t5Hb_WywtcO6BEXtEUOuJlCpWszY', }) ], - providers: [HttpService, MapService, MarkerManager, GoogleMapsAPIWrapper], + providers: [HttpService, MarkerManager, GoogleMapsAPIWrapper], bootstrap: [AppComponent] }) export class AppModule { }