Skip to content

hw7 #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: lecture-7
Choose a base branch
from
Open

hw7 #121

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions src/app/pages/products-list/products-list.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,10 @@
let selectIndex = selectIndex
"
> -->

<!-- hard -->
<!-- <ng-container
*appPagination="
let productsGroup of products | filterByProperty : propertyName : searchPropertyValue;
chankSize: 6;
let pageIndexes = pageIndexes;
let activeIndex = index;
let next = next;
let back = back;
let selectIndex = selectIndex
"
> -->

<ng-container
*appPagination="
let productsGroup of products;
let productsGroup of products | filterByProperty : propertyName : searchPropertyValue;
chankSize: 6;
let pageIndexes = pageIndexes;
let activeIndex = index;
Expand All @@ -36,6 +23,17 @@
let selectIndex = selectIndex
"
>
<!-- <ng-container
*appPagination="
let productsGroup of products;
chankSize: 6;
let pageIndexes = pageIndexes;
let activeIndex = index;
let next = next;
let back = back;
let selectIndex = selectIndex
"
> -->
<div class="cards-container">
<app-card
*ngFor="let product of productsGroup; trackBy: trackBy"
Expand Down Expand Up @@ -64,7 +62,6 @@
</div>
</ng-container>
</ng-container>

<ng-template #loader>
<mat-spinner class="loader"></mat-spinner>
</ng-template>
4 changes: 2 additions & 2 deletions src/app/pages/products-list/products-list.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export class ProductsListComponent implements OnInit {

name = 'Мышь';

readonly propertyName = 'feedbacksCount' as const; // keyof IProduct
searchPropertyValue = 2;
readonly propertyName = 'name' as const; // keyof IProduct
searchPropertyValue = 'а';

constructor(
// @Inject(ChangeDetectorRef) private readonly changeDetectorRef: ChangeDetectorRef,
Expand Down
2 changes: 2 additions & 0 deletions src/app/pages/products-list/products-list.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {CommonModule} from '@angular/common';
import {MatProgressSpinnerModule} from '@angular/material/progress-spinner';
import {MatIconModule} from '@angular/material/icon';
import {MatButtonModule} from '@angular/material/button';
import {FilterByPropertyModule} from 'src/app/shared/filter-by-property/filter-by-property.module';
import {ProductsListComponent} from './products-list.component';
import {CardModule} from './card/card.module';
import {DumpNgIfModule} from '../../shared/dump-ng-if/dump-ng-if.module';
Expand All @@ -20,6 +21,7 @@ import {MyAsyncModule} from '../../shared/my-async/my-async.module';
MatIconModule,
MatButtonModule,
MyAsyncModule,
FilterByPropertyModule,
],
// providers: [
// ...CardModule.providers
Expand Down
10 changes: 10 additions & 0 deletions src/app/shared/filter-by-property/filter-by-property.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {FilterByPropertyPipe} from './filter-by-property.pipe';

@NgModule({
imports: [CommonModule],
declarations: [FilterByPropertyPipe],
exports: [FilterByPropertyPipe],
})
export class FilterByPropertyModule {}
27 changes: 27 additions & 0 deletions src/app/shared/filter-by-property/filter-by-property.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {Pipe, PipeTransform} from '@angular/core';
import {isString} from './is-string';

@Pipe({
name: 'filterByProperty',
})
export class FilterByPropertyPipe implements PipeTransform {
transform<T, P extends keyof T>(
items: T[] | undefined | null,
searchingProperty: P,
searchValue: T[P],
): T[] | undefined | null {
if (!items?.length) {
return items;
}

if (isString(searchValue)) {
const searchValueUpperCase = searchValue.toUpperCase();

return items.filter(item =>
(item[searchingProperty] as string).toUpperCase().includes(searchValueUpperCase),
);
}

return items.filter(item => item[searchingProperty] === searchValue);
}
}
3 changes: 3 additions & 0 deletions src/app/shared/filter-by-property/is-string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isString(item: any): item is string {
return typeof item === 'string';
}