diff --git a/booklore-ui/src/app/features/book/components/shelf-assigner/shelf-assigner.component.html b/booklore-ui/src/app/features/book/components/shelf-assigner/shelf-assigner.component.html index 8ae9beef0..ce95fbf9c 100644 --- a/booklore-ui/src/app/features/book/components/shelf-assigner/shelf-assigner.component.html +++ b/booklore-ui/src/app/features/book/components/shelf-assigner/shelf-assigner.component.html @@ -32,10 +32,22 @@ {{ t('shelvesAvailable', { count: (shelfState$ | async)!.shelves!.length }) }} + + + + @if (searchQuery) { + + } +
- @for (shelf of (shelfState$ | async)?.shelves; track shelf.id) { + @for (shelf of filterShelves((shelfState$ | async)!.shelves!); track shelf.id) {
= combineLatest([ this.shelfService.shelfState$, this.userService.userState$ ]).pipe( - map(([state, userState]) => ({ - ...state, - shelves: state.shelves?.filter(s => s.userId === userState.user?.id) || [] - })) + map(([state, userState]) => { + if (userState.user?.userSettings.sidebarShelfSorting) { + this.shelfSortField = this.validateSortField(userState.user.userSettings.sidebarShelfSorting.field); + this.shelfSortOrder = this.validateSortOrder(userState.user.userSettings.sidebarShelfSorting.order); + } + const filtered = state.shelves?.filter(s => s.userId === userState.user?.id) || []; + return { + ...state, + shelves: this.sortShelves(filtered) + }; + }) ); book: Book = this.dynamicDialogConfig.data.book; @@ -132,4 +149,34 @@ export class ShelfAssignerComponent implements OnInit { return {type: 'PRIME_NG', value: `pi pi-${shelf.icon}`}; } } + + filterShelves(shelves: Shelf[]): Shelf[] { + if (!this.searchQuery.trim()) { + return shelves; + } + const query = this.searchQuery.trim().toLowerCase(); + return shelves.filter(s => s.name.toLowerCase().includes(query)); + } + + private sortShelves(shelves: Shelf[]): Shelf[] { + return [...shelves].sort((a, b) => { + const aVal = (a as unknown as Record)[this.shelfSortField] ?? ''; + const bVal = (b as unknown as Record)[this.shelfSortField] ?? ''; + let comparison = 0; + if (typeof aVal === 'string' && typeof bVal === 'string') { + comparison = aVal.localeCompare(bVal); + } else if (typeof aVal === 'number' && typeof bVal === 'number') { + comparison = aVal - bVal; + } + return this.shelfSortOrder === 'asc' ? comparison : -comparison; + }); + } + + private validateSortField(field: string): 'name' | 'id' { + return field === 'id' ? 'id' : 'name'; + } + + private validateSortOrder(order: string): 'asc' | 'desc' { + return order === 'desc' ? 'desc' : 'asc'; + } } diff --git a/booklore-ui/src/i18n/en/book.json b/booklore-ui/src/i18n/en/book.json index 28581c4e6..c7bfe5a5e 100644 --- a/booklore-ui/src/i18n/en/book.json +++ b/booklore-ui/src/i18n/en/book.json @@ -607,6 +607,7 @@ "descriptionMulti": "Select shelves for {{ count }} book(s)", "descriptionSingle": "Organize \"{{ title }}\" into your shelves", "shelvesAvailable": "{{ count }} shelf(ves) available", + "searchPlaceholder": "Search shelves...", "emptyTitle": "No Shelves Available", "emptyDescription": "Create your first shelf to start organizing your books.
Click the button below to get started.", "createShelf": "Create Shelf", diff --git a/booklore-ui/src/i18n/es/book.json b/booklore-ui/src/i18n/es/book.json index cdbb84a03..f4de19070 100644 --- a/booklore-ui/src/i18n/es/book.json +++ b/booklore-ui/src/i18n/es/book.json @@ -607,6 +607,7 @@ "descriptionMulti": "Seleccionar estantes para {{ count }} libro(s)", "descriptionSingle": "Organizar \"{{ title }}\" en tus estantes", "shelvesAvailable": "{{ count }} estante(s) disponible(s)", + "searchPlaceholder": "Buscar estantes...", "emptyTitle": "No hay estantes disponibles", "emptyDescription": "Crea tu primer estante para empezar a organizar tus libros.
Haz clic en el botón de abajo para comenzar.", "createShelf": "Crear estante",