From 9d5e0a2ef29d1a2539a493f3dc9a5c605a90783d Mon Sep 17 00:00:00 2001 From: Casey Cesari Date: Wed, 4 Apr 2018 11:42:27 -0400 Subject: [PATCH 1/2] Use API city service data for chart city parameter Follow-up to #1063 The city data returned from the API contains the "dataset" field, which is needed to fully populate the indicator charts. We also make the switch to importing the City model from the CCC library as "ApiCity" for better disambiguation between the Temperate model Location. These two models were previously conflated. Refs #1064 --- .../risk-popover/risk-popover.component.html | 2 +- .../risk-popover/risk-popover.component.ts | 16 ++++++++++++---- .../planit/src/app/core/constants/cache.ts | 2 +- .../src/app/core/services/auth.service.ts | 2 +- .../src/app/core/services/city.service.ts | 13 ++++++++++++- .../grouped-risk/grouped-risk.component.html | 2 +- .../grouped-risk/grouped-risk.component.ts | 19 +++++++++++++------ .../app/indicators/indicators.component.html | 2 +- .../app/indicators/indicators.component.ts | 15 ++++++++++++--- .../hazard-step/hazard-step.component.html | 2 +- .../hazard-step/hazard-step.component.ts | 16 ++++++++++++---- .../src/app/shared/chart/chart.component.ts | 6 +++--- .../collapsible-chart.component.html | 2 +- .../collapsible-chart.component.ts | 4 ++-- ...ce-collapse-chart-container.component.html | 2 +- ...orce-collapse-chart-container.component.ts | 4 ++-- .../indicator-chart.component.html | 4 ++-- .../indicator-chart.component.ts | 4 ++-- 18 files changed, 80 insertions(+), 37 deletions(-) diff --git a/src/angular/planit/src/app/assessment/risk-popover/risk-popover.component.html b/src/angular/planit/src/app/assessment/risk-popover/risk-popover.component.html index 8c662dcab..bf8f83a86 100644 --- a/src/angular/planit/src/app/assessment/risk-popover/risk-popover.component.html +++ b/src/angular/planit/src/app/assessment/risk-popover/risk-popover.component.html @@ -37,6 +37,6 @@

Climate data related to {{ risk.weather_event. + [apiCity]="apiCity"> diff --git a/src/angular/planit/src/app/assessment/risk-popover/risk-popover.component.ts b/src/angular/planit/src/app/assessment/risk-popover/risk-popover.component.ts index b819d720c..9c2176ef9 100644 --- a/src/angular/planit/src/app/assessment/risk-popover/risk-popover.component.ts +++ b/src/angular/planit/src/app/assessment/risk-popover/risk-popover.component.ts @@ -3,6 +3,7 @@ import { Component, Input, OnInit, ViewChild } from '@angular/core'; import { PopoverDirective } from 'ngx-bootstrap/popover'; import { + City as ApiCity, Indicator, IndicatorService } from 'climate-change-components'; @@ -12,6 +13,7 @@ import { ModalTemplateComponent } from '../../shared/modal-template/modal-templa import { Location } from '../../shared/models/location.model'; import { Risk } from '../../shared/models/risk.model'; +import { CityService } from '../../core/services/city.service'; import { UserService } from '../../core/services/user.service'; @Component({ @@ -23,7 +25,7 @@ export class RiskPopoverComponent implements OnInit { public indicators: Indicator[]; public selectedIndicator: Indicator; - public city: Location; + public apiCity: ApiCity; @ViewChild('indicatorModal') private indicatorModal: ModalTemplateComponent; @@ -32,13 +34,19 @@ export class RiskPopoverComponent implements OnInit { private popoverElement: PopoverDirective; constructor (private indicatorService: IndicatorService, + private cityService: CityService, private userService: UserService) {} ngOnInit() { this.updateRiskIndicators(); - this.userService.current().subscribe(user => { - this.city = user.primary_organization.location; - }); + this.userService.current() + .switchMap((user) => { + const id = user.primary_organization.location.properties.api_city_id; + return this.cityService.get(id); + }) + .subscribe((apiCity) => { + this.apiCity = apiCity; + }); } public openIndicatorModal(indicator: Indicator) { diff --git a/src/angular/planit/src/app/core/constants/cache.ts b/src/angular/planit/src/app/core/constants/cache.ts index ec0e65d93..a07c8b033 100644 --- a/src/angular/planit/src/app/core/constants/cache.ts +++ b/src/angular/planit/src/app/core/constants/cache.ts @@ -1,5 +1,5 @@ // Naming convention: MODULE_SERVICE_METHOD for clarity and uniqueness export const CORE_USERSERVICE_CURRENT = 'core.userservice.current'; -export const CORE_CITYSERVICE_CURRENT = 'core.cityservice.current'; +export const CORE_CITYSERVICE_GET = 'core.cityservice.get'; export const CORE_ACTIONTYPESERVICE_LIST = 'core.actiontypeservice.list'; export const CORE_WEATHEREVENTSERVICE_LIST = 'core.weathereventservice.list'; diff --git a/src/angular/planit/src/app/core/services/auth.service.ts b/src/angular/planit/src/app/core/services/auth.service.ts index 4e7a1de5f..6bb1790e7 100644 --- a/src/angular/planit/src/app/core/services/auth.service.ts +++ b/src/angular/planit/src/app/core/services/auth.service.ts @@ -6,7 +6,7 @@ import { Observable, Subject } from 'rxjs/Rx'; import { APICacheService } from 'climate-change-components'; import { environment } from '../../../environments/environment'; -import { CORE_CITYSERVICE_CURRENT, CORE_USERSERVICE_CURRENT } from '../constants/cache'; +import { CORE_USERSERVICE_CURRENT } from '../constants/cache'; import { User } from '../../shared/'; diff --git a/src/angular/planit/src/app/core/services/city.service.ts b/src/angular/planit/src/app/core/services/city.service.ts index 7456f9b75..810f35321 100644 --- a/src/angular/planit/src/app/core/services/city.service.ts +++ b/src/angular/planit/src/app/core/services/city.service.ts @@ -7,7 +7,7 @@ import { environment } from '../../../environments/environment'; import { City as ApiCity } from 'climate-change-components'; import { APICacheService } from 'climate-change-components'; -import { CORE_CITYSERVICE_CURRENT } from '../constants/cache'; +import { CORE_CITYSERVICE_GET } from '../constants/cache'; import { PlanItApiHttp } from './api-http.service'; import { UserService } from './user.service'; @@ -17,6 +17,17 @@ export class CityService { constructor(private apiHttp: PlanItApiHttp, private cache: APICacheService, private userService: UserService) {} + get(id: string): Observable { + const url = `${environment.apiUrl}/api/climate-api/api/city/${id}/`; + const request = this.apiHttp.get(url); + const response = this.cache.get(CORE_CITYSERVICE_GET, request); + + return response.map((resp) => { + const data = resp.json() as ApiCity; + return data; + }); + } + search(query: string): Observable { const url = `${environment.apiUrl}/api/climate-api/api/city/?search=${query}`; return this.apiHttp.get(url).map(resp => { diff --git a/src/angular/planit/src/app/dashboard/grouped-risk/grouped-risk.component.html b/src/angular/planit/src/app/dashboard/grouped-risk/grouped-risk.component.html index f71ba5c44..0ce48ab2b 100644 --- a/src/angular/planit/src/app/dashboard/grouped-risk/grouped-risk.component.html +++ b/src/angular/planit/src/app/dashboard/grouped-risk/grouped-risk.component.html @@ -70,7 +70,7 @@
Action steps
+ [apiCity]="apiCity">