Skip to content

Commit 15b48e1

Browse files
committed
merge with master
2 parents 1d4ff4c + 06fc121 commit 15b48e1

File tree

57 files changed

+140
-85
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+140
-85
lines changed

APM-Demo0/src/app/products/product-list.component.ts renamed to APM-Demo0/src/app/products/product-list/product-list.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { Component, OnInit, OnDestroy } from '@angular/core';
22

33
import { Subscription } from 'rxjs';
44

5-
import { Product } from './product';
6-
import { ProductService } from './product.service';
5+
import { Product } from '../product';
6+
import { ProductService } from '../product.service';
77

88
@Component({
99
selector: 'pm-product-list',

APM-Demo0/src/app/products/product.module.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { RouterModule, Routes } from '@angular/router';
33

44
import { SharedModule } from '../shared/shared.module';
55

6-
import { ProductShellComponent } from './product-shell.component';
7-
import { ProductListComponent } from './product-list.component';
8-
import { ProductEditComponent } from './edit/product-edit.component';
6+
import { ProductShellComponent } from './product-shell/product-shell.component';
7+
import { ProductListComponent } from './product-list/product-list.component';
8+
import { ProductEditComponent } from './product-edit/product-edit.component';
99
import { ProductService } from './product.service';
1010

1111
const productRoutes: Routes = [

APM-Demo0/src/app/products/product.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Injectable } from '@angular/core';
2-
import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http';
2+
import { HttpClient, HttpHeaders } from '@angular/common/http';
33

44
import { Observable, of, BehaviorSubject, throwError } from 'rxjs';
55
import { catchError, tap, map } from 'rxjs/operators';
@@ -93,7 +93,7 @@ export class ProductService {
9393
);
9494
}
9595

96-
private handleError(err: HttpErrorResponse) {
96+
private handleError(err) {
9797
// in a real world app, we may send the server to some remote logging infrastructure
9898
// instead of just logging it to the console
9999
let errorMessage: string;
@@ -103,7 +103,7 @@ export class ProductService {
103103
} else {
104104
// The backend returned an unsuccessful response code.
105105
// The response body may contain clues as to what went wrong,
106-
errorMessage = `Backend returned code ${err.status}, body was: ${err.error}`;
106+
errorMessage = `Backend returned code ${err.status}: ${err.body.error}`;
107107
}
108108
console.error(err);
109109
return throwError(errorMessage);

APM-Demo1/src/app/products/product-list.component.ts renamed to APM-Demo1/src/app/products/product-list/product-list.component.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { Component, OnInit, OnDestroy } from '@angular/core';
22

33
import { Subscription } from 'rxjs';
44

5-
import { Product } from './product';
6-
import { ProductService } from './product.service';
5+
import { Product } from '../product';
6+
import { ProductService } from '../product.service';
77

88
/* NgRx */
99
import { Store } from '@ngrx/store';
@@ -38,6 +38,7 @@ export class ProductListComponent implements OnInit, OnDestroy {
3838
(err: any) => this.errorMessage = err.error
3939
);
4040

41+
// TODO: Unsubscribe
4142
this.store.select('products').subscribe(
4243
products => {
4344
if (products) {

APM-Demo1/src/app/products/product.module.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { RouterModule, Routes } from '@angular/router';
33

44
import { SharedModule } from '../shared/shared.module';
55

6-
import { ProductShellComponent } from './product-shell.component';
7-
import { ProductListComponent } from './product-list.component';
8-
import { ProductEditComponent } from './edit/product-edit.component';
6+
import { ProductShellComponent } from './product-shell/product-shell.component';
7+
import { ProductListComponent } from './product-list/product-list.component';
8+
import { ProductEditComponent } from './product-edit/product-edit.component';
99
import { ProductService } from './product.service';
1010

1111
/* NgRx */

APM-Demo1/src/app/products/product.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Injectable } from '@angular/core';
2-
import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http';
2+
import { HttpClient, HttpHeaders } from '@angular/common/http';
33

44
import { Observable, of, BehaviorSubject, throwError } from 'rxjs';
55
import { catchError, tap, map } from 'rxjs/operators';
@@ -93,7 +93,7 @@ export class ProductService {
9393
);
9494
}
9595

96-
private handleError(err: HttpErrorResponse) {
96+
private handleError(err) {
9797
// in a real world app, we may send the server to some remote logging infrastructure
9898
// instead of just logging it to the console
9999
let errorMessage: string;
@@ -103,7 +103,7 @@ export class ProductService {
103103
} else {
104104
// The backend returned an unsuccessful response code.
105105
// The response body may contain clues as to what went wrong,
106-
errorMessage = `Backend returned code ${err.status}, body was: ${err.error}`;
106+
errorMessage = `Backend returned code ${err.status}: ${err.body.error}`;
107107
}
108108
console.error(err);
109109
return throwError(errorMessage);

APM-Demo1/src/app/user/login.component.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ export class LoginComponent implements OnInit {
2222
private router: Router) {
2323
}
2424

25-
// Homework
2625
ngOnInit(): void {
26+
// TODO: Unsubscribe
2727
this.store.select('users').subscribe(
2828
users => {
2929
if (users) {
@@ -37,7 +37,6 @@ export class LoginComponent implements OnInit {
3737
this.router.navigate(['welcome']);
3838
}
3939

40-
// Homework
4140
checkChanged(value: boolean): void {
4241
this.store.dispatch({
4342
type: 'MASK_USER_NAME',

APM-Demo3/src/app/products/edit/product-edit.component.ts renamed to APM-Demo2/src/app/products/product-edit/product-edit.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export class ProductEditComponent implements OnInit, OnDestroy {
6565
});
6666

6767
// Watch for changes to the currently selected product
68+
// TODO: Unsubscribe
6869
this.store.pipe(select(fromProduct.getCurrentProduct)).subscribe(
6970
selectedProduct => this.displayProduct(selectedProduct)
7071
);
@@ -76,7 +77,7 @@ export class ProductEditComponent implements OnInit, OnDestroy {
7677
}
7778

7879
ngOnDestroy(): void {
79-
// TODO: Should we be unsubscribing?
80+
8081
}
8182

8283
// Also validate on blur

APM-Demo2/src/app/products/product-list.component.ts renamed to APM-Demo2/src/app/products/product-list/product-list.component.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { Component, OnInit, OnDestroy } from '@angular/core';
22

3-
import { Product } from './product';
4-
import { ProductService } from './product.service';
3+
import { Product } from '../product';
4+
import { ProductService } from '../product.service';
55

66
/* NgRx */
77
import { Store, select } from '@ngrx/store';
8-
import * as fromProduct from './state/product.reducer';
9-
import * as productActions from './state/product.actions';
8+
import * as fromProduct from '../state/product.reducer';
9+
import * as productActions from '../state/product.actions';
1010

1111
@Component({
1212
selector: 'pm-product-list',
@@ -28,22 +28,24 @@ export class ProductListComponent implements OnInit, OnDestroy {
2828
private productService: ProductService) { }
2929

3030
ngOnInit(): void {
31+
// TODO: Unsubscribe
32+
this.store.pipe(select(fromProduct.getCurrentProduct)).subscribe(
33+
currentProduct => this.selectedProduct = currentProduct
34+
);
35+
3136
this.productService.getProducts().subscribe(
3237
(products: Product[]) => this.products = products,
3338
(err: any) => this.errorMessage = err.error
3439
);
3540

36-
this.store.pipe(select(fromProduct.getCurrentProduct)).subscribe(
37-
currentProduct => this.selectedProduct = currentProduct
38-
);
39-
41+
// TODO: Unsubscribe
4042
this.store.pipe(select(fromProduct.getShowProductCode)).subscribe(
4143
showProductCode => this.displayCode = showProductCode
4244
);
4345
}
4446

4547
ngOnDestroy(): void {
46-
// TODO: Should we unsubscribe?
48+
4749
}
4850

4951
checkChanged(value: boolean): void {

APM-Demo2/src/app/products/product.module.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { RouterModule, Routes } from '@angular/router';
33

44
import { SharedModule } from '../shared/shared.module';
55

6-
import { ProductShellComponent } from './product-shell.component';
7-
import { ProductListComponent } from './product-list.component';
8-
import { ProductEditComponent } from './edit/product-edit.component';
6+
import { ProductShellComponent } from './product-shell/product-shell.component';
7+
import { ProductListComponent } from './product-list/product-list.component';
8+
import { ProductEditComponent } from './product-edit/product-edit.component';
99
import { ProductService } from './product.service';
1010

1111
/* NgRx */

APM-Demo2/src/app/products/state/product.reducer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function reducer(state = initialState, action: ProductActions): ProductSt
4747
case ProductActionTypes.SetCurrentProduct:
4848
return {
4949
...state,
50-
currentProduct: { ... action.payload }
50+
currentProduct: { ...action.payload }
5151
};
5252

5353
case ProductActionTypes.ClearCurrentProduct:

APM-Demo2/src/app/user/login.component.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ export class LoginComponent implements OnInit {
2323
private authService: AuthService,
2424
private router: Router) {}
2525

26-
// Homework
2726
ngOnInit(): void {
27+
// TODO: Unsubscribe
2828
this.store.pipe(select(fromUser.getMaskUserName)).subscribe(
2929
maskUserName => this.maskUserName = maskUserName
3030
);
@@ -34,7 +34,6 @@ export class LoginComponent implements OnInit {
3434
this.router.navigate(['welcome']);
3535
}
3636

37-
// Homework
3837
checkChanged(value: boolean): void {
3938
this.store.dispatch(new userActions.MaskUserName(value));
4039
}

APM-Demo2/src/app/user/user.module.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ const userRoutes: Routes = [
1919
imports: [
2020
SharedModule,
2121
RouterModule.forChild(userRoutes),
22-
// Homework
2322
StoreModule.forFeature('users', reducer)
2423
],
2524
declarations: [

APM-Demo2/src/app/products/edit/product-edit.component.ts renamed to APM-Demo3/src/app/products/product-edit/product-edit.component.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { ProductService } from '../product.service';
66
import { GenericValidator } from '../../shared/generic-validator';
77
import { NumberValidators } from '../../shared/number.validator';
88

9+
import { takeWhile } from 'rxjs/operators';
10+
911
/* NgRx */
1012
import { Store, select } from '@ngrx/store';
1113
import * as fromProduct from '../state/product.reducer';
@@ -19,6 +21,7 @@ import * as productActions from '../state/product.actions';
1921
export class ProductEditComponent implements OnInit, OnDestroy {
2022
pageTitle = 'Product Edit';
2123
errorMessage = '';
24+
alive = true;
2225
productForm: FormGroup;
2326

2427
product: Product | null;
@@ -64,8 +67,10 @@ export class ProductEditComponent implements OnInit, OnDestroy {
6467
description: ''
6568
});
6669

67-
// Watch for changes to the currently selected product
68-
this.store.pipe(select(fromProduct.getCurrentProduct)).subscribe(
70+
this.store.pipe(
71+
select(fromProduct.getCurrentProduct),
72+
takeWhile(() => this.alive)
73+
).subscribe(
6974
selectedProduct => this.displayProduct(selectedProduct)
7075
);
7176

@@ -76,7 +81,7 @@ export class ProductEditComponent implements OnInit, OnDestroy {
7681
}
7782

7883
ngOnDestroy(): void {
79-
// TODO: Should we be unsubscribing?
84+
this.alive = false;
8085
}
8186

8287
// Also validate on blur

APM-Demo4/src/app/products/product-list.component.ts renamed to APM-Demo3/src/app/products/product-list/product-list.component.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { Component, OnInit, OnDestroy } from '@angular/core';
22

3-
import { Observable } from 'rxjs';
3+
import { Product } from '../product';
44

5-
import { Product } from './product';
5+
import { Observable } from 'rxjs';
6+
import { takeWhile } from 'rxjs/operators';
67

78
/* NgRx */
89
import { Store, select } from '@ngrx/store';
9-
import * as fromProduct from './state/product.reducer';
10-
import * as productActions from './state/product.actions';
10+
import * as fromProduct from '../state/product.reducer';
11+
import * as productActions from '../state/product.actions';
1112

1213
@Component({
1314
selector: 'pm-product-list',
@@ -17,6 +18,7 @@ import * as productActions from './state/product.actions';
1718
export class ProductListComponent implements OnInit, OnDestroy {
1819
pageTitle = 'Products';
1920
errorMessage$: Observable<string>;
21+
alive = true;
2022

2123
displayCode: boolean;
2224

@@ -25,7 +27,7 @@ export class ProductListComponent implements OnInit, OnDestroy {
2527
// Used to highlight the selected product in the list
2628
selectedProduct: Product | null;
2729

28-
constructor(private store: Store<fromProduct.ProductState>) {}
30+
constructor(private store: Store<fromProduct.ProductState>) { }
2931

3032
ngOnInit(): void {
3133

@@ -39,18 +41,24 @@ export class ProductListComponent implements OnInit, OnDestroy {
3941
this.store.dispatch(new productActions.Load());
4042

4143
// Subscribe here because it does not use an async pipe
42-
this.store.pipe(select(fromProduct.getCurrentProduct)).subscribe(
44+
this.store.pipe(
45+
select(fromProduct.getCurrentProduct),
46+
takeWhile(() => this.alive)
47+
).subscribe(
4348
currentProduct => this.selectedProduct = currentProduct
4449
);
4550

4651
// Subscribe here because it does not use an async pipe
47-
this.store.pipe(select(fromProduct.getShowProductCode)).subscribe(
52+
this.store.pipe(
53+
select(fromProduct.getShowProductCode),
54+
takeWhile(() => this.alive)
55+
).subscribe(
4856
showProductCode => this.displayCode = showProductCode
4957
);
5058
}
5159

5260
ngOnDestroy(): void {
53-
// TODO: Should we unsubscribe?
61+
this.alive = false;
5462
}
5563

5664
checkChanged(value: boolean): void {

APM-Demo3/src/app/products/product.module.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { RouterModule, Routes } from '@angular/router';
33

44
import { SharedModule } from '../shared/shared.module';
55

6-
import { ProductShellComponent } from './product-shell.component';
7-
import { ProductListComponent } from './product-list.component';
8-
import { ProductEditComponent } from './edit/product-edit.component';
6+
import { ProductShellComponent } from './product-shell/product-shell.component';
7+
import { ProductListComponent } from './product-list/product-list.component';
8+
import { ProductEditComponent } from './product-edit/product-edit.component';
99
import { ProductService } from './product.service';
1010

1111
/* NgRx */

APM-Demo3/src/app/user/login.component.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import { Component, OnInit, OnDestroy } from '@angular/core';
22
import { NgForm } from '@angular/forms';
33
import { Router, ActivatedRoute } from '@angular/router';
44

55
import { AuthService } from './auth.service';
66

7+
import { takeWhile } from 'rxjs/operators';
8+
79
/* NgRx */
810
import { Store, select } from '@ngrx/store';
911
import * as fromUser from './state/user.reducer';
@@ -13,22 +15,30 @@ import * as userActions from './state/user.actions';
1315
templateUrl: './login.component.html',
1416
styleUrls: ['./login.component.css']
1517
})
16-
export class LoginComponent implements OnInit {
18+
export class LoginComponent implements OnInit, OnDestroy {
1719
pageTitle = 'Log In';
1820
errorMessage: string;
21+
alive = true;
1922

2023
maskUserName: boolean;
2124

2225
constructor(private store: Store<fromUser.UserState>,
2326
private authService: AuthService,
24-
private router: Router) {}
27+
private router: Router) { }
2528

2629
ngOnInit(): void {
27-
this.store.pipe(select(fromUser.getMaskUserName)).subscribe(
30+
this.store.pipe(
31+
select(fromUser.getMaskUserName),
32+
takeWhile(() => this.alive)
33+
).subscribe(
2834
maskUserName => this.maskUserName = maskUserName
2935
);
3036
}
3137

38+
ngOnDestroy(): void {
39+
this.alive = false;
40+
}
41+
3242
cancel(): void {
3343
this.router.navigate(['welcome']);
3444
}

0 commit comments

Comments
 (0)