Improve searching

This commit is contained in:
aditya.chandel
2024-12-13 16:05:04 -07:00
parent 1e9249b257
commit 033fc95373
5 changed files with 26 additions and 8 deletions
@@ -50,7 +50,7 @@ public class BookController {
@GetMapping("/{bookId}/cover")
public ResponseEntity<Resource> getBookCover(@PathVariable long bookId) {
Random random = new Random();
int delay = 250 + random.nextInt(750);
int delay = 50 + random.nextInt(250);
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
@@ -12,10 +12,19 @@
<button *ngIf="searchQuery" pButton icon="pi pi-times" (click)="clearSearch()" class="clear-btn" type="button" aria-label="Clear Search"></button>
</span>
<div class="search-dropdown layout-menu" [class.show]="books.length > 0">
<div class="search-dropdown-item" *ngFor="let book of books" (click)="onBookClick(book)">
<img [src]="getBookCoverUrl(book.id)" alt="Book Cover" class="search-book-cover" />
<span class="search-book-name">{{ book.title }}</span>
</div>
<div class="search-dropdown layout-menu" [class.show]="(books.length > 0 || (searchQuery?.length ?? 0) > 0)">
<ng-container *ngIf="books.length > 0; else noResults">
<div class="search-dropdown-item" *ngFor="let book of books" (click)="onBookClick(book)">
<img [src]="getBookCoverUrl(book.id)" alt="Book Cover" class="search-book-cover" />
<span class="search-book-name">{{ book.title }}</span>
</div>
</ng-container>
<!-- No results message -->
<ng-template #noResults>
<div class="search-dropdown-item">
<span>No results found</span>
</div>
</ng-template>
</div>
</div>
@@ -75,3 +75,12 @@
cursor: pointer;
font-size: 12px;
}
.search-dropdown-item.no-results {
font-size: 14px;
color: var(--text-color-secondary);
padding: 10px;
text-align: center;
font-style: italic;
}
@@ -34,7 +34,7 @@ export class SearchComponent implements OnInit, OnDestroy {
initializeSearch(): void {
this.#subscription = this.#searchSubject.pipe(
debounceTime(500),
debounceTime(350),
distinctUntilChanged(),
switchMap((query) => this.bookService.searchBooks(query).pipe(
catchError((error) => {
@@ -31,7 +31,7 @@ export class BookService {
}
searchBooks(query: string): Observable<Book[]> {
if (query.length < 3) {
if (query.length < 2) {
return new Observable<Book[]>();
}
return this.http.get<Book[]>(`${this.bookUrl}/search?title=${encodeURIComponent(query)}`);