Skip to content

Commit da45fb5

Browse files
author
Andrew Evans
committed
unwrap observable to correctly use RxJS operators
1 parent 0337a2b commit da45fb5

8 files changed

+26
-62
lines changed

src/app/cards/current-conditions/current-conditions.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<mat-grid-list cols="4" rowHeight="4em" *ngIf="data != undefined; else elseBlock">
1+
<mat-grid-list cols="4" rowHeight="4em" *ngIf="data$ | async as data; else elseBlock">
22
<mat-grid-tile [colspan]="2" [rowspan]="1">
33
<p><span class="title">Latitude</span><span>{{ data.currentConditions.latitude }}</span></p>
44
</mat-grid-tile>
Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,22 @@
1-
import { Component, OnInit, OnDestroy } from '@angular/core';
1+
import { Component, OnInit } from '@angular/core';
22
import { WeatherData } from 'src/app/models/weather-data/weather-data';
3-
import { Store } from '@ngrx/store';
3+
import { Store, select } from '@ngrx/store';
44
import { AppState, selectWeather } from 'src/app/reducers';
5-
import { Subject } from 'rxjs';
6-
import { takeUntil } from 'rxjs/operators';
5+
import { Observable } from 'rxjs';
76

87
@Component({
98
selector: 'app-current-conditions',
109
templateUrl: './current-conditions.component.html',
1110
styleUrls: ['./current-conditions.component.css']
1211
})
13-
export class CurrentConditionsComponent implements OnInit, OnDestroy {
12+
export class CurrentConditionsComponent implements OnInit {
1413

15-
data: WeatherData;
16-
private unsubscribeWeather: Subject<void> = new Subject<void>();
14+
data$: Observable<WeatherData>;
1715

1816
constructor(private store: Store<AppState>) { }
1917

2018
ngOnInit(): void {
21-
this.store
22-
.select(selectWeather)
23-
.pipe(takeUntil(this.unsubscribeWeather))
24-
.subscribe((weather) => this.data = weather);
25-
}
26-
27-
ngOnDestroy() {
28-
this.unsubscribeWeather.next();
19+
this.data$ = this.store.pipe(select(selectWeather));
2920
}
3021

3122
}

src/app/cards/hourly-forecast/hourly-forecast.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div class="hourly-forecast" *ngIf="data != undefined; else elseBlock">
1+
<div class="hourly-forecast" *ngIf="data$ | async as data; else elseBlock">
22
<table mat-table [dataSource]="data.hourlyForecast" class="mat-elevation-z8">
33
<ng-container matColumnDef="Time">
44
<th mat-header-cell *matHeaderCellDef> <span>Time</span> </th>
Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,24 @@
1-
import { Component, OnInit, OnDestroy } from '@angular/core';
1+
import { Component, OnInit } from '@angular/core';
22
import { WeatherData } from 'src/app/models/weather-data/weather-data';
3-
import { Store } from '@ngrx/store';
3+
import { Store, select } from '@ngrx/store';
44
import { AppState, selectWeather } from 'src/app/reducers';
5-
import { Subject } from 'rxjs';
6-
import { takeUntil } from 'rxjs/operators';
5+
import { Observable } from 'rxjs';
76

87
@Component({
98
selector: 'app-hourly-forecast',
109
templateUrl: './hourly-forecast.component.html',
1110
styleUrls: ['./hourly-forecast.component.css']
1211
})
13-
export class HourlyForecastComponent implements OnInit, OnDestroy {
12+
export class HourlyForecastComponent implements OnInit {
1413

1514
displayedColumns: string[] = ['Time', 'Temp', 'Wind', 'Condition'];
1615

17-
data: WeatherData;
18-
private unsubscribeWeather: Subject<void> = new Subject<void>();
16+
data$: Observable<WeatherData>;
1917

2018
constructor(private store: Store<AppState>) { }
2119

2220
ngOnInit(): void {
23-
this.store
24-
.select(selectWeather)
25-
.pipe(takeUntil(this.unsubscribeWeather))
26-
.subscribe((weather) => this.data = weather);
27-
}
28-
29-
ngOnDestroy() {
30-
this.unsubscribeWeather.next();
21+
this.data$ = this.store.pipe(select(selectWeather));
3122
}
3223

3324
}

src/app/cards/weather-discussion/weather-discussion.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div class="weather-discussion" *ngIf="data != undefined; else elseBlock">
1+
<div class="weather-discussion" *ngIf="data$ | async as data; else elseBlock">
22
<mat-accordion>
33
<mat-expansion-panel *ngFor="let week of data.weeklyForecast">
44
<mat-expansion-panel-header>
Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,22 @@
1-
import { Component, OnInit, OnDestroy } from '@angular/core';
1+
import { Component, OnInit } from '@angular/core';
22
import { WeatherData } from 'src/app/models/weather-data/weather-data';
3-
import { Store } from '@ngrx/store';
3+
import { Store, select } from '@ngrx/store';
44
import { AppState, selectWeather } from 'src/app/reducers';
5-
import { Subject } from 'rxjs';
6-
import { takeUntil } from 'rxjs/operators';
5+
import { Observable } from 'rxjs';
76

87
@Component({
98
selector: 'app-weather-discussion',
109
templateUrl: './weather-discussion.component.html',
1110
styleUrls: ['./weather-discussion.component.css']
1211
})
13-
export class WeatherDiscussionComponent implements OnInit, OnDestroy {
12+
export class WeatherDiscussionComponent implements OnInit {
1413

15-
data: WeatherData;
16-
private unsubscribeWeather: Subject<void> = new Subject<void>();
14+
data$: Observable<WeatherData>;
1715

1816
constructor(private store: Store<AppState>) { }
1917

2018
ngOnInit(): void {
21-
this.store
22-
.select(selectWeather)
23-
.pipe(takeUntil(this.unsubscribeWeather))
24-
.subscribe((weather) => this.data = weather);
25-
}
26-
27-
ngOnDestroy() {
28-
this.unsubscribeWeather.next();
19+
this.data$ = this.store.pipe(select(selectWeather));
2920
}
3021

3122
}

src/app/cards/weekly-forecast/weekly-forecast.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div class="forecast-tile">
2-
<table class="table-responsive-sm" *ngIf="data != undefined; else elseBlock">
2+
<table class="table-responsive-sm" *ngIf="data$ | async as data; else elseBlock">
33
<tbody>
44
<td *ngFor="let weekday of data.weeklyForecast">
55
<tr class="name">{{weekday.name}}</tr>

src/app/cards/weekly-forecast/weekly-forecast.component.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,21 @@ import { Component, OnInit, OnDestroy } from '@angular/core';
22
import { WeatherData } from 'src/app/models/weather-data/weather-data';
33
import { Store, select } from '@ngrx/store';
44
import { AppState, selectWeather } from 'src/app/reducers';
5-
import { map, takeUntil } from 'rxjs/operators';
6-
import { Subject } from 'rxjs';
5+
import { Observable } from 'rxjs';
76

87
@Component({
98
selector: 'app-weekly-forecast',
109
templateUrl: './weekly-forecast.component.html',
1110
styleUrls: ['./weekly-forecast.component.css']
1211
})
13-
export class WeeklyForecastComponent implements OnInit, OnDestroy {
12+
export class WeeklyForecastComponent implements OnInit {
1413

15-
data: WeatherData;
16-
private unsubscribeWeather: Subject<void> = new Subject<void>();
14+
data$: Observable<WeatherData>;
1715

1816
constructor(private store: Store<AppState>) { }
1917

2018
ngOnInit(): void {
21-
this.store
22-
.select(selectWeather)
23-
.pipe(takeUntil(this.unsubscribeWeather))
24-
.subscribe((weather) => this.data = weather);
25-
}
26-
27-
ngOnDestroy() {
28-
this.unsubscribeWeather.next();
19+
this.data$ = this.store.pipe(select(selectWeather));
2920
}
3021

3122
}

0 commit comments

Comments
 (0)