Skip to content

Commit 5b7684b

Browse files
committed
chore: update formatter logic
1 parent 68e9b81 commit 5b7684b

File tree

4 files changed

+23
-46
lines changed

4 files changed

+23
-46
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,25 @@
1-
import { disposeBatch } from "@mendix/widget-plugin-mobx-kit/disposeBatch";
2-
import { ISetupable } from "@mendix/widget-plugin-mobx-kit/setupable";
3-
import { FilterCondition } from "mendix/filters";
4-
import { isAnd, isTag } from "../condition-utils";
51
import { FilterAPI } from "../context";
62
import { NumberInputFilterStore } from "../stores/input/NumberInputFilterStore";
73
import { Number_InputFilterInterface } from "../typings/InputFilterInterface";
4+
import { BaseStoreProvider } from "./BaseStoreProvider";
85
import { FilterSpec } from "./typings";
96

10-
export class NumberStoreProvider implements ISetupable {
11-
private _store: NumberInputFilterStore;
7+
export class NumberStoreProvider extends BaseStoreProvider<NumberInputFilterStore> {
8+
protected _store: NumberInputFilterStore;
9+
protected filterAPI: FilterAPI;
1210
readonly dataKey: string;
1311

14-
constructor(private filterAPI: FilterAPI, spec: FilterSpec<Big>) {
12+
constructor(filterAPI: FilterAPI, spec: FilterSpec<Big>) {
13+
super();
14+
this.filterAPI = filterAPI;
1515
this.dataKey = spec.dataKey;
1616
this._store = new NumberInputFilterStore(
1717
spec.attributes,
1818
this.findInitFilter(filterAPI.sharedInitFilter, this.dataKey)
1919
);
2020
}
2121

22-
private findInitFilter(conditions: Array<FilterCondition | undefined>, key: string): FilterCondition | null {
23-
for (const cond of conditions) {
24-
if (cond && isAnd(cond)) {
25-
const [tag, initFilter] = cond.args;
26-
if (isTag(tag) && tag.arg1.value === key) {
27-
return initFilter;
28-
}
29-
}
30-
}
31-
32-
return null;
33-
}
34-
3522
get store(): Number_InputFilterInterface {
3623
return this._store;
3724
}
38-
39-
setup(): () => void {
40-
const [add, disposeAll] = disposeBatch();
41-
this.filterAPI.filterObserver.observe(this.dataKey, this._store);
42-
add(() => this.filterAPI.filterObserver.unobserve(this.dataKey));
43-
add(this.store.setup?.());
44-
45-
return disposeAll;
46-
}
4725
}

packages/shared/widget-plugin-filtering/src/stores/input/NumberInputFilterStore.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ import { FilterData, InputData } from "../../typings/settings";
99
import { NumberArgument } from "./Argument";
1010
import { BaseInputFilterStore } from "./BaseInputFilterStore";
1111
import { baseNames } from "./fn-mappers";
12-
import { getFormatter } from "./store-utils";
1312

1413
type NumFns = FilterFunctionGeneric | FilterFunctionNonValue | FilterFunctionBinary;
15-
type Formatter = ListAttributeValue<Big>["formatter"];
14+
type Formatter = SimpleFormatter<Big>;
1615
type AttrMeta = AttributeMetaData<Big> & { formatter?: SimpleFormatter<Big> };
1716

1817
export class NumberInputFilterStore
@@ -23,7 +22,7 @@ export class NumberInputFilterStore
2322
readonly type = "number";
2423

2524
constructor(attributes: AttrMeta[], initCond: FilterCondition | null) {
26-
let formatter = formatterFix(getFormatter<Big>(attributes[0]));
25+
const formatter = formatterFix(attributes[0].formatter);
2726
super(new NumberArgument(formatter), new NumberArgument(formatter), "equal", attributes);
2827
makeObservable(this, {
2928
updateProps: action,
@@ -80,11 +79,12 @@ export class NumberInputFilterStore
8079
}
8180
}
8281

83-
function formatterFix(formatter: Formatter): Formatter {
82+
function formatterFix(formatter: Formatter | undefined): Formatter {
8483
// Check formatter.parse to see if it is a valid formatter.
85-
if (formatter.parse("none")?.valid === false) {
84+
if (formatter && formatter.parse("none")?.valid === false) {
8685
return formatter;
8786
}
87+
8888
// Create a new formatter that will handle the autonumber values.
8989
return {
9090
format: (value: Big) => {

packages/shared/widget-plugin-filtering/src/stores/input/StringInputFilterStore.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { FilterData, InputData } from "../../typings/settings";
1313
import { StringArgument } from "./Argument";
1414
import { BaseInputFilterStore } from "./BaseInputFilterStore";
1515
import { baseNames } from "./fn-mappers";
16-
import { getFormatter } from "./store-utils";
1716

1817
type StrFns = FilterFunctionString | FilterFunctionGeneric | FilterFunctionNonValue | FilterFunctionBinary;
1918
type AttrMeta = AttributeMetaData<string> & { formatter?: SimpleFormatter<string> };
@@ -94,3 +93,13 @@ export class StringInputFilterStore
9493
this.isInitialized = true;
9594
}
9695
}
96+
97+
function getFormatter<T>(attr: { formatter?: SimpleFormatter<T> }): SimpleFormatter<T> {
98+
return (
99+
attr.formatter ??
100+
({
101+
format: v => v ?? "",
102+
parse: v => ({ valid: true, value: v ?? "" })
103+
} as SimpleFormatter<T>)
104+
);
105+
}

packages/shared/widget-plugin-filtering/src/stores/input/store-utils.ts

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ListAttributeValue, SimpleFormatter } from "mendix";
1+
import { ListAttributeValue } from "mendix";
22
import { FilterCondition } from "mendix/filters";
33
import {
44
Date_InputFilterInterface,
@@ -52,13 +52,3 @@ export function isStringFilter(store: InputFilterInterface): store is String_Inp
5252
export function isDateFilter(store: InputFilterInterface): store is Date_InputFilterInterface {
5353
return store.arg1.type === "date";
5454
}
55-
56-
export function getFormatter<T>(attr: { formatter?: SimpleFormatter<T> }): SimpleFormatter<T> {
57-
return (
58-
attr.formatter ??
59-
({
60-
format: v => v ?? "",
61-
parse: v => ({ valid: true, value: v ?? "" })
62-
} as SimpleFormatter<T>)
63-
);
64-
}

0 commit comments

Comments
 (0)