|
1 | 1 | import { Component } from '@angular/core'; |
2 | 2 | import { SocksService } from './socks.service'; |
3 | | -import { Observable } from 'rxjs'; |
| 3 | +import { Observable,BehaviorSubject, combineLatest } from 'rxjs'; |
4 | 4 | import { Sock } from './sock.model'; |
5 | 5 | import { AsyncPipe, NgFor } from '@angular/common'; |
| 6 | +import { NgxPaginationModule } from 'ngx-pagination'; |
| 7 | +import { map } from 'rxjs/operators'; |
| 8 | +import { PricePipe } from '../price.pipe'; |
6 | 9 |
|
7 | 10 | @Component({ |
8 | 11 | selector: 'app-shop', |
9 | 12 | standalone: true, |
10 | | - imports: [NgFor, AsyncPipe], |
| 13 | + imports: [NgFor, AsyncPipe, NgxPaginationModule, PricePipe], |
11 | 14 | templateUrl: './shop.component.html' |
12 | 15 | }) |
13 | 16 | export class ShopComponent { |
14 | 17 | socks$!: Observable<Sock[]>; |
| 18 | + filteredSocks$!: Observable<Sock[]>; |
| 19 | + |
| 20 | + public nameFilterSubject = new BehaviorSubject<string>(''); |
| 21 | + public colorFilterSubject = new BehaviorSubject<string>(''); |
| 22 | + public sortBySubject = new BehaviorSubject<string>('name'); |
| 23 | + public currentPageSubject = new BehaviorSubject<number>(1); |
| 24 | + public pageSize = 10; |
15 | 25 |
|
16 | 26 | constructor(private socksService: SocksService) {} |
17 | 27 |
|
18 | 28 | ngOnInit(): void { |
19 | 29 | this.socks$ = this.socksService.get(); |
| 30 | + |
| 31 | + this.filteredSocks$ = combineLatest([ |
| 32 | + this.socks$, |
| 33 | + this.nameFilterSubject.asObservable(), |
| 34 | + this.colorFilterSubject.asObservable(), |
| 35 | + this.sortBySubject.asObservable(), |
| 36 | + this.currentPageSubject.asObservable() |
| 37 | + ]).pipe( |
| 38 | + map(([socks, nameFilter, colorFilter, sortBy, currentPage]) => { |
| 39 | + let filtered = socks.filter(sock => |
| 40 | + sock.name.toLowerCase().includes(nameFilter.toLowerCase()) && |
| 41 | + sock.variant.toLowerCase().includes(colorFilter.toLowerCase()) |
| 42 | + ); |
| 43 | + |
| 44 | + filtered = filtered.sort((a, b) => { |
| 45 | + if (sortBy === 'name') { |
| 46 | + return a.name.localeCompare(b.name); |
| 47 | + } else if (sortBy === 'price') { |
| 48 | + return a.price - b.price; |
| 49 | + } |
| 50 | + return 0; |
| 51 | + }); |
| 52 | + |
| 53 | + const startIndex = (currentPage - 1) * this.pageSize; |
| 54 | + return filtered.slice(startIndex, startIndex + this.pageSize); |
| 55 | + }) |
| 56 | + ); |
20 | 57 | } |
| 58 | + |
| 59 | + onFilterName(event: Event) { |
| 60 | + const name = (event.target as HTMLInputElement).value; |
| 61 | + this.nameFilterSubject.next(name); |
| 62 | + } |
| 63 | + |
| 64 | + onFilterColor(event: Event) { |
| 65 | + const color = (event.target as HTMLInputElement).value; |
| 66 | + this.colorFilterSubject.next(color); |
| 67 | + } |
| 68 | + |
| 69 | + onSortBy(event: Event) { |
| 70 | + const sortBy = (event.target as HTMLInputElement).value; |
| 71 | + this.sortBySubject.next(sortBy); |
| 72 | + } |
| 73 | + |
| 74 | + onPageChange(page: number) { |
| 75 | + this.currentPageSubject.next(page); |
| 76 | + } |
| 77 | + |
21 | 78 | } |
0 commit comments