Skip to content

Commit 8604706

Browse files
committed
fix: change how we pass init sort
1 parent 70bf587 commit 8604706

5 files changed

Lines changed: 58 additions & 50 deletions

File tree

packages/pluggableWidgets/dropdown-sort-web/src/components/__test__/DropdownSort.spec.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ const commonProps: DropdownSortContainerProps = {
1515

1616
const createAPI = (observer: SortStoreHost): SortAPI => ({
1717
version: 1,
18-
host: observer,
19-
initSortOrder: []
18+
host: observer
2019
});
2120

2221
const mockAttributes = (): AttributesType[] => [
@@ -45,9 +44,10 @@ describe("Dropdown Sort", () => {
4544
let attributes: DropdownSortContainerProps["attributes"];
4645
let api: SortAPI;
4746
beforeEach(() => {
48-
const host = new SortStoreHost();
47+
const host = new SortStoreHost({
48+
initSort: [[attrId("1"), "asc"]]
49+
});
4950
api = createAPI(host);
50-
api.initSortOrder = [[attrId("1"), "asc"]];
5151
attributes = mockAttributes();
5252
});
5353
it("loads correct values from attributes", () => {
@@ -78,9 +78,10 @@ describe("Dropdown Sort", () => {
7878
let attributes: DropdownSortContainerProps["attributes"];
7979
let api: SortAPI;
8080
beforeEach(() => {
81-
const host = new SortStoreHost();
81+
const host = new SortStoreHost({
82+
initSort: [[attrId("1"), "asc"]]
83+
});
8284
api = createAPI(host);
83-
api.initSortOrder = [[attrId("1"), "asc"]];
8485
attributes = mockAttributes();
8586
});
8687
it("loads correct default option", () => {
@@ -111,9 +112,10 @@ describe("Dropdown Sort", () => {
111112
let api: SortAPI;
112113
beforeEach(() => {
113114
delete (global as any)["com.mendix.widgets.web.UUID"];
114-
const host = new SortStoreHost();
115+
const host = new SortStoreHost({
116+
initSort: [[attrId("1"), "asc"]]
117+
});
115118
api = createAPI(host);
116-
api.initSortOrder = [[attrId("1"), "asc"]];
117119
attributes = mockAttributes();
118120
});
119121

packages/pluggableWidgets/gallery-web/src/stores/GalleryStore.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ export class GalleryStore extends BaseControllerHost {
7878

7979
this.sortAPI = {
8080
version: 1,
81-
host: this._sortHost,
82-
initSortOrder: spec.gate.props.datasource.sortOrder
81+
host: this._sortHost
8382
};
8483

8584
new RefreshController(this, {

packages/shared/widget-plugin-sorting/src/react/context.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ import { useConst } from "@mendix/widget-plugin-mobx-kit/react/useConst";
22
import { generateUUID } from "@mendix/widget-plugin-platform/framework/generate-uuid";
33
import { Context, createContext, useContext, useEffect } from "react";
44
import { SortStoreHost } from "../stores/SortStoreHost";
5-
import { SortInstruction } from "../types/store";
65
import { Result, error, value } from "./result-meta";
76

87
export interface SortAPI {
98
version: 1;
109
host: SortStoreHost;
11-
initSortOrder?: SortInstruction[];
1210
}
1311

1412
const SORT_PATH = "com.mendix.widgets.web.sortable.sortContext";

packages/shared/widget-plugin-sorting/src/react/hocs/withLinkedSortStore.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function withLinkedSortStore<P extends RequiredProps>(
2020
() =>
2121
new SortStoreProvider({
2222
host: props.sortAPI.host,
23-
initSortOrder: props.sortAPI.initSortOrder,
23+
initSortOrder: props.sortAPI.host.sortOrder,
2424
attributes: props.attributes
2525
})
2626
);
Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,77 @@
1-
import { PlainJs, Serializable } from "@mendix/filter-commons/typings/settings";
2-
import { action, computed, makeObservable, observable } from "mobx";
1+
import { action, autorun, computed, makeObservable, observable, runInAction } from "mobx";
32
import { ObservableSortStore, SortInstruction } from "../types/store";
43

5-
export class SortStoreHost implements Serializable {
6-
private _store: ObservableSortStore | null = null;
4+
export class SortStoreHost {
75
private _usedBy: string[] = [];
6+
private _state: SortInstruction[];
7+
private _cleanup: (() => void) | null = null;
88

9-
get sortOrder(): SortInstruction[] {
10-
return this._store?.sortOrder ?? [];
11-
}
9+
constructor(spec: { initSort?: SortInstruction[] } = {}) {
10+
this._state = spec.initSort ?? [];
1211

13-
constructor() {
14-
makeObservable<this, "_usedBy" | "_add" | "_remove">(this, {
12+
makeObservable<this, "_usedBy" | "_state" | "_setState">(this, {
13+
_state: observable.ref,
1514
sortOrder: computed,
1615
lock: action,
17-
_add: action,
18-
_remove: action,
1916
_usedBy: observable,
2017
usedBy: computed,
2118
observe: action,
22-
unobserve: action
19+
unobserve: action,
20+
_setState: action
2321
});
2422
}
2523

24+
private _setState(state: SortInstruction[]): void {
25+
this._state = state;
26+
}
27+
28+
get sortOrder(): SortInstruction[] {
29+
return this._state;
30+
}
31+
2632
observe(store: ObservableSortStore): void {
27-
this._store = store;
33+
if (this._cleanup) {
34+
this._cleanup();
35+
}
36+
this._cleanup = autorun(() => {
37+
this._setState(store.sortOrder);
38+
});
2839
}
2940

3041
unobserve(): void {
31-
this._store = null;
42+
this._cleanup?.();
43+
this._cleanup = null;
3244
}
3345

3446
lock(id: string): () => void {
35-
this._add(id);
36-
return () => this._remove(id);
37-
}
38-
39-
private _add(id: string): void {
4047
if (this._usedBy.indexOf(id) === -1) {
4148
this._usedBy.push(id);
4249
}
43-
}
44-
45-
private _remove(id: string): void {
46-
const index = this._usedBy.indexOf(id);
47-
if (index !== -1) {
48-
this._usedBy.splice(index, 1);
49-
}
50+
const remove = (): void => {
51+
runInAction(() => {
52+
const index = this._usedBy.indexOf(id);
53+
if (index !== -1) {
54+
this._usedBy.splice(index, 1);
55+
}
56+
});
57+
};
58+
return remove;
5059
}
5160

5261
get usedBy(): string | null {
5362
return this._usedBy.at(0) ?? null;
5463
}
5564

56-
toJSON(): PlainJs {
57-
return this._store ? this._store.toJSON() : null;
58-
}
65+
// toJSON(): PlainJs {
66+
// return this._store ? this._store.toJSON() : null;
67+
// }
5968

60-
fromJSON(data: PlainJs): void {
61-
if (data == null || !Array.isArray(data)) {
62-
return;
63-
}
64-
if (this._store) {
65-
this._store.fromJSON(data);
66-
}
67-
}
69+
// fromJSON(data: PlainJs): void {
70+
// if (data == null || !Array.isArray(data)) {
71+
// return;
72+
// }
73+
// if (this._store) {
74+
// this._store.fromJSON(data);
75+
// }
76+
// }
6877
}

0 commit comments

Comments
 (0)