Polish marketplace search states

This commit is contained in:
sdarbinyan
2026-07-05 01:10:56 +04:00
parent 3974eefbfe
commit 3f5aa2af2a
6 changed files with 43 additions and 6 deletions

View File

@@ -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')