feat(ui): sync profile locale and theme preferences (#1604)
This commit is contained in:
@@ -52,6 +52,10 @@ function makeCurrentUser() {
|
||||
username: 'tester',
|
||||
name: 'Tester',
|
||||
email: 'tester@example.com',
|
||||
locale: 'en',
|
||||
theme: 'grimmory',
|
||||
themeAccent: null,
|
||||
themeSyncEnabled: true,
|
||||
assignedLibraries: [],
|
||||
permissions: {
|
||||
admin: false,
|
||||
|
||||
+4
@@ -41,6 +41,10 @@ function makeUser(metadataCenterViewMode: 'route' | 'dialog'): User {
|
||||
username: 'tester',
|
||||
name: 'Tester',
|
||||
email: 'tester@example.com',
|
||||
locale: 'en',
|
||||
theme: 'grimmory',
|
||||
themeAccent: null,
|
||||
themeSyncEnabled: true,
|
||||
assignedLibraries: [],
|
||||
permissions: {
|
||||
admin: false,
|
||||
|
||||
+4
@@ -38,6 +38,10 @@ function buildUser(overrides: Partial<User['permissions']> = {}): User {
|
||||
username: 'admin',
|
||||
name: 'Admin',
|
||||
email: 'admin@example.com',
|
||||
locale: 'en',
|
||||
theme: 'grimmory',
|
||||
themeAccent: null,
|
||||
themeSyncEnabled: true,
|
||||
assignedLibraries: [],
|
||||
permissions: {admin: false, ...overrides} as User['permissions'],
|
||||
userSettings: {} as User['userSettings'],
|
||||
|
||||
@@ -17,6 +17,10 @@ function buildUser(overrides: Partial<User['permissions']> = {}): User {
|
||||
username: 'reader',
|
||||
name: 'Reader',
|
||||
email: 'reader@example.com',
|
||||
locale: 'en',
|
||||
theme: 'grimmory',
|
||||
themeAccent: null,
|
||||
themeSyncEnabled: true,
|
||||
assignedLibraries: [],
|
||||
permissions: {admin: false, canAccessOpds: true, ...overrides} as User['permissions'],
|
||||
userSettings: {} as User['userSettings'],
|
||||
|
||||
+8
@@ -64,6 +64,10 @@ describe('ReaderPreferencesService', () => {
|
||||
username: 'reader',
|
||||
name: 'Reader',
|
||||
email: 'reader@example.test',
|
||||
locale: 'en',
|
||||
theme: 'grimmory',
|
||||
themeAccent: null,
|
||||
themeSyncEnabled: true,
|
||||
assignedLibraries: [],
|
||||
} as unknown as User);
|
||||
const updateUserSetting = vi.fn();
|
||||
@@ -148,6 +152,10 @@ describe('ReaderPreferencesService', () => {
|
||||
username: 'reader',
|
||||
name: 'Reader',
|
||||
email: 'reader@example.test',
|
||||
locale: 'en',
|
||||
theme: 'grimmory',
|
||||
themeAccent: null,
|
||||
themeSyncEnabled: true,
|
||||
assignedLibraries: [],
|
||||
} as unknown as User);
|
||||
const updateUserSetting = vi.fn();
|
||||
|
||||
@@ -109,6 +109,10 @@ function buildUser(overrides: BuildUserOverrides = {}): User {
|
||||
username: 'reader',
|
||||
name: 'Reader',
|
||||
email: 'reader@example.test',
|
||||
locale: 'en',
|
||||
theme: 'grimmory',
|
||||
themeAccent: null,
|
||||
themeSyncEnabled: true,
|
||||
assignedLibraries: [],
|
||||
permissions,
|
||||
userSettings: buildUserSettings(userSettings),
|
||||
@@ -233,4 +237,24 @@ describe('UserService', () => {
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('updates the current-user profile through the profile endpoint', async () => {
|
||||
httpTestingController.expectOne(req => req.url.endsWith('/api/v1/users/me')).flush(buildUser());
|
||||
await flushCurrentUserQuery();
|
||||
|
||||
service.updateUserProfile(7, {locale: 'de', theme: 'custom', themeAccent: 'teal', themeSyncEnabled: true}).subscribe();
|
||||
|
||||
const request = httpTestingController.expectOne(req => req.url.endsWith('/api/v1/users/7/profile'));
|
||||
expect(request.request.method).toBe('PUT');
|
||||
expect(request.request.body).toEqual({locale: 'de', theme: 'custom', themeAccent: 'teal', themeSyncEnabled: true});
|
||||
request.flush(buildUser({locale: 'de', theme: 'custom', themeAccent: 'teal', themeSyncEnabled: true}));
|
||||
await flushCurrentUserQuery();
|
||||
|
||||
expect(service.currentUser()).toEqual(expect.objectContaining({
|
||||
locale: 'de',
|
||||
theme: 'custom',
|
||||
themeAccent: 'teal',
|
||||
themeSyncEnabled: true,
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -8,6 +8,7 @@ import { AuthService } from '../../../shared/service/auth.service';
|
||||
import { DashboardConfig } from '../../dashboard/models/dashboard-config.model';
|
||||
import { injectQuery, queryOptions, QueryClient } from '@tanstack/angular-query-experimental';
|
||||
import { CURRENT_USER_QUERY_KEY } from './user-query-keys';
|
||||
import { AppTheme, CustomPrimary } from '../../../shared/model/app-state.model';
|
||||
|
||||
export interface EntityViewPreferences {
|
||||
global: EntityViewPreference;
|
||||
@@ -319,6 +320,10 @@ export interface User {
|
||||
username: string;
|
||||
name: string;
|
||||
email: string;
|
||||
locale: string;
|
||||
theme: AppTheme;
|
||||
themeAccent: CustomPrimary | null;
|
||||
themeSyncEnabled: boolean;
|
||||
assignedLibraries: Library[];
|
||||
permissions: {
|
||||
admin: boolean;
|
||||
@@ -370,6 +375,15 @@ export interface UserUpdateRequest {
|
||||
assignedLibraries?: number[];
|
||||
}
|
||||
|
||||
export interface UserProfileUpdateRequest {
|
||||
name?: string;
|
||||
email?: string;
|
||||
locale?: string;
|
||||
theme?: AppTheme;
|
||||
themeAccent?: CustomPrimary | null;
|
||||
themeSyncEnabled?: boolean;
|
||||
}
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
@@ -446,6 +460,17 @@ export class UserService {
|
||||
);
|
||||
}
|
||||
|
||||
updateUserProfile(userId: number, updateData: UserProfileUpdateRequest): Observable<User> {
|
||||
return this.http.put<User>(`${this.userUrl}/${userId}/profile`, updateData).pipe(
|
||||
map(user => this.normalizeUser(user)),
|
||||
tap(user => {
|
||||
if (this.currentUser()?.id === user.id) {
|
||||
this.queryClient.setQueryData(CURRENT_USER_QUERY_KEY, user);
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
deleteUser(userId: number): Observable<void> {
|
||||
return this.http.delete<void>(`${this.userUrl}/${userId}`);
|
||||
}
|
||||
@@ -496,8 +521,13 @@ export class UserService {
|
||||
|
||||
private normalizeUser(user: User): User {
|
||||
const permissions = user.permissions;
|
||||
const theme = user.theme ?? 'grimmory';
|
||||
return {
|
||||
...user,
|
||||
locale: user.locale ?? 'en',
|
||||
theme,
|
||||
themeAccent: theme === 'custom' ? user.themeAccent ?? 'orange' : null,
|
||||
themeSyncEnabled: user.themeSyncEnabled ?? true,
|
||||
permissions: {
|
||||
...permissions,
|
||||
canBulkResetGrimmoryReadProgress: permissions.canBulkResetGrimmoryReadProgress ?? permissions.canBulkResetBookloreReadProgress,
|
||||
|
||||
+10
-1
@@ -48,7 +48,7 @@
|
||||
<p-toggleswitch
|
||||
inputId="oled-dark-mode"
|
||||
[ngModel]="oledDarkMode()"
|
||||
(ngModelChange)="configService.setOledDarkMode($event)">
|
||||
(ngModelChange)="updateOledDarkMode($event)">
|
||||
</p-toggleswitch>
|
||||
</div>
|
||||
}
|
||||
@@ -93,6 +93,15 @@
|
||||
</p-select>
|
||||
</div>
|
||||
|
||||
<div class="form-field">
|
||||
<label class="field-label" for="theme-sync">{{ 'layout.theme.syncAcrossDevices' | transloco }}</label>
|
||||
<p-toggleswitch
|
||||
inputId="theme-sync"
|
||||
[ngModel]="selectedThemeSyncEnabled()"
|
||||
(ngModelChange)="updateThemeSyncEnabled($event)">
|
||||
</p-toggleswitch>
|
||||
</div>
|
||||
|
||||
@if (selectedThemePreference() === 'custom') {
|
||||
<fieldset class="form-field">
|
||||
<legend class="field-label">{{ 'layout.theme.customPrimary' | transloco }}</legend>
|
||||
|
||||
+74
-21
@@ -3,21 +3,24 @@ import {Button} from 'primeng/button';
|
||||
import {AbstractControl, FormBuilder, FormGroup, FormsModule, ReactiveFormsModule, ValidationErrors, ValidatorFn, Validators} from '@angular/forms';
|
||||
import {InputText} from 'primeng/inputtext';
|
||||
import {Password} from 'primeng/password';
|
||||
import {User, UserProfileUpdateRequest, UserService} from '../user-management/user.service';
|
||||
import {ToggleSwitch} from 'primeng/toggleswitch';
|
||||
import {User, UserService, UserUpdateRequest} from '../user-management/user.service';
|
||||
import {MessageService} from 'primeng/api';
|
||||
import {DynamicDialogRef} from 'primeng/dynamicdialog';
|
||||
import {TranslocoDirective, TranslocoPipe, TranslocoService} from '@jsverse/transloco';
|
||||
import {Select} from 'primeng/select';
|
||||
import {takeUntilDestroyed, toSignal} from '@angular/core/rxjs-interop';
|
||||
import {from, switchMap} from 'rxjs';
|
||||
import {AVAILABLE_LANGS, LANG_LABELS} from '../../../core/config/transloco-loader';
|
||||
import {LANG_STORAGE_KEY} from '../../../core/config/language-initializer';
|
||||
import {AppConfigService} from '../../../shared/service/app-config.service';
|
||||
import {AppThemeService} from '../../../shared/service/app-theme.service';
|
||||
import {AppLocaleService} from '../../../shared/service/app-locale.service';
|
||||
import {
|
||||
AppearancePreference,
|
||||
AppTheme,
|
||||
CUSTOM_PRIMARY_OPTIONS,
|
||||
CustomPrimary,
|
||||
DEFAULT_APP_THEME,
|
||||
DEFAULT_CUSTOM_PRIMARY,
|
||||
} from '../../../shared/model/app-state.model';
|
||||
|
||||
export const passwordMatchValidator: ValidatorFn = (control: AbstractControl): ValidationErrors | null => {
|
||||
@@ -59,24 +62,26 @@ export class UserProfileDialogComponent {
|
||||
}));
|
||||
|
||||
protected readonly userService = inject(UserService);
|
||||
protected readonly configService = inject(AppConfigService);
|
||||
protected readonly themeService = inject(AppThemeService);
|
||||
protected readonly appLocaleService = inject(AppLocaleService);
|
||||
private readonly messageService = inject(MessageService);
|
||||
private readonly fb = inject(FormBuilder);
|
||||
private readonly dialogRef = inject(DynamicDialogRef);
|
||||
private readonly t = inject(TranslocoService);
|
||||
private readonly destroyRef = inject(DestroyRef);
|
||||
protected readonly activeLang = toSignal(this.t.langChanges$, {initialValue: this.t.getActiveLang()});
|
||||
protected readonly selectedThemePreference = computed(() => this.configService.appState().themePreference);
|
||||
protected readonly selectedAppearancePreference = computed(() => this.configService.appState().appearancePreference);
|
||||
protected readonly oledDarkMode = computed(() => this.configService.appState().oledDarkMode);
|
||||
protected readonly showOledDarkModeToggle = computed(() => this.configService.effectiveAppearance() === 'dark');
|
||||
protected readonly selectedThemePreference = computed(() => this.themeService.appState().themePreference ?? DEFAULT_APP_THEME);
|
||||
protected readonly selectedAppearancePreference = computed(() => this.themeService.appState().appearancePreference ?? 'system');
|
||||
protected readonly oledDarkMode = computed(() => this.themeService.appState().oledDarkMode);
|
||||
protected readonly showOledDarkModeToggle = computed(() => this.themeService.effectiveAppearance() === 'dark');
|
||||
protected readonly selectedCustomPrimary = computed<CustomPrimary>(
|
||||
() => this.configService.appState().customPrimary,
|
||||
() => this.themeService.appState().customPrimary ?? DEFAULT_CUSTOM_PRIMARY,
|
||||
);
|
||||
protected readonly selectedThemeSyncEnabled = computed(() => this.themeService.appState().themeSyncEnabled !== false);
|
||||
protected readonly customPrimaryOptions = CUSTOM_PRIMARY_OPTIONS;
|
||||
protected readonly themeOptions = computed(() => {
|
||||
this.activeLang();
|
||||
return this.configService.themes.map((theme) => ({
|
||||
return this.themeService.themes.map((theme) => ({
|
||||
value: theme.name,
|
||||
label: this.t.translate(theme.labelKey),
|
||||
}));
|
||||
@@ -128,24 +133,72 @@ export class UserProfileDialogComponent {
|
||||
|
||||
onLanguageChange(lang: string): void {
|
||||
if (lang === this.activeLang()) return;
|
||||
this.t.load(lang).pipe(takeUntilDestroyed(this.destroyRef)).subscribe({
|
||||
next: () => {
|
||||
this.t.setActiveLang(lang);
|
||||
localStorage.setItem(LANG_STORAGE_KEY, lang);
|
||||
},
|
||||
});
|
||||
if (!this.currentUser) return;
|
||||
|
||||
this.userService.updateUserProfile(this.currentUser.id, {locale: lang}).pipe(
|
||||
switchMap(user => from(this.appLocaleService.applyLocale(user.locale))),
|
||||
takeUntilDestroyed(this.destroyRef),
|
||||
).subscribe();
|
||||
}
|
||||
|
||||
updateThemePreference(themePreference: AppTheme): void {
|
||||
this.configService.setThemePreference(themePreference);
|
||||
if (!this.currentUser) return;
|
||||
|
||||
if (!this.selectedThemeSyncEnabled()) {
|
||||
this.themeService.applyDeviceTheme(themePreference, this.selectedCustomPrimary());
|
||||
return;
|
||||
}
|
||||
|
||||
const updateRequest: UserProfileUpdateRequest = {
|
||||
theme: themePreference,
|
||||
...(themePreference === 'custom' ? {themeAccent: this.selectedCustomPrimary()} : {}),
|
||||
};
|
||||
|
||||
this.userService.updateUserProfile(this.currentUser.id, updateRequest).pipe(takeUntilDestroyed(this.destroyRef)).subscribe({
|
||||
next: (user) => this.themeService.applySyncedTheme(user.theme, user.themeAccent ?? DEFAULT_CUSTOM_PRIMARY),
|
||||
});
|
||||
}
|
||||
|
||||
updateCustomPrimary(customPrimary: CustomPrimary): void {
|
||||
this.configService.setCustomPrimary(customPrimary);
|
||||
if (!this.currentUser) return;
|
||||
|
||||
if (!this.selectedThemeSyncEnabled()) {
|
||||
this.themeService.applyDeviceTheme('custom', customPrimary);
|
||||
return;
|
||||
}
|
||||
|
||||
this.userService.updateUserProfile(this.currentUser.id, {theme: 'custom', themeAccent: customPrimary}).pipe(takeUntilDestroyed(this.destroyRef)).subscribe({
|
||||
next: (user) => this.themeService.applySyncedTheme(user.theme, user.themeAccent ?? DEFAULT_CUSTOM_PRIMARY),
|
||||
});
|
||||
}
|
||||
|
||||
updateThemeSyncEnabled(themeSyncEnabled: boolean): void {
|
||||
if (!this.currentUser) return;
|
||||
|
||||
if (!themeSyncEnabled) {
|
||||
this.userService.updateUserProfile(this.currentUser.id, {themeSyncEnabled: false}).pipe(takeUntilDestroyed(this.destroyRef)).subscribe({
|
||||
next: () => this.themeService.applyDeviceTheme(this.selectedThemePreference(), this.selectedCustomPrimary()),
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const theme = this.selectedThemePreference();
|
||||
const updateRequest: UserProfileUpdateRequest = {
|
||||
themeSyncEnabled: true,
|
||||
theme,
|
||||
...(theme === 'custom' ? {themeAccent: this.selectedCustomPrimary()} : {}),
|
||||
};
|
||||
this.userService.updateUserProfile(this.currentUser.id, updateRequest).pipe(takeUntilDestroyed(this.destroyRef)).subscribe({
|
||||
next: (user) => this.themeService.applySyncedTheme(user.theme, user.themeAccent ?? DEFAULT_CUSTOM_PRIMARY),
|
||||
});
|
||||
}
|
||||
|
||||
updateAppearancePreference(appearancePreference: AppearancePreference): void {
|
||||
this.configService.setAppearancePreference(appearancePreference);
|
||||
this.themeService.setAppearancePreference(appearancePreference);
|
||||
}
|
||||
|
||||
updateOledDarkMode(oledDarkMode: boolean): void {
|
||||
this.themeService.setOledDarkMode(oledDarkMode);
|
||||
}
|
||||
|
||||
updateProfile(): void {
|
||||
@@ -164,11 +217,11 @@ export class UserProfileDialogComponent {
|
||||
return;
|
||||
}
|
||||
|
||||
const updateRequest: UserUpdateRequest = {
|
||||
const updateRequest: UserProfileUpdateRequest = {
|
||||
name: this.editUserData.name,
|
||||
email: this.editUserData.email,
|
||||
};
|
||||
this.userService.updateUser(this.currentUser.id, updateRequest).subscribe({
|
||||
this.userService.updateUserProfile(this.currentUser.id, updateRequest).subscribe({
|
||||
next: () => {
|
||||
this.messageService.add({severity: 'success', summary: this.t.translate('common.success'), detail: this.t.translate('settingsProfile.toast.profileUpdated')});
|
||||
this.isEditing = false;
|
||||
|
||||
+4
@@ -35,6 +35,10 @@ describe('MetaCenterViewModeComponent', () => {
|
||||
name: 'Reader',
|
||||
email: 'reader@example.test',
|
||||
assignedLibraries: [],
|
||||
locale: 'en',
|
||||
theme: 'grimmory',
|
||||
themeAccent: null,
|
||||
themeSyncEnabled: true,
|
||||
} as unknown as User;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user