Skip to content

Commit fe3aa3f

Browse files
committed
feat: change filters host
1 parent 60d7140 commit fe3aa3f

File tree

4 files changed

+46
-16
lines changed

4 files changed

+46
-16
lines changed

packages/shared/filter-commons/src/condition-utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ interface TagCond {
5555
readonly arg2: TagMarker;
5656
}
5757

58+
/** @deprecated use for unit tests only */
5859
export function tag(name: string): TagCond {
5960
return notEqual(literal(name), literal(MARKER)) as TagCond;
6061
}

packages/shared/widget-plugin-filtering/src/stores/generic/CombinedFilter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { FilterCondition } from "mendix/filters";
44
import { autorun, reaction } from "mobx";
55
import { fnv1aHash } from "../../utils/fnv-1a-hash";
66

7-
type ConditionWithMeta = {
7+
export type ConditionWithMeta = {
88
cond: FilterCondition | undefined;
99
meta: string;
1010
};
@@ -110,7 +110,7 @@ export class CombinedFilter {
110110
return { filter, bag, hash: filter ? this.filterHash(filter) : null };
111111
}
112112

113-
saveFilterMeta(hash: string | null, bag: MetaBag): void {
113+
private _saveFilterMeta(hash: string | null, bag: MetaBag): void {
114114
if (!hash) {
115115
return;
116116
}
@@ -131,7 +131,7 @@ export class CombinedFilter {
131131
this.clearFilterMeta(prev.hash);
132132
}
133133
if (next.hash) {
134-
this.saveFilterMeta(next.hash, next.bag);
134+
this._saveFilterMeta(next.hash, next.bag);
135135
}
136136
}
137137
)

packages/shared/widget-plugin-filtering/src/stores/generic/CustomFilterHost.ts

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
import { tag } from "@mendix/filter-commons/condition-utils";
1+
import { reduceMap, restoreMap } from "@mendix/filter-commons/condition-utils";
22
import { FilterData, FiltersSettingsMap, PlainJs, Serializable } from "@mendix/filter-commons/typings/settings";
33
import { FilterCondition } from "mendix/filters";
4-
import { and } from "mendix/filters/builders";
5-
import { autorun, makeAutoObservable } from "mobx";
4+
import { action, autorun, makeAutoObservable } from "mobx";
65
import { Filter, ObservableFilterHost } from "../../typings/ObservableFilterHost";
6+
import { ConditionWithMeta } from "./CombinedFilter";
77

88
export class CustomFilterHost implements ObservableFilterHost, Serializable {
99
private filters: Map<string, [store: Filter, dispose: () => void]> = new Map();
1010
private settingsBuffer: FiltersSettingsMap<string> = new Map();
11+
private _state: Map<string, FilterCondition | undefined> = new Map();
12+
13+
readonly metaKey = "custom-filter-host";
1114

1215
constructor() {
13-
makeAutoObservable(this);
16+
makeAutoObservable(this, {
17+
hydrate: action
18+
});
1419
}
1520

1621
get settings(): FiltersSettingsMap<string> {
@@ -21,22 +26,26 @@ export class CustomFilterHost implements ObservableFilterHost, Serializable {
2126
this.settingsBuffer = data;
2227
}
2328

24-
get conditions(): Array<FilterCondition | undefined> {
25-
return [...this.filters].map(([key, [{ condition }]]) => {
26-
return condition ? and(tag(key), condition) : undefined;
27-
});
28-
}
29-
3029
observe(key: string, filter: Filter): void {
3130
this.unobserve(key);
32-
const clear = autorun(() => {
31+
const clearSettingsSync = autorun(() => {
3332
if (this.settingsBuffer.has(key)) {
3433
filter.fromJSON(this.settingsBuffer.get(key));
3534
}
3635
});
36+
const clearStateSync = autorun(() => {
37+
this._state.set(key, filter.condition);
38+
});
39+
const skipInit = this.settingsBuffer.has(key);
40+
if (!skipInit && this._state.has(key)) {
41+
filter.fromViewState(this._state.get(key)!);
42+
}
43+
3744
const dispose = (): void => {
38-
clear();
45+
clearSettingsSync();
46+
clearStateSync();
3947
this.filters.delete(key);
48+
this._state.delete(key);
4049
};
4150
this.filters.set(key, [filter, dispose]);
4251
}
@@ -58,4 +67,24 @@ export class CustomFilterHost implements ObservableFilterHost, Serializable {
5867

5968
this.settings = new Map(data as Array<[string, FilterData]>);
6069
}
70+
71+
get condWithMeta(): ConditionWithMeta {
72+
const conditions: Record<string, FilterCondition | undefined> = {};
73+
74+
for (const [key, [filter]] of this.filters) {
75+
conditions[key] = filter.condition;
76+
}
77+
78+
const [cond, meta] = reduceMap(conditions);
79+
80+
return {
81+
cond,
82+
meta
83+
};
84+
}
85+
86+
hydrate({ cond, meta }: ConditionWithMeta): void {
87+
const map = restoreMap(cond, meta);
88+
this._state = new Map(Object.entries(map));
89+
}
6190
}

packages/shared/widget-plugin-filtering/src/typings/ObservableFilterHost.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import { FilterCondition } from "mendix/filters";
44
export interface Filter {
55
toJSON(): FilterData;
66
fromJSON(data: FilterData): void;
7+
fromViewState(data: FilterCondition): void;
78
condition: FilterCondition | undefined;
89
setup?: () => void | void;
910
}
1011

1112
export interface ObservableFilterHost {
12-
conditions: Array<FilterCondition | undefined>;
1313
get settings(): FiltersSettingsMap<string>;
1414
set settings(settings: FiltersSettingsMap<string>);
1515
observe(key: string, filter: Filter): void;

0 commit comments

Comments
 (0)