Skip to content

Commit c1c4ed0

Browse files
committed
/product-index: counters and ngtemplate added
1 parent d678749 commit c1c4ed0

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

section_19-project/project/src/app/components/products/interfaces.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ export interface IProduct {
44
price: number;
55
categories?: string[];
66
category: string;
7-
id?: string;
7+
id: string;
8+
count: number;
9+
isOpen: boolean;
810
}
911

1012
export interface ICategory {

section_19-project/project/src/app/components/products/products-index/products-index.component.html

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<div class="col-md-6 col-xl-10">
66
<div class="card-group">
77
<bootstrap-card *ngFor="let product of products">
8-
<img class="selector-header card-img-top" src={{product.imageUrl}} alt="">
8+
<img class="selector-header card-img-top" src={{product.imageUrl}} alt="{{product.title}}">
99
<ng-container class="selector-body">
1010
<div class="card-body">
1111
<h5 class="card-title">
@@ -14,7 +14,26 @@ <h5 class="card-title">
1414
<p class="card-text">
1515
${{product.price}}
1616
</p>
17-
<a href="#" class="btn btn-primary">Go somewhere</a>
17+
<ng-container *ngIf="!product.isOpen
18+
then button
19+
else counter">
20+
</ng-container>
21+
22+
<ng-template #counter>
23+
<div class="input-group mb-3">
24+
<div class="input-group-prepend">
25+
<button (click)="removeItem(product)" class="btn btn-secondary" type="button" id="button-addon1">-</button>
26+
</div>
27+
<input type="text" class="form-control text-center" [placeholder]="product.count + ' in cart'" readonly>
28+
<div class="input-group-append">
29+
<button (click)="addItem(product)" class="btn btn-secondary" type="button" id="button-addon2">+</button>
30+
</div>
31+
</div>
32+
</ng-template>
33+
34+
<ng-template #button>
35+
<button (click)="counterShow(product)" class="btn btn-secondary btn-block">ADD TO CART</button>
36+
</ng-template>
1837
</div>
1938
</ng-container>
2039
</bootstrap-card>

section_19-project/project/src/app/components/products/products-index/products-index.component.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,20 @@ export class ProductsIndexComponent implements OnInit, OnDestroy {
1515
products: IProduct[] = [];
1616
categories: ICategory[] = [];
1717

18-
1918
constructor(private service: DataService) { }
2019

2120
getCollection() {
2221
this.subscription = this.service.getAll('vegetables')
2322
.subscribe((response: any) => {
24-
this.products = response;
23+
24+
const addCounter = response.map((item) => {
25+
return {
26+
...item,
27+
count: 1,
28+
isOpen:false
29+
}
30+
});
31+
this.products = addCounter;
2532
this.extractCategories(this.products);
2633
});
2734
}
@@ -35,9 +42,32 @@ export class ProductsIndexComponent implements OnInit, OnDestroy {
3542
cssClass: 'organic'
3643
}
3744
});
38-
this.listGroup = { list: remap, key: 'categoryName'};
45+
this.listGroup = { list: remap, key: 'categoryName' };
3946
}
4047

48+
counterShow(current: IProduct) {
49+
current.isOpen = true;
50+
}
51+
52+
53+
addItem(current: IProduct) {
54+
const index = this.products.indexOf(current);
55+
current.count = current.count + 1;
56+
this.products[index] = current;
57+
}
58+
59+
removeItem(current: IProduct) {
60+
if (current.count >= 1) {
61+
const index = this.products.indexOf(current);
62+
current.count = current.count - 1;
63+
this.products[index] = current;
64+
}
65+
if (current.count === 0) {
66+
// hide counter
67+
current.isOpen = false;
68+
}
69+
70+
}
4171

4272
ngOnInit(): void {
4373
this.getCollection();

0 commit comments

Comments
 (0)