Implement user-configurable sidebar filter sorting
This commit is contained in:
committed by
Aditya Chandel
parent
49b56b78e9
commit
47d8aa9ef0
+29
@@ -0,0 +1,29 @@
|
||||
<div class="p-4">
|
||||
<div class="flex items-center gap-2 pb-6 pt-4">
|
||||
<p class="text-lg">Sidebar Filter Sorting:</p>
|
||||
<i class="pi pi-info-circle text-sky-600"
|
||||
pTooltip="This setting controls the order in which filter values (like authors, categories, etc.) appear in the sidebar."
|
||||
tooltipPosition="right"
|
||||
style="cursor: pointer;">
|
||||
</i>
|
||||
</div>
|
||||
|
||||
<div class="grid grid-cols-[auto,1fr] pl-6 gap-y-4 gap-x-4 items-center">
|
||||
<p class="py-1">Sorting Mode:
|
||||
<i class="pi pi-info-circle text-sky-600"
|
||||
pTooltip="You can sort filter options alphabetically (A–Z) or by the number of books that match each filter value. For example, categories with more books can appear at the top."
|
||||
tooltipPosition="right"
|
||||
style="cursor: pointer;">
|
||||
</i>
|
||||
</p>
|
||||
<div class="flex gap-4 justify-start">
|
||||
<p-select
|
||||
size="small"
|
||||
[options]="filterSortingOptions"
|
||||
[(ngModel)]="selectedFilterSorting"
|
||||
(ngModelChange)="onFilterSortingChange()"
|
||||
placeholder="Select Sorting">
|
||||
</p-select>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
import {Component, inject, OnDestroy, OnInit} from '@angular/core';
|
||||
import {Select} from 'primeng/select';
|
||||
import {Tooltip} from 'primeng/tooltip';
|
||||
import {User, UserService, UserSettings} from '../../user-management/user.service';
|
||||
import {MessageService} from 'primeng/api';
|
||||
import {Observable, Subscription} from 'rxjs';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
|
||||
@Component({
|
||||
selector: 'app-sidebar-filter-sorting-prefs-component',
|
||||
imports: [
|
||||
Select,
|
||||
Tooltip,
|
||||
FormsModule
|
||||
],
|
||||
templateUrl: './sidebar-filter-sorting-prefs-component.html',
|
||||
styleUrl: './sidebar-filter-sorting-prefs-component.scss'
|
||||
})
|
||||
export class SidebarFilterSortingPrefsComponent implements OnInit, OnDestroy {
|
||||
|
||||
readonly filterSortingOptions = [
|
||||
{label: 'Alphabetical (A–Z)', value: 'alphabetical'},
|
||||
{label: 'Book Count (High to Low)', value: 'count'}
|
||||
];
|
||||
|
||||
selectedFilterSorting: 'alphabetical' | 'count' = 'count';
|
||||
|
||||
private readonly userService = inject(UserService);
|
||||
private readonly messageService = inject(MessageService);
|
||||
|
||||
userData$: Observable<User | null> = this.userService.userState$;
|
||||
private subscription?: Subscription;
|
||||
private currentUser: User | null = null;
|
||||
|
||||
ngOnInit(): void {
|
||||
this.subscription = this.userData$.subscribe(user => {
|
||||
if (user) {
|
||||
this.currentUser = user;
|
||||
this.loadPreferences(user.userSettings);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
this.subscription?.unsubscribe();
|
||||
}
|
||||
|
||||
private loadPreferences(settings: UserSettings): void {
|
||||
console.log(settings.filterSortingMode)
|
||||
this.selectedFilterSorting = settings.filterSortingMode;
|
||||
}
|
||||
|
||||
private updatePreference(path: string[], value: any): void {
|
||||
if (!this.currentUser) return;
|
||||
let target: any = this.currentUser.userSettings;
|
||||
for (let i = 0; i < path.length - 1; i++) {
|
||||
target = target[path[i]] ||= {};
|
||||
}
|
||||
target[path.at(-1)!] = value;
|
||||
|
||||
const [rootKey] = path;
|
||||
const updatedValue = this.currentUser.userSettings[rootKey as keyof UserSettings];
|
||||
this.userService.updateUserSetting(this.currentUser.id, rootKey, updatedValue);
|
||||
this.messageService.add({
|
||||
severity: 'success',
|
||||
summary: 'Preferences Updated',
|
||||
detail: 'Your preferences have been saved successfully.',
|
||||
life: 2000
|
||||
});
|
||||
}
|
||||
|
||||
onFilterSortingChange(): void {
|
||||
this.updatePreference(['filterSortingMode'], this.selectedFilterSorting);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
<div class="p-4">
|
||||
<div class="flex items-center gap-2 pb-6 pt-4">
|
||||
<p class="text-lg ">Sidebar Library and Shelf Sorting Perference</p>
|
||||
<p class="text-lg">Sidebar Library and Shelf Sorting Preference</p>
|
||||
<i class="pi pi-info-circle text-sky-600"
|
||||
pTooltip="Configure sorting options for your library and shelves shown in the sidebar."
|
||||
tooltipPosition="right"
|
||||
|
||||
-2
@@ -53,7 +53,6 @@ export class SidebarSortingPreferencesComponent implements OnInit, OnDestroy {
|
||||
this.selectedShelfSorting = settings.sidebarShelfSorting;
|
||||
}
|
||||
|
||||
|
||||
private updatePreference(path: string[], value: any): void {
|
||||
if (!this.currentUser) return;
|
||||
let target: any = this.currentUser.userSettings;
|
||||
@@ -80,5 +79,4 @@ export class SidebarSortingPreferencesComponent implements OnInit, OnDestroy {
|
||||
onShelfSortingChange() {
|
||||
this.updatePreference(['sidebarShelfSorting'], this.selectedShelfSorting);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+2
@@ -2,4 +2,6 @@
|
||||
<app-view-preferences></app-view-preferences>
|
||||
<p-divider></p-divider>
|
||||
<app-sidebar-sorting-preferences></app-sidebar-sorting-preferences>
|
||||
<p-divider></p-divider>
|
||||
<app-sidebar-filter-sorting-prefs-component></app-sidebar-filter-sorting-prefs-component>
|
||||
</div>
|
||||
|
||||
+3
-1
@@ -5,6 +5,7 @@ import {TableModule} from 'primeng/table';
|
||||
import {ToastModule} from 'primeng/toast';
|
||||
import {ViewPreferencesComponent} from './view-preferences/view-preferences.component';
|
||||
import {SidebarSortingPreferencesComponent} from './sidebar-sorting-preferences/sidebar-sorting-preferences.component';
|
||||
import {SidebarFilterSortingPrefsComponent} from './sidebar-filter-sorting-prefs-component/sidebar-filter-sorting-prefs-component';
|
||||
|
||||
@Component({
|
||||
selector: 'app-view-preferences-parent',
|
||||
@@ -15,7 +16,8 @@ import {SidebarSortingPreferencesComponent} from './sidebar-sorting-preferences/
|
||||
TableModule,
|
||||
ToastModule,
|
||||
ViewPreferencesComponent,
|
||||
SidebarSortingPreferencesComponent
|
||||
SidebarSortingPreferencesComponent,
|
||||
SidebarFilterSortingPrefsComponent
|
||||
],
|
||||
templateUrl: './view-preferences-parent.component.html',
|
||||
styleUrl: './view-preferences-parent.component.scss'
|
||||
|
||||
Reference in New Issue
Block a user