diff --git a/src-ui/src/app/services/document-list-view.service.spec.ts b/src-ui/src/app/services/document-list-view.service.spec.ts index 74ef8e5a1..26455f8c0 100644 --- a/src-ui/src/app/services/document-list-view.service.spec.ts +++ b/src-ui/src/app/services/document-list-view.service.spec.ts @@ -599,14 +599,17 @@ describe('DocumentListViewService', () => { it('should not filter out custom fields if settings not initialized', () => { const customFields = ['custom_field_1', 'custom_field_2'] documentListViewService.displayFields = customFields as any - settingsService.displayFieldsInitialized = false expect(documentListViewService.displayFields).toEqual(customFields) jest.spyOn(settingsService, 'allDisplayFields', 'get').mockReturnValue([ { id: DisplayField.ADDED, name: 'Added' }, { id: DisplayField.TITLE, name: 'Title' }, { id: 'custom_field_1', name: 'Custom Field 1' }, ] as any) - settingsService.displayFieldsInitialized = true + settingsService.displayFieldsInit.emit(true) + expect(documentListViewService.displayFields).toEqual(['custom_field_1']) + + // will now filter on set + documentListViewService.displayFields = customFields as any expect(documentListViewService.displayFields).toEqual(['custom_field_1']) }) }) diff --git a/src-ui/src/app/services/document-list-view.service.ts b/src-ui/src/app/services/document-list-view.service.ts index 3867376de..eee8daea1 100644 --- a/src-ui/src/app/services/document-list-view.service.ts +++ b/src-ui/src/app/services/document-list-view.service.ts @@ -20,6 +20,10 @@ import { paramsFromViewState, paramsToViewState } from '../utils/query-params' import { DocumentService, SelectionData } from './rest/document.service' import { SettingsService } from './settings.service' +const LIST_DEFAULT_DISPLAY_FIELDS: DisplayField[] = DEFAULT_DISPLAY_FIELDS.map( + (f) => f.id +).filter((f) => f !== DisplayField.ADDED) + /** * Captures the current state of the list view. */ @@ -102,6 +106,8 @@ export class DocumentListViewService { private _activeSavedViewId: number = null + private displayFieldsInitialized: boolean = false + get activeSavedViewId() { return this._activeSavedViewId } @@ -134,6 +140,19 @@ export class DocumentListViewService { localStorage.removeItem(DOCUMENT_LIST_SERVICE.CURRENT_VIEW_CONFIG) } } + + this.settings.displayFieldsInit.subscribe(() => { + this.displayFieldsInitialized = true + if (this.activeListViewState.displayFields) { + this.activeListViewState.displayFields = + this.activeListViewState.displayFields.filter( + (field) => + this.settings.allDisplayFields.find((f) => f.id === field) !== + undefined + ) + this.saveDocumentListView() + } + }) } private defaultListViewState(): ListViewState { @@ -415,23 +434,17 @@ export class DocumentListViewService { } get displayFields(): DisplayField[] { - let fields = - this.activeListViewState.displayFields ?? - DEFAULT_DISPLAY_FIELDS.map((f) => f.id) - if (!this.activeListViewState.displayFields) { - fields = fields.filter((f) => f !== DisplayField.ADDED) - } - return this.settings.displayFieldsInitialized - ? fields.filter( + return this.activeListViewState.displayFields ?? LIST_DEFAULT_DISPLAY_FIELDS + } + + set displayFields(fields: DisplayField[]) { + this.activeListViewState.displayFields = this.displayFieldsInitialized + ? fields?.filter( (field) => this.settings.allDisplayFields.find((f) => f.id === field) !== undefined ) : fields - } - - set displayFields(fields: DisplayField[]) { - this.activeListViewState.displayFields = fields this.saveDocumentListView() } diff --git a/src-ui/src/app/services/settings.service.ts b/src-ui/src/app/services/settings.service.ts index c3ea3f856..5005a2865 100644 --- a/src-ui/src/app/services/settings.service.ts +++ b/src-ui/src/app/services/settings.service.ts @@ -274,7 +274,7 @@ export class SettingsService { public get allDisplayFields(): Array<{ id: DisplayField; name: string }> { return this._allDisplayFields } - public displayFieldsInitialized: boolean = false + public displayFieldsInit: EventEmitter = new EventEmitter() constructor( rendererFactory: RendererFactory2, @@ -382,10 +382,10 @@ export class SettingsService { } }) ) - this.displayFieldsInitialized = true + this.displayFieldsInit.emit(true) }) } else { - this.displayFieldsInitialized = true + this.displayFieldsInit.emit(true) } }