Skip to content

Commit 1d0e835

Browse files
committed
bs-filter-list added to product-index : done
1 parent cfc4b5c commit 1d0e835

File tree

6 files changed

+71
-19
lines changed

6 files changed

+71
-19
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,14 @@ export interface IProduct {
55
categories?: string[];
66
category: string;
77
id?: string;
8+
}
9+
10+
export interface ICategory {
11+
categoryName: string;
12+
isSelected: boolean;
13+
}
14+
15+
export interface IListGroup {
16+
list: any[];
17+
key: string;
818
}

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
<div class="row mt-5">
2-
<div class="col-2">Lorem ipsum dolor sit amet consectetur adipisicing elit. Deleniti quae illo, ipsa asperiores iure a! Et molestiae, atque cum at corrupti esse, perferendis tempore eaque officia qui laborum libero quisquam!</div>
3-
<div class="col-10">
2+
<div class="col-md-6 col-xl-2">
3+
<bootstrap-list [props]='listGroup'></bootstrap-list>
4+
</div>
5+
<div class="col-md-6 col-xl-10">
46
<div class="card-group">
5-
<bootstrap-card *ngFor="let item of products">
6-
<img class="selector-header card-img-top" src={{item.imageUrl}} alt="">
7+
<bootstrap-card *ngFor="let product of products">
8+
<img class="selector-header card-img-top" src={{product.imageUrl}} alt="">
79
<ng-container class="selector-body">
810
<div class="card-body">
911
<h5 class="card-title">
10-
{{item.title}}
12+
{{product.title}}
1113
</h5>
1214
<p class="card-text">
13-
${{item.price}}
15+
${{product.price}}
1416
</p>
1517
<a href="#" class="btn btn-primary">Go somewhere</a>
1618
</div>

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Component, OnInit, OnDestroy } from '@angular/core';
22
import { DataService } from '../../../services/data.service';
3-
import { IProduct } from '../interfaces';
3+
import { IProduct, ICategory, IListGroup } from '../interfaces';
44
import { Subscription } from 'rxjs';
55

66
@Component({
@@ -10,20 +10,34 @@ import { Subscription } from 'rxjs';
1010
})
1111
export class ProductsIndexComponent implements OnInit, OnDestroy {
1212

13-
products: IProduct[] = [];
1413
subscription: Subscription;
14+
listGroup: IListGroup;
15+
products: IProduct[] = [];
16+
categories: ICategory[] = [];
17+
1518

1619
constructor(private service: DataService) { }
1720

1821
getCollection() {
1922
this.subscription = this.service.getAll('vegetables')
2023
.subscribe((response: any) => {
21-
console.log(response);
22-
2324
this.products = response;
25+
this.extractCategories(this.products);
2426
});
2527
}
2628

29+
extractCategories(data: IProduct[]) {
30+
const { categories } = data[0];
31+
const remap: ICategory[] = categories.map((item: string) => {
32+
return {
33+
categoryName: item,
34+
isSelected: false
35+
}
36+
});
37+
this.listGroup = { list: remap, key: 'categoryName'};
38+
}
39+
40+
2741
ngOnInit(): void {
2842
this.getCollection();
2943
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
<p>bootstrap-list works!</p>
1+
<ul class="list-group">
2+
<li *ngFor="let item of list"
3+
(click)="toggleActiveClass(item)"
4+
class="list-group-item"
5+
[ngClass]="{'active': item.isSelected}">
6+
{{ item[propertyKey] }}
7+
</li>
8+
</ul>
Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import { Component, Input, OnChanges, SimpleChanges } from '@angular/core';
22

33
@Component({
4-
selector: 'app-bootstrap-list',
5-
templateUrl: './bootstrap-list.component.html',
6-
styleUrls: ['./bootstrap-list.component.scss']
4+
selector: 'bootstrap-list',
5+
templateUrl: './bootstrap-list.component.html',
6+
styleUrls: ['./bootstrap-list.component.scss']
77
})
8-
export class BootstrapListComponent implements OnInit {
8+
export class BootstrapListComponent implements OnChanges {
9+
@Input() props: any;
910

10-
constructor() { }
11+
list: any[] = [];
12+
propertyKey: string;
1113

12-
ngOnInit(): void {
13-
}
14+
constructor() { }
1415

16+
toggleActiveClass(current) {
17+
this.list
18+
.filter(el => el != current)
19+
.forEach(item => item.isSelected = false);
20+
current.isSelected = !current.isSelected;
21+
}
22+
23+
ngOnChanges(change: SimpleChanges): void {
24+
const { currentValue, firstChange } = change.props;
25+
if (currentValue && !firstChange) {
26+
const { list, key } = currentValue;
27+
this.list = list;
28+
this.propertyKey = key;
29+
}
30+
}
1531
}
32+
33+

section_19-project/project/src/app/services/data.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export class DataService {
1010
constructor(private db: AngularFirestore) { }
1111

1212
getAll(collectioName: string) {
13+
1314
const response: AngularFirestoreCollection<ID> = this.db.collection(collectioName);
1415

1516
return response.snapshotChanges()

0 commit comments

Comments
 (0)