Skip to content

Commit 9df1892

Browse files
committed
Add all fields to product detail page
1 parent 7178d5b commit 9df1892

File tree

3 files changed

+72
-20
lines changed

3 files changed

+72
-20
lines changed
Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,56 @@
11
<div class='card'>
22
<div class='card-header'>
3-
{{pageTitle + ': ' + product?.productName}}
3+
{{pageTitle + ': ' + product?.productName}}
44
</div>
5+
6+
<div class='card-body'
7+
*ngIf='product'>
8+
<div class='row'>
9+
<div class='col-md-8'>
10+
<div class='row'>
11+
<div class='col-md-4'>Name:</div>
12+
<div class='col-md-8'>{{product.productName}}</div>
13+
</div>
14+
<div class='row'>
15+
<div class='col-md-4'>Code:</div>
16+
<div class='col-md-8'>{{product.productCode | lowercase | convertToSpaces: '-'}}</div>
17+
</div>
18+
<div class='row'>
19+
<div class='col-md-4'>Description:</div>
20+
<div class='col-md-8'>{{product.description}}</div>
21+
</div>
22+
<div class='row'>
23+
<div class='col-md-4'>Availability:</div>
24+
<div class='col-md-8'>{{product.releaseDate}}</div>
25+
</div>
26+
<div class='row'>
27+
<div class='col-md-4'>Price:</div>
28+
<div class='col-md-8'>{{product.price|currency:'USD':'symbol'}}</div>
29+
</div>
30+
<div class='row'>
31+
<div class='col-md-4'>5 Star Rating:</div>
32+
<div class='col-md-8'>
33+
<pm-star [rating]='product.starRating'>
34+
</pm-star>
35+
</div>
36+
</div>
37+
</div>
38+
39+
<div class='col-md-4'>
40+
<img class='center-block img-responsive'
41+
[style.width.px]='200'
42+
[style.margin.px]='2'
43+
[src]='product.imageUrl'
44+
[title]='product.productName'>
45+
</div>
46+
</div>
47+
</div>
48+
549
<div class='card-footer'>
6-
<button class='btn btn-outlline-secondary'
7-
style='width:80px'
8-
(click)='onBack()'>
9-
<i class='fa fa-chevron-left'></i>Back
10-
</button>
50+
<button class='btn btn-outline-secondary'
51+
style='width:80px'
52+
(click)='onBack()'>
53+
<i class='fa fa-chevron-left'></i> Back
54+
</button>
1155
</div>
12-
</div>
56+
</div>

APM-Start/src/app/products/product-detail.component.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Component, OnInit } from '@angular/core';
22
import { ActivatedRoute, Router } from '@angular/router';
33
import { IProduct } from './product';
4+
import { ProductService } from './product.service';
45

56
@Component({
67
selector: 'pm-product-detail',
@@ -9,25 +10,25 @@ import { IProduct } from './product';
910
})
1011
export class ProductDetailComponent implements OnInit {
1112
product: IProduct | undefined;
12-
constructor(private route: ActivatedRoute, private router: Router) {}
13+
14+
constructor(private route: ActivatedRoute, private router: Router, private productService: ProductService) {}
1315

1416
pageTitle: string = 'Product Details';
1517

1618
ngOnInit(): void {
1719
const id = Number(this.route.snapshot.paramMap.get('id'));
1820
this.pageTitle += `: ${id}`;
1921

20-
// TODO remove hard coded product:
21-
this.product = {
22-
'productId': id,
23-
'productName': 'Blah',
24-
'productCode': '010202',
25-
'releaseDate': 'March 19, 2021',
26-
'description': 'Leaf Rake',
27-
'price': 19.95,
28-
'starRating': 3.2,
29-
'imageUrl': 'assets/images/leaf_rake.png'
30-
};
22+
if(id){
23+
this.getProduct(id);
24+
}
25+
}
26+
27+
getProduct(id: number): void {
28+
this.productService.getProduct(id).subscribe({
29+
next: product => this.product = product
30+
//error: error => this.errorMessage = error;
31+
});
3132
}
3233

3334
onBack(): void {

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { HttpClient, HttpErrorResponse } from "@angular/common/http";
22
import { Injectable } from "@angular/core";
3-
import { catchError, Observable, tap, throwError } from "rxjs";
3+
import { catchError, map, Observable, tap, throwError } from "rxjs";
44
import { IProduct } from "./product";
55

66
@Injectable({
@@ -19,6 +19,13 @@ export class ProductService {
1919
);
2020
}
2121

22+
getProduct(id: number): Observable<IProduct | undefined> {
23+
return this.getProducts()
24+
.pipe(
25+
map((products:IProduct[]) => products.find(x => x.productId === id))
26+
);
27+
}
28+
2229
private handleError(err: HttpErrorResponse) {
2330
let errorMessage = '';
2431
if (err.error instanceof ErrorEvent) {

0 commit comments

Comments
 (0)