Polish marketplace search states
This commit is contained in:
@@ -21,6 +21,7 @@ import { ProductCardComponent } from '../../components/product-card/product-card
|
||||
changeDetection: ChangeDetectionStrategy.OnPush
|
||||
})
|
||||
export class SearchComponent implements OnDestroy {
|
||||
readonly minSearchLength = 3;
|
||||
searchQuery = '';
|
||||
items = signal<Item[]>([]);
|
||||
loading = signal(false);
|
||||
@@ -60,17 +61,23 @@ export class SearchComponent implements OnDestroy {
|
||||
|
||||
onSearchInput(query: string): void {
|
||||
this.searchQuery = query;
|
||||
this.error.set(null);
|
||||
|
||||
if (this.isQueryTooShort()) {
|
||||
this.resetSearchState();
|
||||
return;
|
||||
}
|
||||
|
||||
this.searchSubject.next(query);
|
||||
}
|
||||
|
||||
performSearch(query: string): void {
|
||||
if (!query.trim()) {
|
||||
this.items.set([]);
|
||||
this.hasMore.set(false);
|
||||
this.totalResults.set(0);
|
||||
this.resetSearchState();
|
||||
return;
|
||||
}
|
||||
|
||||
this.error.set(null);
|
||||
this.items.set([]);
|
||||
this.skip = 0;
|
||||
this.hasMore.set(true);
|
||||
@@ -84,7 +91,7 @@ export class SearchComponent implements OnDestroy {
|
||||
this.loading.set(true);
|
||||
this.isLoadingMore = true;
|
||||
|
||||
this.productFacade.searchProducts({ search: this.searchQuery, count: this.count, skip: this.skip }).subscribe({
|
||||
this.productFacade.searchProducts({ search: this.searchQuery.trim(), count: this.count, skip: this.skip }).subscribe({
|
||||
next: (response) => {
|
||||
// Update total results (only on first load)
|
||||
if (this.skip === 0) {
|
||||
@@ -114,6 +121,20 @@ export class SearchComponent implements OnDestroy {
|
||||
});
|
||||
}
|
||||
|
||||
private resetSearchState(): void {
|
||||
this.items.set([]);
|
||||
this.loading.set(false);
|
||||
this.hasMore.set(false);
|
||||
this.totalResults.set(0);
|
||||
this.skip = 0;
|
||||
this.isLoadingMore = false;
|
||||
}
|
||||
|
||||
isQueryTooShort(): boolean {
|
||||
const length = this.searchQuery.trim().length;
|
||||
return length > 0 && length < this.minSearchLength;
|
||||
}
|
||||
|
||||
private scrollTimeout?: ReturnType<typeof setTimeout>;
|
||||
|
||||
@HostListener('window:scroll')
|
||||
|
||||
Reference in New Issue
Block a user