fix(ui): use signals for user management to improve reactivity (#1726)
This commit is contained in:
@@ -34,7 +34,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="section-body">
|
<div class="section-body">
|
||||||
@if (users.length === 0) {
|
@if (users().length === 0) {
|
||||||
<div class="empty-state">
|
<div class="empty-state">
|
||||||
<i class="pi pi-users"></i>
|
<i class="pi pi-users"></i>
|
||||||
<p class="empty-title">{{ t('emptyState.title') }}</p>
|
<p class="empty-title">{{ t('emptyState.title') }}</p>
|
||||||
@@ -42,7 +42,7 @@
|
|||||||
</div>
|
</div>
|
||||||
} @else {
|
} @else {
|
||||||
<div class="users-list">
|
<div class="users-list">
|
||||||
@for (user of users; track user.id) {
|
@for (user of users(); track user.id) {
|
||||||
<div class="user-card" [class.is-expanded]="isRowExpanded(user)" [class.is-admin]="user.permissions.admin">
|
<div class="user-card" [class.is-expanded]="isRowExpanded(user)" [class.is-admin]="user.permissions.admin">
|
||||||
<div class="user-card-header">
|
<div class="user-card-header">
|
||||||
<div class="user-main-info">
|
<div class="user-main-info">
|
||||||
|
|||||||
+79
-7
@@ -1,14 +1,86 @@
|
|||||||
import {describe, it} from 'vitest';
|
import {signal} from '@angular/core';
|
||||||
|
import {TestBed} from '@angular/core/testing';
|
||||||
|
import {MessageService} from 'primeng/api';
|
||||||
|
import {of, type Observable} from 'rxjs';
|
||||||
|
import {describe, expect, it, vi} from 'vitest';
|
||||||
|
|
||||||
// NOTE(frontend-seam): Real coverage here needs seams around table row editing, confirm dialog
|
import {getTranslocoModule} from '../../../core/testing/transloco-testing';
|
||||||
// usage, password dialog state, and dialog-launcher interactions so user administration can be
|
import {AppSettings} from '../../../shared/model/app-settings.model';
|
||||||
// exercised without building the full Prime table and modal stack.
|
import {AppSettingsService} from '../../../shared/service/app-settings.service';
|
||||||
describe.skip('UserManagementComponent', () => {
|
import {DialogLauncherService} from '../../../shared/services/dialog-launcher.service';
|
||||||
it('needs service seams to verify user loading, edit toggling, and save payload shaping', () => {
|
import {LibraryService} from '../../book/service/library.service';
|
||||||
|
import {UserManagementComponent, type UserWithEditing} from './user-management.component';
|
||||||
|
import {User, UserService} from './user.service';
|
||||||
|
|
||||||
|
function buildUser(overrides: Partial<User['permissions']> = {}): User {
|
||||||
|
return {
|
||||||
|
id: 1,
|
||||||
|
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'],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
interface UserManagementTestEnv {
|
||||||
|
userState: ReturnType<typeof signal<User | null>>;
|
||||||
|
appSettingsState: ReturnType<typeof signal<AppSettings | null>>;
|
||||||
|
getUsers: () => Observable<UserWithEditing[]>;
|
||||||
|
}
|
||||||
|
|
||||||
|
function setupUserManagementTest(env: UserManagementTestEnv): void {
|
||||||
|
TestBed.configureTestingModule({
|
||||||
|
imports: [UserManagementComponent, getTranslocoModule()],
|
||||||
|
providers: [
|
||||||
|
{
|
||||||
|
provide: UserService,
|
||||||
|
useValue: {
|
||||||
|
currentUser: () => env.userState(),
|
||||||
|
getUsers: env.getUsers
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ provide: LibraryService, useValue: { libraries: signal([]) } },
|
||||||
|
{
|
||||||
|
provide: DialogLauncherService,
|
||||||
|
useValue: {
|
||||||
|
openCreateUserDialog: vi.fn(() => Promise.resolve(null)),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{provide: AppSettingsService, useValue: {appSettings: () => env.appSettingsState()}},
|
||||||
|
{provide: MessageService, useValue: {add: vi.fn()}},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('UserManagementComponent', () => {
|
||||||
|
it('loads and hydrates users on init', () => {
|
||||||
|
const userState = signal<User | null>(buildUser({admin: true}));
|
||||||
|
const appSettingsState = signal<AppSettings | null>(null);
|
||||||
|
const getUsers = vi.fn(() => of([buildUser({admin: true})] as UserWithEditing[]));
|
||||||
|
|
||||||
|
setupUserManagementTest({userState, appSettingsState, getUsers});
|
||||||
|
|
||||||
|
const fixture = TestBed.createComponent(UserManagementComponent);
|
||||||
|
const component = fixture.componentInstance;
|
||||||
|
fixture.detectChanges();
|
||||||
|
|
||||||
|
expect(getUsers).toHaveBeenCalledOnce();
|
||||||
|
expect(component.users()).toHaveLength(1);
|
||||||
|
|
||||||
|
fixture.destroy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it.skip('needs service seams to verify user loading, edit toggling, and save payload shaping', () => {
|
||||||
// TODO(seam): Cover loadUsers, toggleEdit, and saveUser once the table-editing state and async user-service flows are isolated.
|
// TODO(seam): Cover loadUsers, toggleEdit, and saveUser once the table-editing state and async user-service flows are isolated.
|
||||||
});
|
});
|
||||||
|
|
||||||
it('needs dialog seams to verify create-user refresh, delete confirmation, and password-change validation', () => {
|
it.skip('needs dialog seams to verify create-user refresh, delete confirmation, and password-change validation', () => {
|
||||||
// TODO(seam): Cover openCreateUserDialog, deleteUser, and submitPasswordChange after extracting confirm and modal runtime concerns.
|
// TODO(seam): Cover openCreateUserDialog, deleteUser, and submitPasswordChange after extracting confirm and modal runtime concerns.
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import {Component, DestroyRef, inject, OnInit} from '@angular/core';
|
import {Component, DestroyRef, inject, OnInit, signal, WritableSignal} from '@angular/core';
|
||||||
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
|
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
|
||||||
import {FormsModule} from '@angular/forms';
|
import {FormsModule} from '@angular/forms';
|
||||||
import {Button} from 'primeng/button';
|
import {Button} from 'primeng/button';
|
||||||
@@ -19,7 +19,7 @@ import {DialogLauncherService} from '../../../shared/services/dialog-launcher.se
|
|||||||
import {ContentRestrictionsEditorComponent} from './content-restrictions-editor/content-restrictions-editor.component';
|
import {ContentRestrictionsEditorComponent} from './content-restrictions-editor/content-restrictions-editor.component';
|
||||||
import {TranslocoDirective, TranslocoPipe, TranslocoService} from '@jsverse/transloco';
|
import {TranslocoDirective, TranslocoPipe, TranslocoService} from '@jsverse/transloco';
|
||||||
|
|
||||||
interface UserWithEditing extends User {
|
export interface UserWithEditing extends User {
|
||||||
isEditing?: boolean;
|
isEditing?: boolean;
|
||||||
selectedLibraryIds?: number[];
|
selectedLibraryIds?: number[];
|
||||||
libraryNames?: string;
|
libraryNames?: string;
|
||||||
@@ -56,7 +56,7 @@ export class UserManagementComponent implements OnInit {
|
|||||||
private destroyRef = inject(DestroyRef);
|
private destroyRef = inject(DestroyRef);
|
||||||
get allLibraries() { return this.libraryService.libraries(); }
|
get allLibraries() { return this.libraryService.libraries(); }
|
||||||
|
|
||||||
users: UserWithEditing[] = [];
|
users: WritableSignal<UserWithEditing[]> = signal([]);
|
||||||
currentUser: User | null = null;
|
currentUser: User | null = null;
|
||||||
editingLibraryIds: number[] = [];
|
editingLibraryIds: number[] = [];
|
||||||
expandedRows: Record<string, boolean> = {};
|
expandedRows: Record<string, boolean> = {};
|
||||||
@@ -82,13 +82,13 @@ export class UserManagementComponent implements OnInit {
|
|||||||
loadUsers() {
|
loadUsers() {
|
||||||
this.userService.getUsers().pipe(takeUntilDestroyed(this.destroyRef)).subscribe({
|
this.userService.getUsers().pipe(takeUntilDestroyed(this.destroyRef)).subscribe({
|
||||||
next: (data) => {
|
next: (data) => {
|
||||||
this.users = data.map((user) => ({
|
this.users.set(data.map((user: UserWithEditing) => ({
|
||||||
...user,
|
...user,
|
||||||
isEditing: false,
|
isEditing: false,
|
||||||
selectedLibraryIds: user.assignedLibraries?.map((lib) => lib.id!).filter(id => id !== undefined) as number[] || [],
|
selectedLibraryIds: user.assignedLibraries?.map((lib) => lib.id!).filter(id => id !== undefined) as number[] || [],
|
||||||
libraryNames:
|
libraryNames:
|
||||||
user.assignedLibraries?.map((lib) => lib.name).join(', ') || '',
|
user.assignedLibraries?.map((lib) => lib.name).join(', ') || '',
|
||||||
}));
|
})));
|
||||||
},
|
},
|
||||||
error: () => {
|
error: () => {
|
||||||
this.messageService.add({
|
this.messageService.add({
|
||||||
|
|||||||
Reference in New Issue
Block a user