Fix: use static object for activedisplayfields to prevent changes (#8120)

This commit is contained in:
shamoon 2024-10-30 11:21:01 -07:00 committed by GitHub
parent 11cfa0871e
commit 56d296f04b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 33 additions and 17 deletions

View File

@ -599,14 +599,17 @@ describe('DocumentListViewService', () => {
it('should not filter out custom fields if settings not initialized', () => { it('should not filter out custom fields if settings not initialized', () => {
const customFields = ['custom_field_1', 'custom_field_2'] const customFields = ['custom_field_1', 'custom_field_2']
documentListViewService.displayFields = customFields as any documentListViewService.displayFields = customFields as any
settingsService.displayFieldsInitialized = false
expect(documentListViewService.displayFields).toEqual(customFields) expect(documentListViewService.displayFields).toEqual(customFields)
jest.spyOn(settingsService, 'allDisplayFields', 'get').mockReturnValue([ jest.spyOn(settingsService, 'allDisplayFields', 'get').mockReturnValue([
{ id: DisplayField.ADDED, name: 'Added' }, { id: DisplayField.ADDED, name: 'Added' },
{ id: DisplayField.TITLE, name: 'Title' }, { id: DisplayField.TITLE, name: 'Title' },
{ id: 'custom_field_1', name: 'Custom Field 1' }, { id: 'custom_field_1', name: 'Custom Field 1' },
] as any) ] 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']) expect(documentListViewService.displayFields).toEqual(['custom_field_1'])
}) })
}) })

View File

@ -20,6 +20,10 @@ import { paramsFromViewState, paramsToViewState } from '../utils/query-params'
import { DocumentService, SelectionData } from './rest/document.service' import { DocumentService, SelectionData } from './rest/document.service'
import { SettingsService } from './settings.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. * Captures the current state of the list view.
*/ */
@ -102,6 +106,8 @@ export class DocumentListViewService {
private _activeSavedViewId: number = null private _activeSavedViewId: number = null
private displayFieldsInitialized: boolean = false
get activeSavedViewId() { get activeSavedViewId() {
return this._activeSavedViewId return this._activeSavedViewId
} }
@ -134,6 +140,19 @@ export class DocumentListViewService {
localStorage.removeItem(DOCUMENT_LIST_SERVICE.CURRENT_VIEW_CONFIG) 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 { private defaultListViewState(): ListViewState {
@ -415,23 +434,17 @@ export class DocumentListViewService {
} }
get displayFields(): DisplayField[] { get displayFields(): DisplayField[] {
let fields = return this.activeListViewState.displayFields ?? LIST_DEFAULT_DISPLAY_FIELDS
this.activeListViewState.displayFields ?? }
DEFAULT_DISPLAY_FIELDS.map((f) => f.id)
if (!this.activeListViewState.displayFields) { set displayFields(fields: DisplayField[]) {
fields = fields.filter((f) => f !== DisplayField.ADDED) this.activeListViewState.displayFields = this.displayFieldsInitialized
} ? fields?.filter(
return this.settings.displayFieldsInitialized
? fields.filter(
(field) => (field) =>
this.settings.allDisplayFields.find((f) => f.id === field) !== this.settings.allDisplayFields.find((f) => f.id === field) !==
undefined undefined
) )
: fields : fields
}
set displayFields(fields: DisplayField[]) {
this.activeListViewState.displayFields = fields
this.saveDocumentListView() this.saveDocumentListView()
} }

View File

@ -274,7 +274,7 @@ export class SettingsService {
public get allDisplayFields(): Array<{ id: DisplayField; name: string }> { public get allDisplayFields(): Array<{ id: DisplayField; name: string }> {
return this._allDisplayFields return this._allDisplayFields
} }
public displayFieldsInitialized: boolean = false public displayFieldsInit: EventEmitter<boolean> = new EventEmitter()
constructor( constructor(
rendererFactory: RendererFactory2, rendererFactory: RendererFactory2,
@ -382,10 +382,10 @@ export class SettingsService {
} }
}) })
) )
this.displayFieldsInitialized = true this.displayFieldsInit.emit(true)
}) })
} else { } else {
this.displayFieldsInitialized = true this.displayFieldsInit.emit(true)
} }
} }