Skip to content

Commit 0b0566c

Browse files
feat(datagrid-web): replace isLoading with isFirstLoad and update related logic
1 parent 01ceaad commit 0b0566c

File tree

9 files changed

+45
-21
lines changed

9 files changed

+45
-21
lines changed

packages/pluggableWidgets/datagrid-web/src/Datagrid.editorPreview.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ export function preview(props: DatagridPreviewProps): ReactElement {
141141
cellEventsController={eventsController}
142142
checkboxEventsController={eventsController}
143143
focusController={focusController}
144+
isFirstLoad={false}
144145
isLoading={false}
145146
isFetchingNextBatch={false}
146147
loadingType="spinner"

packages/pluggableWidgets/datagrid-web/src/Datagrid.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,9 @@ const Container = observer((props: Props): ReactElement => {
118118
cellEventsController={cellEventsController}
119119
checkboxEventsController={checkboxEventsController}
120120
focusController={focusController}
121-
isLoading={rootStore.loaderCtrl.isLoading}
121+
isFirstLoad={rootStore.loaderCtrl.isFirstLoad}
122122
isFetchingNextBatch={rootStore.loaderCtrl.isFetchingNextBatch}
123+
isLoading={props.datasource.status === "loading"}
123124
loadingType={props.loadingType}
124125
columnsLoading={!columnsStore.loaded}
125126
refreshIndicator={props.refreshIndicator}

packages/pluggableWidgets/datagrid-web/src/components/GridBody.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ interface Props {
88
className?: string;
99
children?: React.ReactNode;
1010
loadingType: LoadingTypeEnum;
11+
isFirstLoad: boolean;
1112
isFetchingNextBatch?: boolean;
1213
columnsHidable: boolean;
1314
columnsSize: number;
@@ -19,6 +20,9 @@ export function GridBody(props: Props): ReactElement {
1920
const { children } = props;
2021

2122
const content = (): React.ReactElement => {
23+
if (props.isFirstLoad) {
24+
return <Loader {...props} rowsSize={props.rowsSize > 0 ? props.rowsSize : props.pageSize} />;
25+
}
2226
return (
2327
<Fragment>
2428
{children}

packages/pluggableWidgets/datagrid-web/src/components/Widget.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export interface WidgetProps<C extends GridColumn, T extends ObjectItem = Object
6666
cancelExportLabel?: string;
6767
selectRowLabel?: string;
6868
selectAllRowsLabel?: string;
69+
isFirstLoad: boolean;
6970
isLoading: boolean;
7071
isFetchingNextBatch: boolean;
7172
loadingType: LoadingTypeEnum;
@@ -164,6 +165,8 @@ const Main = observer(<C extends GridColumn>(props: WidgetProps<C>): ReactElemen
164165

165166
const selectionEnabled = selectActionHelper.selectionType !== "None";
166167

168+
const showRefreshIndicator = refreshIndicator && props.isLoading && !props.isFirstLoad;
169+
167170
return (
168171
<Fragment>
169172
{showTopBar && <WidgetTopBar>{pagination}</WidgetTopBar>}
@@ -192,9 +195,9 @@ const Main = observer(<C extends GridColumn>(props: WidgetProps<C>): ReactElemen
192195
isLoading={props.columnsLoading}
193196
preview={props.preview}
194197
/>
195-
{refreshIndicator && props.isLoading ? <RefreshIndicator /> : null}
198+
{showRefreshIndicator ? <RefreshIndicator /> : null}
196199
<GridBody
197-
// isLoading={props.isLoading}
200+
isFirstLoad={props.isFirstLoad}
198201
isFetchingNextBatch={props.isFetchingNextBatch}
199202
loadingType={props.loadingType}
200203
columnsHidable={columnsHidable}

packages/pluggableWidgets/datagrid-web/src/controllers/DerivedLoaderController.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ import { computed, makeObservable } from "mobx";
33
type DerivedLoaderControllerSpec = {
44
exp: { exporting: boolean };
55
cols: { loaded: boolean };
6-
query: { isFetchingNextBatch: boolean; isLoading: boolean; isRefreshing: boolean };
6+
query: { isFetchingNextBatch: boolean; isFirstLoad: boolean; isRefreshing: boolean };
77
};
88

99
export class DerivedLoaderController {
1010
constructor(private spec: DerivedLoaderControllerSpec) {
1111
makeObservable(this, {
12-
isLoading: computed,
12+
isFirstLoad: computed,
1313
isFetchingNextBatch: computed,
1414
isRefreshing: computed
1515
});
1616
}
1717

18-
get isLoading(): boolean {
18+
get isFirstLoad(): boolean {
1919
const { cols, exp, query } = this.spec;
2020
if (!cols.loaded) {
2121
return true;
@@ -25,7 +25,7 @@ export class DerivedLoaderController {
2525
return false;
2626
}
2727

28-
return query.isLoading;
28+
return query.isFirstLoad;
2929
}
3030

3131
get isFetchingNextBatch(): boolean {

packages/pluggableWidgets/datagrid-web/src/utils/test-utils.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export function mockWidgetProps(): WidgetProps<GridColumn, ObjectItem> {
105105
selectActionHelper: mockSelectionProps(),
106106
cellEventsController: { getProps: () => Object.create({}) },
107107
checkboxEventsController: { getProps: () => Object.create({}) },
108+
isFirstLoad: false,
108109
isLoading: false,
109110
isFetchingNextBatch: false,
110111
loadingType: "spinner",

packages/shared/widget-plugin-grid/src/__tests__/DatasourceController.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ describe("DatasourceController loading states", () => {
2121
provider.setProps({ datasource });
2222
});
2323

24-
it("isLoading returns true by default", () => {
25-
expect(controller.isLoading).toBe(true);
24+
it("isFirstLoad returns true by default", () => {
25+
expect(controller.isFirstLoad).toBe(true);
2626
});
2727

2828
it("refresh has no effect if ds is loading", () => {
@@ -39,13 +39,13 @@ describe("DatasourceController loading states", () => {
3939
provider.setProps({ datasource: list.loading() });
4040
expect(provider.gate.props.datasource.status).toBe("loading");
4141
expect(controller.isRefreshing).toBe(true);
42-
expect(controller.isLoading).toBe(false);
42+
expect(controller.isFirstLoad).toBe(true);
4343
});
4444

4545
it("isFetchingNextBatch returns true after setLimit call", () => {
4646
controller.setLimit(20);
4747
expect(controller.isFetchingNextBatch).toBe(true);
48-
expect(controller.isLoading).toBe(false);
48+
expect(controller.isFirstLoad).toBe(true);
4949
});
5050
});
5151

@@ -56,7 +56,7 @@ describe("DatasourceController loading states", () => {
5656
});
5757

5858
it("all loading states return false", () => {
59-
expect(controller.isLoading).toBe(false);
59+
expect(controller.isFirstLoad).toBe(true);
6060
expect(controller.isRefreshing).toBe(false);
6161
expect(controller.isFetchingNextBatch).toBe(false);
6262
});

packages/shared/widget-plugin-grid/src/query/DatasourceController.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { DerivedPropsGate } from "@mendix/widget-plugin-mobx-kit/props-gate";
22
import { ReactiveController, ReactiveControllerHost } from "@mendix/widget-plugin-mobx-kit/reactive-controller";
33
import { ListValue, ValueStatus } from "mendix";
4-
import { action, autorun, computed, IComputedValue, makeAutoObservable } from "mobx";
4+
import { action, autorun, computed, IComputedValue, makeAutoObservable, when } from "mobx";
55
import { QueryController } from "./query-controller";
66

77
type Gate = DerivedPropsGate<{ datasource: ListValue }>;
@@ -13,19 +13,27 @@ export class DatasourceController implements ReactiveController, QueryController
1313
private refreshing = false;
1414
private fetching = false;
1515
private pageSize = Infinity;
16+
private isLoaded = false;
1617

1718
constructor(host: ReactiveControllerHost, spec: DatasourceControllerSpec) {
1819
host.addController(this);
1920
this.gate = spec.gate;
2021

21-
type PrivateMembers = "resetFlags" | "updateFlags" | "setRefreshing" | "setFetching" | "pageSize";
22+
type PrivateMembers =
23+
| "resetFlags"
24+
| "updateFlags"
25+
| "setRefreshing"
26+
| "setFetching"
27+
| "pageSize"
28+
| "setIsLoaded";
2229
makeAutoObservable<this, PrivateMembers>(this, {
2330
setup: false,
2431
pageSize: false,
2532
updateFlags: action,
2633
resetFlags: action,
2734
setRefreshing: action,
28-
setFetching: action
35+
setFetching: action,
36+
setIsLoaded: action
2937
});
3038
}
3139

@@ -51,6 +59,10 @@ export class DatasourceController implements ReactiveController, QueryController
5159
this.fetching = value;
5260
}
5361

62+
private setIsLoaded(value: boolean): void {
63+
this.isLoaded = value;
64+
}
65+
5466
private resetLimit(): void {
5567
this.datasource.setLimit(this.pageSize);
5668
}
@@ -63,11 +75,8 @@ export class DatasourceController implements ReactiveController, QueryController
6375
return this.gate.props.datasource;
6476
}
6577

66-
get isLoading(): boolean {
67-
if (this.isRefreshing || this.isFetchingNextBatch) {
68-
return false;
69-
}
70-
return this.isDSLoading;
78+
get isFirstLoad(): boolean {
79+
return !this.isLoaded;
7180
}
7281

7382
get isRefreshing(): boolean {
@@ -105,6 +114,11 @@ export class DatasourceController implements ReactiveController, QueryController
105114
}
106115

107116
setup(): () => void {
117+
when(
118+
() => !this.isDSLoading,
119+
() => this.setIsLoaded(true)
120+
);
121+
108122
return autorun(() => {
109123
// Always use actions to set flags to avoid subscribing to them
110124
this.updateFlags(this.datasource.status);

packages/shared/widget-plugin-grid/src/query/query-controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface QueryController extends Pick<ListValue, Members> {
1515
refresh(): void;
1616
setPageSize(size: number): void;
1717
hasMoreItems: boolean;
18-
isLoading: boolean;
18+
isFirstLoad: boolean;
1919
isRefreshing: boolean;
2020
isFetchingNextBatch: boolean;
2121
}

0 commit comments

Comments
 (0)