Skip to content

Commit 2a9ae87

Browse files
committed
As of Module 11
1 parent e2b7539 commit 2a9ae87

File tree

6 files changed

+66
-4
lines changed

6 files changed

+66
-4
lines changed

APM-CLI-Final/src/app/app.module.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { ConvertToSpacesPipe } from './shared/convert-to-spaces.pipe';
1010
import { StarComponent } from './shared/star.component';
1111
import { ProductDetailComponent } from './products/product-detail.component';
1212
import { WelcomeComponent } from './home/welcome.component';
13+
import { ProductGuardService } from './products/product-guard.service';
1314

1415
@NgModule({
1516
declarations: [
@@ -26,13 +27,15 @@ import { WelcomeComponent } from './home/welcome.component';
2627
HttpModule,
2728
RouterModule.forRoot([
2829
{ path: 'products', component: ProductListComponent },
29-
{ path: 'products/:id', component: ProductDetailComponent },
30+
{ path: 'products/:id',
31+
canActivate: [ ProductGuardService ],
32+
component: ProductDetailComponent },
3033
{ path: 'welcome', component: WelcomeComponent },
3134
{ path: '', redirectTo: 'welcome', pathMatch: 'full'},
3235
{ path: '**', redirectTo: 'welcome', pathMatch: 'full'}
3336
])
3437
],
35-
providers: [],
38+
providers: [ProductGuardService],
3639
bootstrap: [AppComponent]
3740
})
3841
export class AppModule { }

APM-CLI-Final/src/app/products/product-detail.component.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@
22
<div class='panel-heading'>
33
{{pageTitle + ': ' + product.productName}}
44
</div>
5+
<div class='panel-footer'>
6+
<a class='btn btn-default' (click)='onBack()' style='width:80px'>
7+
<i class='glyphicon glyphicon-chevron-left'></i> Back
8+
</a>
9+
</div>
510
</div>

APM-CLI-Final/src/app/products/product-detail.component.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Component, OnInit } from '@angular/core';
2+
import { ActivatedRoute, Router } from '@angular/router';
23

34
import { IProduct } from './product';
45

@@ -10,9 +11,26 @@ export class ProductDetailComponent implements OnInit {
1011
pageTitle: string = 'Product Detail';
1112
product: IProduct;
1213

13-
constructor() { }
14+
constructor(private _route: ActivatedRoute,
15+
private _router: Router) { }
1416

1517
ngOnInit() {
18+
let id = +this._route.snapshot.paramMap.get('id');
19+
this.pageTitle += `: ${id}`;
20+
this.product = {
21+
"productId": id,
22+
"productName": "Leaf Rake",
23+
"productCode": "GDN-0011",
24+
"releaseDate": "March 19, 2016",
25+
"description": "Leaf rake with 48-inch wooden handle.",
26+
"price": 19.95,
27+
"starRating": 3.2,
28+
"imageUrl": "http://openclipart.org/image/300px/svg_to_png/26215/Anonymous_Leaf_Rake.png"
29+
}
30+
}
31+
32+
onBack(): void {
33+
this._router.navigate(['/products']);
1634
}
1735

1836
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { TestBed, inject } from '@angular/core/testing';
2+
3+
import { ProductGuardService } from './product-guard.service';
4+
5+
describe('ProductGuardService', () => {
6+
beforeEach(() => {
7+
TestBed.configureTestingModule({
8+
providers: [ProductGuardService]
9+
});
10+
});
11+
12+
it('should be created', inject([ProductGuardService], (service: ProductGuardService) => {
13+
expect(service).toBeTruthy();
14+
}));
15+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Injectable } from '@angular/core';
2+
import { CanActivate, ActivatedRouteSnapshot, Router } from '@angular/router';
3+
4+
@Injectable()
5+
export class ProductGuardService implements CanActivate {
6+
7+
constructor(private _router: Router) { }
8+
9+
canActivate(route: ActivatedRouteSnapshot): boolean {
10+
let id = +route.url[1].path;
11+
if (isNaN(id) || id < 1) {
12+
alert('Invalid product Id');
13+
this._router.navigate(['/products']);
14+
return false;
15+
};
16+
return true;
17+
}
18+
}

APM-CLI-Final/src/app/products/product-list.component.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ <h3>Filtered by: {{listFilter}} </h3>
4141
[style.width.px]='imageWidth'
4242
[style.margin.px]='imageMargin'>
4343
</td>
44-
<td>{{ product.productName }}</td>
44+
<td><a [routerLink]="['/products', product.productId]">
45+
{{ product.productName }}
46+
</a>
47+
</td>
4548
<td>{{ product.productCode | lowercase | convertToSpaces: '-' }}</td>
4649
<td>{{ product.releaseDate }}</td>
4750
<td>{{ product.price | currency:'USD':true:'1.2-2'}}</td>

0 commit comments

Comments
 (0)