Skip to content

Commit

Permalink
stuff refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
andreyfoggy committed Mar 25, 2020
1 parent b3594b8 commit bdf8a27
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 51 deletions.
16 changes: 6 additions & 10 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ const path = require('path');
const request = require('request');

const app = express();
let regionsINFO = null;

getRegionsData();
initAPI()
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() {
Expand All @@ -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;
});
}
6 changes: 3 additions & 3 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ <h2>Поширення COVID-19 в Україні</h2>
<agm-map [fullscreenControl]="true"
(mapClick)="onMapClick($event)"
[maxZoom]="10"
[zoom]="6"
[zoom]="5"
[latitude]="lat"
[longitude]="lng">

Expand All @@ -15,7 +15,7 @@ <h2>Поширення COVID-19 в Україні</h2>
<div class="info-wraper">
<div class="region-name">{{marker.title}}</div>
<div class="cases-counter">захворювань: {{marker.cases}}</div>
<div *ngIf="marker.deaths" class="death-counter">смертей: {{marker.deaths}}</div>
<div *ngIf="marker.deaths" class="death-counter">загинуло: {{marker.deaths}}</div>
</div>
</agm-info-window>
</agm-marker>
Expand All @@ -32,7 +32,7 @@ <h2>Поширення COVID-19 в Україні</h2>
</div>
<div class="general-info">
<div style="color: #aaa;">Усього випадків виявлено: {{ casesAll }}</div>
<div style="color: #696969;">Усього смертей: {{ deathsAll }}</div>
<div style="color: #696969;">Усього загинуло: {{ deathsAll }}</div>
</div>
</div>

Expand Down
56 changes: 29 additions & 27 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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<any>) {
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();
}
Expand Down
44 changes: 34 additions & 10 deletions src/app/app.constants.ts
Original file line number Diff line number Diff line change
@@ -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,
}
];
2 changes: 1 addition & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { }

0 comments on commit bdf8a27

Please sign in to comment.