Skip to content

Commit

Permalink
Update sort and filter at meeting switch (#2947)
Browse files Browse the repository at this point in the history
  • Loading branch information
reiterl authored Oct 27, 2023
1 parent e3be455 commit 2c0b95f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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';
Expand Down Expand Up @@ -177,6 +179,8 @@ export abstract class BaseSortListService<V extends BaseViewModel>

private initializationCount = 0;

private activeMeetingIdService = inject(ActiveMeetingIdService);

public constructor(
translate: TranslateService,
private store: StorageService,
Expand All @@ -203,6 +207,11 @@ export abstract class BaseSortListService<V extends BaseViewModel>
} else {
this._defaultDefinitionSubject.next(defaultDefinition);
}
this.activeMeetingIdService.meetingIdChanged.subscribe(event => {
if (event.nextMeetingId) {
this.setSortingAfterMeetingChange(event.nextMeetingId);
}
});
}

/**
Expand Down Expand Up @@ -360,50 +369,33 @@ export abstract class BaseSortListService<V extends BaseViewModel>
}

private async loadDefinition(): Promise<void> {
let [storedDefinition, sortProperty, sortAscending]: [OsSortingDefinition<V>, OsSortProperty<V>, 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<OsSortingDefinition<V>>(`sorting_` + this.storageKey),
this.store.get<OsSortProperty<V>>(`sorting_property_` + this.storageKey),
this.store.get<boolean>(`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<V>, boolean] = await Promise.all([
this.store.get<OsSortProperty<V>>(this.calcStorageKey(`sorting_property`, this.storageKey)),
this.store.get<boolean>(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<void> {
let [sortProperty, sortAscending]: [OsSortProperty<V>, boolean] = await Promise.all([
this.store.get<OsSortProperty<V>>(`sorting_property_${this.storageKey}_${meetingId}`),
this.store.get<boolean>(`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.
Expand All @@ -430,11 +422,11 @@ export abstract class BaseSortListService<V extends BaseViewModel>
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 {
Expand All @@ -443,4 +435,12 @@ export abstract class BaseSortListService<V extends BaseViewModel>
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}`;
}
}
15 changes: 12 additions & 3 deletions client/src/app/site/services/active-filters.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<V>(storageKey: string, filterDefinitions: OsFilter<V>[]): Promise<void> {
return await this.store.set(`filter_` + storageKey, filterDefinitions);
return await this.store.set(this.calcStorageKey(storageKey), filterDefinitions);
}

public load<V>(storageKey: string): Promise<OsFilter<V>[]> {
return this.store.get<OsFilter<V>[]>(`filter_` + storageKey);
return this.store.get<OsFilter<V>[]>(this.calcStorageKey(storageKey));
}

private calcStorageKey(storageKey: string): string {
const possibleMeetingId = this.activeMeetingIdService.meetingId;
if (possibleMeetingId) {
return `filter_${storageKey}_${possibleMeetingId}`;
}
return `filter_${storageKey}`;
}
}

0 comments on commit 2c0b95f

Please sign in to comment.