From 3705ceb95b6a5516f76698b1c12ab35c373cdcc8 Mon Sep 17 00:00:00 2001 From: tanshu Date: Fri, 26 Mar 2021 08:01:30 +0530 Subject: [PATCH] Search Added to Product List. --- .../product-list/product-list-datasource.ts | 44 ++++++++++++++----- .../product-list/product-list.component.html | 15 ++++++- .../product-list/product-list.component.ts | 33 +++++++++++--- 3 files changed, 72 insertions(+), 20 deletions(-) diff --git a/bookie/src/app/product/product-list/product-list-datasource.ts b/bookie/src/app/product/product-list/product-list-datasource.ts index cfa142a..a4e1f1b 100644 --- a/bookie/src/app/product/product-list/product-list-datasource.ts +++ b/bookie/src/app/product/product-list/product-list-datasource.ts @@ -8,16 +8,19 @@ import { Product } from '../../core/product'; export class ProductListDataSource extends DataSource { public data: Product[]; public filteredData: Product[]; - public filterValue: string; + public search: string; + public menuCategory: string; constructor( - private readonly filter: Observable, + private readonly searchFilter: Observable, + private readonly menuCategoryFilter: Observable, private readonly dataObs: Observable, ) { super(); this.data = []; this.filteredData = []; - this.filterValue = ''; + this.search = ''; + this.menuCategory = ''; } connect(): Observable { @@ -27,14 +30,19 @@ export class ProductListDataSource extends DataSource { this.data = x; }), ), - this.filter.pipe( + this.searchFilter.pipe( tap((x) => { - this.filterValue = x; + this.search = x; + }), + ), + this.menuCategoryFilter.pipe( + tap((x) => { + this.menuCategory = x; }), ), ]; return merge(...dataMutations).pipe( - map(() => this.getFilteredData(this.data, this.filterValue)), + map(() => this.getFilteredData(this.data, this.search, this.menuCategory)), tap((x) => { this.filteredData = x; }), @@ -43,11 +51,23 @@ export class ProductListDataSource extends DataSource { disconnect() {} - // eslint-disable-next-line class-methods-use-this - private getFilteredData(data: Product[], filter: string): Product[] { - if (filter === null || filter === undefined || filter === '') { - return data; - } - return data.filter((x) => (x.menuCategory as MenuCategory).id === filter); + private getFilteredData(data: Product[], search: string, menuCategory: string): Product[] { + return data + .filter( + (x: Product) => + search === null || + search === undefined || + search === '' || + `${x.name} ${x.units} ${x.saleCategory?.name} ${x.menuCategory?.name}` + .toLowerCase() + .indexOf(search.toLowerCase()) !== -1, + ) + .filter( + (x) => + menuCategory === null || + menuCategory === undefined || + menuCategory === '' || + (x.menuCategory as MenuCategory).id === menuCategory, + ); } } diff --git a/bookie/src/app/product/product-list/product-list.component.html b/bookie/src/app/product/product-list/product-list.component.html index 2d3ce4d..f07ea19 100644 --- a/bookie/src/app/product/product-list/product-list.component.html +++ b/bookie/src/app/product/product-list/product-list.component.html @@ -1,7 +1,11 @@ Products - @@ -22,6 +26,15 @@ fxLayoutGap="20px" fxLayoutGap.lt-md="0px" > + + + Product Type = new BehaviorSubject(''); + searchFilter: Observable = new Observable(); + menuCategoryFilter: BehaviorSubject = new BehaviorSubject(''); data: BehaviorSubject = new BehaviorSubject([]); - dataSource: ProductListDataSource = new ProductListDataSource(this.filter, this.data); + dataSource: ProductListDataSource = new ProductListDataSource( + this.searchFilter, + this.menuCategoryFilter, + this.data, + ); + form: FormGroup; list: Product[] = []; menuCategories: MenuCategory[] = []; @@ -43,18 +52,28 @@ export class ProductListComponent implements OnInit { ) { this.form = this.fb.group({ menuCategory: '', + filter: '', }); this.data.subscribe((data: Product[]) => { this.list = data; }); + this.searchFilter = (this.form.get('filter') as FormControl).valueChanges.pipe( + startWith(''), + debounceTime(150), + distinctUntilChanged(), + ); } filterOn(val: string) { - this.filter.next(val); + this.menuCategoryFilter.next(val); } ngOnInit() { - this.dataSource = new ProductListDataSource(this.filter, this.data); + this.dataSource = new ProductListDataSource( + this.searchFilter, + this.menuCategoryFilter, + this.data, + ); this.route.data.subscribe((value) => { const data = value as { list: Product[]; menuCategories: MenuCategory[] }; this.loadData(data.list, data.menuCategories); @@ -81,11 +100,11 @@ export class ProductListComponent implements OnInit { dropTable(event: CdkDragDrop) { const prevIndex = this.dataSource.filteredData.indexOf(event.item.data); moveItemInArray(this.dataSource.filteredData, prevIndex, event.currentIndex); - if (this.dataSource.filterValue === undefined) { + if (this.dataSource.menuCategory === undefined) { this.list = this.dataSource.filteredData; } else { this.list = this.list - .filter((x) => (x.menuCategory as MenuCategory).id !== this.dataSource.filterValue) + .filter((x) => (x.menuCategory as MenuCategory).id !== this.dataSource.menuCategory) .concat(this.dataSource.filteredData); } this.data.next(this.list);