Improve searching
This commit is contained in:
+1
-1
@@ -50,7 +50,7 @@ public class BookController {
|
|||||||
@GetMapping("/{bookId}/cover")
|
@GetMapping("/{bookId}/cover")
|
||||||
public ResponseEntity<Resource> getBookCover(@PathVariable long bookId) {
|
public ResponseEntity<Resource> getBookCover(@PathVariable long bookId) {
|
||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
int delay = 250 + random.nextInt(750);
|
int delay = 50 + random.nextInt(250);
|
||||||
try {
|
try {
|
||||||
Thread.sleep(delay);
|
Thread.sleep(delay);
|
||||||
} catch (InterruptedException e) {
|
} 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>
|
<button *ngIf="searchQuery" pButton icon="pi pi-times" (click)="clearSearch()" class="clear-btn" type="button" aria-label="Clear Search"></button>
|
||||||
</span>
|
</span>
|
||||||
|
|
||||||
<div class="search-dropdown layout-menu" [class.show]="books.length > 0">
|
<div class="search-dropdown layout-menu" [class.show]="(books.length > 0 || (searchQuery?.length ?? 0) > 0)">
|
||||||
<div class="search-dropdown-item" *ngFor="let book of books" (click)="onBookClick(book)">
|
<ng-container *ngIf="books.length > 0; else noResults">
|
||||||
<img [src]="getBookCoverUrl(book.id)" alt="Book Cover" class="search-book-cover" />
|
<div class="search-dropdown-item" *ngFor="let book of books" (click)="onBookClick(book)">
|
||||||
<span class="search-book-name">{{ book.title }}</span>
|
<img [src]="getBookCoverUrl(book.id)" alt="Book Cover" class="search-book-cover" />
|
||||||
</div>
|
<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>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -75,3 +75,12 @@
|
|||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
font-size: 12px;
|
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 {
|
initializeSearch(): void {
|
||||||
this.#subscription = this.#searchSubject.pipe(
|
this.#subscription = this.#searchSubject.pipe(
|
||||||
debounceTime(500),
|
debounceTime(350),
|
||||||
distinctUntilChanged(),
|
distinctUntilChanged(),
|
||||||
switchMap((query) => this.bookService.searchBooks(query).pipe(
|
switchMap((query) => this.bookService.searchBooks(query).pipe(
|
||||||
catchError((error) => {
|
catchError((error) => {
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ export class BookService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
searchBooks(query: string): Observable<Book[]> {
|
searchBooks(query: string): Observable<Book[]> {
|
||||||
if (query.length < 3) {
|
if (query.length < 2) {
|
||||||
return new Observable<Book[]>();
|
return new Observable<Book[]>();
|
||||||
}
|
}
|
||||||
return this.http.get<Book[]>(`${this.bookUrl}/search?title=${encodeURIComponent(query)}`);
|
return this.http.get<Book[]>(`${this.bookUrl}/search?title=${encodeURIComponent(query)}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user