-
Notifications
You must be signed in to change notification settings - Fork 0
#T13 add filter by rating #15
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { ChangeDetectionStrategy, Component, ViewEncapsulation } from '@angular/core'; | ||
import { ActivatedRoute, Router } from '@angular/router'; | ||
import { FormControl, FormGroup } from '@angular/forms'; | ||
import { AbstractControl, FormArray, FormControl, FormGroup } from '@angular/forms'; | ||
import { filter, map, startWith, switchMap, tap } from 'rxjs/operators'; | ||
import { CategoryModel } from '@app/models/category.model'; | ||
import { ProductsService } from '@app/services/products.service'; | ||
|
@@ -85,16 +85,17 @@ export class CategoryProductsComponent { | |
|
||
|
||
// SEARCH BY PRICE | ||
readonly formSearchByPrice: FormGroup = new FormGroup({ | ||
readonly searchForm: FormGroup = new FormGroup({ | ||
priceFrom: new FormControl(), | ||
priceTo: new FormControl(), | ||
ratingsArray: new FormArray([]), | ||
}); | ||
|
||
readonly productsByCategory$: Observable<ProductModel[]> = combineLatest([ | ||
this._productsService.getAll(), | ||
this.category, | ||
this.sortBy$, | ||
this.formSearchByPrice.valueChanges.pipe(startWith({ priceFrom: -1, priceTo: 9999999 })), | ||
this.searchForm.valueChanges.pipe(startWith({ priceFrom: -1 })), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. czemu właściwie muszę podać cokolwiek w startWith? jesli usune tez i priceFrom to przestaje mi sie wyswietlac lista to dlatego że w 94 linijce w category-products.components.ts mam combineLatest i musi się wygenerować jakakolwiek wartość dla searchForm, żeby dostarczyło wszystkie elementy do combineLatest i przeszło dalej? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dokładnie tak, bez tego dane załadowałyby się dopiero po zmianach w formularzu np. dodaniu priceFrom |
||
]).pipe( | ||
map( | ||
([products, category, sortBy, searchValues]: [ | ||
|
@@ -104,13 +105,19 @@ export class CategoryProductsComponent { | |
SearchParamsQueryModel | ||
]) => { | ||
|
||
console.log(searchValues) | ||
|
||
const filteredProducts = products.filter( | ||
(product) => product.categoryId === category.id | ||
).filter( | ||
(product) => | ||
(!searchValues.priceFrom || product.price >= searchValues.priceFrom) | ||
&& | ||
(!searchValues.priceTo || product.price <= searchValues.priceTo) | ||
&& | ||
(!searchValues.ratingsArray || | ||
searchValues.ratingsArray.length === 0 || | ||
searchValues.ratingsArray.includes(Math.floor(product.ratingValue).toString())) | ||
); | ||
|
||
|
||
|
@@ -191,7 +198,6 @@ export class CategoryProductsComponent { | |
) | ||
.pipe( | ||
tap(([pageNumberOptions, form]) => { | ||
|
||
return this._router.navigate([], { | ||
queryParams: { | ||
pageSize: form.pageSize, | ||
|
@@ -202,5 +208,29 @@ export class CategoryProductsComponent { | |
) | ||
.subscribe(); | ||
|
||
onCheckChange(event:Event) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. czy jest może jakiś lepszy typ niż Event, tak żebym nie musiała póżniej zapisywać eventTarget jako HTMLInputElement? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. w zwykłym bootstrapowym checkbox chyba nie, tutaj ewentualnie dać typ 'any' zamiast Event, ale Twoje rozwiązanie na plus W angular material jest event specjalny z którym jest dużo łatwiej |
||
const formArray: FormArray = this.searchForm.get('ratingsArray') as FormArray; | ||
const eventTarget = event.target as HTMLInputElement | ||
|
||
/* Selected */ | ||
if(eventTarget.checked){ | ||
// Add a new control in the arrayForm | ||
formArray.push(new FormControl(eventTarget.value)); | ||
} | ||
/* unselected */ | ||
else{ | ||
// find the unselected element | ||
let i: number = 0; | ||
|
||
formArray.controls.forEach((ctrl: AbstractControl) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tutaj w przykładzie było żeby ctrl oznaczyc jako FormControl, ale nie działało i musiałam użyć AbstractControl There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Przykład który podałaś jest sprzed 6 lat, to dużo jak na programowanie xd możesz nie robić tego na forEach tylko znaleźć index za pomocą metody findIndex na formArray.controls i po tym indeksie usunąć element. Zmienna 'let i' jest tutaj niepotrzebna, metoda forEach jako drugi parametr ma index elementu np. Nie jestem pewien, ale FormControl rozszerza AbstractControl, i tym może to być spowodowane co do typu. |
||
if(ctrl.value == eventTarget.value) { | ||
// Remove the unselected element from the arrayForm | ||
formArray.removeAt(i); | ||
return; | ||
} | ||
|
||
i++; | ||
}); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export interface SearchParamsQueryModel { | ||
readonly priceFrom: number; | ||
readonly priceTo: number; | ||
readonly ratingsArray: string[]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
czy jest może jakiś ładniejszy sposób na to żeby wygenerować te gwiazdki?
wiem, że moglabym wrzucic tą 5 do zmiennej i byloby troche ladniej, ale chodzi mi o cos jeszcze bardziej poprawnego
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tak, można w ts zrobić np. tablicę obiektów, gdzie obiekt by miał dane potrzebne do prezentacji gwiazdki. Ciekawy sposób masz ale raczej tak nie robimy