From 2c0b95f40f99f242b8d25ce9a5637ecedad720f4 Mon Sep 17 00:00:00 2001 From: reiterl Date: Fri, 27 Oct 2023 10:46:43 +0200 Subject: [PATCH] Update sort and filter at meeting switch (#2947) --- .../base-sort-list.service.ts | 90 +++++++++---------- .../site/services/active-filters.service.ts | 15 +++- 2 files changed, 57 insertions(+), 48 deletions(-) diff --git a/client/src/app/site/base/base-sort.service/base-sort-list.service.ts b/client/src/app/site/base/base-sort.service/base-sort-list.service.ts index 54dd0abd67..bb41484550 100644 --- a/client/src/app/site/base/base-sort.service/base-sort-list.service.ts +++ b/client/src/app/site/base/base-sort.service/base-sort-list.service.ts @@ -1,4 +1,4 @@ -import { Directive, Injector, ProviderToken } from '@angular/core'; +import { Directive, inject, Injector, ProviderToken } from '@angular/core'; import { TranslateService } from '@ngx-translate/core'; import { auditTime, @@ -9,9 +9,11 @@ import { isObservable, Observable } from 'rxjs'; +import { Id } from 'src/app/domain/definitions/key-types'; import { BaseRepository } from 'src/app/gateways/repositories/base-repository'; import { Deferred } from 'src/app/infrastructure/utils/promises'; import { deepCopy } from 'src/app/infrastructure/utils/transform-functions'; +import { ActiveMeetingIdService } from 'src/app/site/pages/meetings/services/active-meeting-id.service'; import { SortListService } from 'src/app/ui/modules/list/definitions/sort-service'; import { StorageService } from '../../../gateways/storage.service'; @@ -177,6 +179,8 @@ export abstract class BaseSortListService private initializationCount = 0; + private activeMeetingIdService = inject(ActiveMeetingIdService); + public constructor( translate: TranslateService, private store: StorageService, @@ -203,6 +207,11 @@ export abstract class BaseSortListService } else { this._defaultDefinitionSubject.next(defaultDefinition); } + this.activeMeetingIdService.meetingIdChanged.subscribe(event => { + if (event.nextMeetingId) { + this.setSortingAfterMeetingChange(event.nextMeetingId); + } + }); } /** @@ -360,50 +369,33 @@ export abstract class BaseSortListService } private async loadDefinition(): Promise { - let [storedDefinition, sortProperty, sortAscending]: [OsSortingDefinition, OsSortProperty, boolean] = - await Promise.all([ - // TODO: Remove the sorting definition loading part and everything caused by 'transformDeprecated' at a later date, it is only here for backwards compatibility - this.store.get>(`sorting_` + this.storageKey), - this.store.get>(`sorting_property_` + this.storageKey), - this.store.get(`sorting_ascending_` + this.storageKey) - ]); - - const transformDeprecated = !!storedDefinition; - if (transformDeprecated) { - this.store.remove(`sorting_` + this.storageKey); - } - - if ((sortAscending ?? sortProperty) != null) { - storedDefinition = { - sortAscending, - sortProperty - }; - } - - if (storedDefinition) { - this.sortDefinition = storedDefinition; - } - - if (this.sortDefinition && this.sortDefinition.sortProperty) { - if (transformDeprecated) { - this.updateSortDefinitions(); - } else { - this.calculateDefaultStatus(); - this.updateSortedData(); - } - } else { - const defaultDef = await this.getDefaultDefinition(); - sortAscending = sortAscending ?? defaultDef.sortAscending; - sortProperty = sortProperty ?? defaultDef.sortProperty; - this.sortDefinition = { - sortAscending, - sortProperty - }; - this.updateSortDefinitions(); - } + let [sortProperty, sortAscending]: [OsSortProperty, boolean] = await Promise.all([ + this.store.get>(this.calcStorageKey(`sorting_property`, this.storageKey)), + this.store.get(this.calcStorageKey(`sorting_ascending`, this.storageKey)) + ]); + + const defaultDef = await this.getDefaultDefinition(); + sortAscending = sortAscending ?? defaultDef.sortAscending; + sortProperty = sortProperty ?? defaultDef.sortProperty; + this.sortDefinition = { + sortAscending, + sortProperty + }; + this.updateSortDefinitions(); this.hasLoaded.resolve(true); } + private async setSortingAfterMeetingChange(meetingId: Id): Promise { + let [sortProperty, sortAscending]: [OsSortProperty, boolean] = await Promise.all([ + this.store.get>(`sorting_property_${this.storageKey}_${meetingId}`), + this.store.get(`sorting_ascending_${this.storageKey}_${meetingId}`) + ]); + const defaultDef = await this.getDefaultDefinition(); + sortProperty = sortProperty ?? defaultDef.sortProperty; + sortAscending = sortAscending ?? defaultDef.sortAscending; + this.setSorting(sortProperty, sortAscending); + } + /** * Determines if the given properties are either the same property or both arrays of the same * properties. @@ -430,11 +422,11 @@ export abstract class BaseSortListService this.calculateDefaultStatus(); this.updateSortedData(); if (this._isDefaultSorting) { - this.store.remove(`sorting_property_` + this.storageKey); + this.store.remove(this.calcStorageKey(`sorting_property`, this.storageKey)); } else { - this.store.set(`sorting_property_` + this.storageKey, this.sortDefinition?.sortProperty); + this.store.set(this.calcStorageKey(`sorting_property`, this.storageKey), this.sortDefinition?.sortProperty); } - this.store.set(`sorting_ascending_` + this.storageKey, this.sortDefinition?.sortAscending); + this.store.set(this.calcStorageKey(`sorting_ascending`, this.storageKey), this.sortDefinition?.sortAscending); } private calculateDefaultStatus(): void { @@ -443,4 +435,12 @@ export abstract class BaseSortListService this._defaultDefinitionSubject.value?.sortProperty ); } + + private calcStorageKey(prefix: string, storageKey: string): string { + const possibleMeetingId = this.activeMeetingIdService.meetingId; + if (possibleMeetingId) { + return `${prefix}_${storageKey}_${possibleMeetingId}`; + } + return `${prefix}_${storageKey}`; + } } diff --git a/client/src/app/site/services/active-filters.service.ts b/client/src/app/site/services/active-filters.service.ts index 01099b15ef..27715cd28f 100644 --- a/client/src/app/site/services/active-filters.service.ts +++ b/client/src/app/site/services/active-filters.service.ts @@ -1,5 +1,6 @@ import { Injectable } from '@angular/core'; import { StorageService } from 'src/app/gateways/storage.service'; +import { ActiveMeetingIdService } from 'src/app/site/pages/meetings/services/active-meeting-id.service'; import { ActiveFiltersStoreService } from 'src/app/ui/modules/list/definitions'; import { OsFilter } from '../base/base-filter.service'; @@ -8,13 +9,21 @@ import { OsFilter } from '../base/base-filter.service'; providedIn: `root` }) export class ActiveFiltersService implements ActiveFiltersStoreService { - public constructor(private store: StorageService) {} + public constructor(private store: StorageService, private activeMeetingIdService: ActiveMeetingIdService) {} public async save(storageKey: string, filterDefinitions: OsFilter[]): Promise { - return await this.store.set(`filter_` + storageKey, filterDefinitions); + return await this.store.set(this.calcStorageKey(storageKey), filterDefinitions); } public load(storageKey: string): Promise[]> { - return this.store.get[]>(`filter_` + storageKey); + return this.store.get[]>(this.calcStorageKey(storageKey)); + } + + private calcStorageKey(storageKey: string): string { + const possibleMeetingId = this.activeMeetingIdService.meetingId; + if (possibleMeetingId) { + return `filter_${storageKey}_${possibleMeetingId}`; + } + return `filter_${storageKey}`; } }