1
- import { Component , OnInit } from '@angular/core' ;
2
- import { map , startWith } from 'rxjs/operators' ;
1
+ import { Component , OnInit , OnDestroy } from '@angular/core' ;
2
+ import { map , startWith , takeUntil } from 'rxjs/operators' ;
3
3
import { Breakpoints , BreakpointObserver } from '@angular/cdk/layout' ;
4
4
import { WeatherService } from '../services/weather.service' ;
5
5
import { CurrentConditionsComponent } from '../cards/current-conditions/current-conditions.component' ;
@@ -9,11 +9,11 @@ import { HourlyForecastComponent } from '../cards/hourly-forecast/hourly-forecas
9
9
import { AboutDesktopComponent } from '../cards/about-desktop/about-desktop.component' ;
10
10
import { AboutMobileComponent } from '../cards/about-mobile/about-mobile.component' ;
11
11
import { Store } from '@ngrx/store' ;
12
- import { AppState } from '../reducers' ;
12
+ import { AppState , selectError } from '../reducers' ;
13
13
import { LoadLocations } from '../actions/location.actions' ;
14
14
import { LocationData } from '../models/location-data/location-data' ;
15
15
import { FormControl } from '@angular/forms' ;
16
- import { Observable } from 'rxjs' ;
16
+ import { Observable , Subject } from 'rxjs' ;
17
17
import * as USCities from '../../assets/us_cities.json' ;
18
18
import { City } from '../models/city/city' ;
19
19
import { MatAutocompleteSelectedEvent } from '@angular/material' ;
@@ -24,7 +24,7 @@ import { LoadWeather } from '../actions/weather.actions';
24
24
templateUrl : './weather.component.html' ,
25
25
styleUrls : [ './weather.component.css' ]
26
26
} )
27
- export class WeatherComponent implements OnInit {
27
+ export class WeatherComponent implements OnInit , OnDestroy {
28
28
29
29
lat : string ;
30
30
long : string ;
@@ -38,6 +38,8 @@ export class WeatherComponent implements OnInit {
38
38
filteredCities : Observable < City [ ] > ;
39
39
cities = [ ] ;
40
40
mobileView = false ;
41
+ error : string ;
42
+ private unsubscribeError : Subject < void > = new Subject < void > ( ) ;
41
43
42
44
cards = this . breakpointObserver . observe ( Breakpoints . Handset ) . pipe (
43
45
map ( ( { matches } ) => {
@@ -159,6 +161,11 @@ export class WeatherComponent implements OnInit {
159
161
}
160
162
161
163
ngOnInit ( ) : void {
164
+ this . store
165
+ . select ( selectError )
166
+ . pipe ( takeUntil ( this . unsubscribeError ) )
167
+ . subscribe ( ( error ) => this . error = error ) ;
168
+
162
169
try {
163
170
navigator . geolocation . getCurrentPosition ( ( position ) => {
164
171
this . savePosition ( position ) ;
@@ -178,7 +185,7 @@ export class WeatherComponent implements OnInit {
178
185
}
179
186
}
180
187
181
- this . store . dispatch ( new LoadLocations ( { locationData : this . locationData } ) ) ;
188
+ this . store . dispatch ( new LoadLocations ( { locationData : this . locationData , error : null } ) ) ;
182
189
}
183
190
184
191
onSelectionChanged ( event : MatAutocompleteSelectedEvent ) {
@@ -187,10 +194,14 @@ export class WeatherComponent implements OnInit {
187
194
this . locationData . latitude = city . latitude ;
188
195
this . locationData . longitude = city . longitude ;
189
196
this . store . dispatch ( new LoadWeather ( { weatherData : null } ) ) ;
190
- this . store . dispatch ( new LoadLocations ( { locationData : this . locationData } ) ) ;
197
+ this . store . dispatch ( new LoadLocations ( { locationData : this . locationData , error : null } ) ) ;
191
198
break ;
192
199
}
193
200
}
194
201
}
195
202
203
+ ngOnDestroy ( ) {
204
+ this . unsubscribeError . next ( ) ;
205
+ }
206
+
196
207
}
0 commit comments