|
| 1 | +import { compactArray, fromCompactArray, isAnd } from "@mendix/filter-commons/condition-utils"; |
| 2 | +import { createContextWithStub, FilterAPI } from "@mendix/widget-plugin-filtering/context"; |
| 3 | +import { CustomFilterHost } from "@mendix/widget-plugin-filtering/stores/generic/CustomFilterHost"; |
| 4 | +import { DatasourceController } from "@mendix/widget-plugin-grid/query/DatasourceController"; |
| 5 | +import { PaginationController } from "@mendix/widget-plugin-grid/query/PaginationController"; |
| 6 | +import { RefreshController } from "@mendix/widget-plugin-grid/query/RefreshController"; |
| 7 | +import { BaseControllerHost } from "@mendix/widget-plugin-mobx-kit/BaseControllerHost"; |
| 8 | +import { disposeBatch } from "@mendix/widget-plugin-mobx-kit/disposeBatch"; |
| 9 | +import { DerivedPropsGate } from "@mendix/widget-plugin-mobx-kit/props-gate"; |
| 10 | +import { ReactiveController, ReactiveControllerHost } from "@mendix/widget-plugin-mobx-kit/reactive-controller"; |
| 11 | +import { generateUUID } from "@mendix/widget-plugin-platform/framework/generate-uuid"; |
| 12 | +import { ListValue } from "mendix"; |
| 13 | +import { FilterCondition } from "mendix/filters"; |
| 14 | +import { makeAutoObservable, reaction } from "mobx"; |
| 15 | +import { PaginationEnum } from "../../typings/GalleryProps"; |
| 16 | + |
| 17 | +interface DynamicProps { |
| 18 | + datasource: ListValue; |
| 19 | +} |
| 20 | + |
| 21 | +interface StaticProps { |
| 22 | + pagination: PaginationEnum; |
| 23 | + showPagingButtons: "always" | "auto"; |
| 24 | + showTotalCount: boolean; |
| 25 | + pageSize: number; |
| 26 | +} |
| 27 | + |
| 28 | +type Gate = DerivedPropsGate<DynamicProps>; |
| 29 | + |
| 30 | +type GalleryStoreSpec = StaticProps & { |
| 31 | + gate: Gate; |
| 32 | +}; |
| 33 | + |
| 34 | +export class GalleryStore extends BaseControllerHost { |
| 35 | + private readonly _id: string; |
| 36 | + private readonly _query: DatasourceController; |
| 37 | + readonly paging: PaginationController; |
| 38 | + readonly filterAPI: FilterAPI; |
| 39 | + |
| 40 | + constructor(spec: GalleryStoreSpec) { |
| 41 | + super(); |
| 42 | + |
| 43 | + this._id = `GalleryStore@${generateUUID()}`; |
| 44 | + |
| 45 | + this._query = new DatasourceController(this, { gate: spec.gate }); |
| 46 | + |
| 47 | + this.paging = new PaginationController(this, { |
| 48 | + gate: undefined, |
| 49 | + query: this._query, |
| 50 | + pageSize: spec.pageSize, |
| 51 | + pagination: spec.pagination, |
| 52 | + showPagingButtons: spec.showPagingButtons, |
| 53 | + showTotalCount: true |
| 54 | + }); |
| 55 | + |
| 56 | + const filterObserver = new CustomFilterHost(); |
| 57 | + |
| 58 | + const paramCtrl = new QueryParamsController(this, this._query, filterObserver); |
| 59 | + |
| 60 | + this.filterAPI = createContextWithStub({ |
| 61 | + filterObserver, |
| 62 | + parentChannelName: this._id, |
| 63 | + sharedInitFilter: paramCtrl.unzipFilter(spec.gate.props.datasource.filter) |
| 64 | + }); |
| 65 | + |
| 66 | + new RefreshController(this, { |
| 67 | + delay: 0, |
| 68 | + query: this._query.derivedQuery |
| 69 | + }); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +class QueryParamsController implements ReactiveController { |
| 74 | + private readonly _query: DatasourceController; |
| 75 | + private readonly _filters: CustomFilterHost; |
| 76 | + |
| 77 | + constructor(host: ReactiveControllerHost, query: DatasourceController, filters: CustomFilterHost) { |
| 78 | + host.addController(this); |
| 79 | + |
| 80 | + this._query = query; |
| 81 | + this._filters = filters; |
| 82 | + |
| 83 | + makeAutoObservable(this, { setup: false }); |
| 84 | + } |
| 85 | + |
| 86 | + private get _derivedSortOrder(): ListValue["sortOrder"] { |
| 87 | + return []; |
| 88 | + } |
| 89 | + |
| 90 | + private get _derivedFilter(): FilterCondition { |
| 91 | + return compactArray(this._filters.conditions); |
| 92 | + } |
| 93 | + |
| 94 | + setup(): () => void { |
| 95 | + const [add, disposeAll] = disposeBatch(); |
| 96 | + add( |
| 97 | + reaction( |
| 98 | + () => this._derivedSortOrder, |
| 99 | + sortOrder => this._query.setSortOrder(sortOrder), |
| 100 | + { fireImmediately: true } |
| 101 | + ) |
| 102 | + ); |
| 103 | + add( |
| 104 | + reaction( |
| 105 | + () => this._derivedFilter, |
| 106 | + filter => this._query.setFilter(filter), |
| 107 | + { fireImmediately: true } |
| 108 | + ) |
| 109 | + ); |
| 110 | + |
| 111 | + return disposeAll; |
| 112 | + } |
| 113 | + |
| 114 | + unzipFilter(filter?: FilterCondition): Array<FilterCondition | undefined> { |
| 115 | + if (!filter || !isAnd(filter)) { |
| 116 | + return []; |
| 117 | + } |
| 118 | + return fromCompactArray(filter); |
| 119 | + } |
| 120 | +} |
0 commit comments