19231: Add smaller page sizes (10 and 20 items) to load faster
[arvados-workbench2.git] / src / store / data-explorer / data-explorer-middleware.ts
1
2 // Copyright (C) The Arvados Authors. All rights reserved.
3 //
4 // SPDX-License-Identifier: AGPL-3.0
5
6 import { Dispatch } from 'redux';
7 import { RootState } from 'store/store';
8 import { ServiceRepository } from 'services/services';
9 import { Middleware } from "redux";
10 import { dataExplorerActions, bindDataExplorerActions, DataTableRequestState } from "./data-explorer-action";
11 import { getDataExplorer } from "./data-explorer-reducer";
12 import { DataExplorerMiddlewareService } from "./data-explorer-middleware-service";
13
14 export const dataExplorerMiddleware = (service: DataExplorerMiddlewareService): Middleware => api => next => {
15     const actions = bindDataExplorerActions(service.getId());
16
17     return action => {
18         const handleAction = <T extends { id: string }>(handler: (data: T) => void) =>
19             (data: T) => {
20                 next(action);
21                 if (data.id === service.getId()) {
22                     handler(data);
23                 }
24             };
25         dataExplorerActions.match(action, {
26             SET_PAGE: handleAction(() => {
27                 api.dispatch(actions.REQUEST_ITEMS(false));
28             }),
29             SET_ROWS_PER_PAGE: handleAction(() => {
30                 api.dispatch(actions.REQUEST_ITEMS(true));
31             }),
32             SET_FILTERS: handleAction(() => {
33                 api.dispatch(actions.RESET_PAGINATION());
34                 api.dispatch(actions.REQUEST_ITEMS(true));
35             }),
36             TOGGLE_SORT: handleAction(() => {
37                 api.dispatch(actions.REQUEST_ITEMS(true));
38             }),
39             SET_EXPLORER_SEARCH_VALUE: handleAction(() => {
40                 api.dispatch(actions.RESET_PAGINATION());
41                 api.dispatch(actions.REQUEST_ITEMS(true));
42             }),
43             REQUEST_ITEMS: handleAction(({ criteriaChanged }) => {
44                 api.dispatch<any>(async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
45                     while (true) {
46                         let de = getDataExplorer(getState().dataExplorer, service.getId());
47                         switch (de.requestState) {
48                             case DataTableRequestState.IDLE:
49                                 // Start a new request.
50                                 try {
51                                     dispatch(actions.SET_REQUEST_STATE({ requestState: DataTableRequestState.PENDING }));
52                                     await service.requestItems(api, criteriaChanged);
53                                 } catch {
54                                     dispatch(actions.SET_REQUEST_STATE({ requestState: DataTableRequestState.NEED_REFRESH }));
55                                 }
56                                 // Now check if the state is still PENDING, if it moved to NEED_REFRESH
57                                 // then we need to reissue requestItems
58                                 de = getDataExplorer(getState().dataExplorer, service.getId());
59                                 const complete = (de.requestState === DataTableRequestState.PENDING);
60                                 dispatch(actions.SET_REQUEST_STATE({ requestState: DataTableRequestState.IDLE }));
61                                 if (complete) {
62                                     return;
63                                 }
64                                 break;
65                             case DataTableRequestState.PENDING:
66                                 // State is PENDING, move it to NEED_REFRESH so that when the current request finishes it starts a new one.
67                                 dispatch(actions.SET_REQUEST_STATE({ requestState: DataTableRequestState.NEED_REFRESH }));
68                                 return;
69                             case DataTableRequestState.NEED_REFRESH:
70                                 // Nothing to do right now.
71                                 return;
72                         }
73                     }
74                 });
75             }),
76             default: () => next(action)
77         });
78     };
79 };