1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch } from 'redux';
6 import { RootState } from 'store/store';
7 import { ServiceRepository } from 'services/services';
8 import { Middleware } from 'redux';
11 bindDataExplorerActions,
12 DataTableRequestState,
13 } from './data-explorer-action';
14 import { getDataExplorer } from './data-explorer-reducer';
15 import { DataExplorerMiddlewareService } from './data-explorer-middleware-service';
17 export const dataExplorerMiddleware =
18 (service: DataExplorerMiddlewareService): Middleware =>
21 const actions = bindDataExplorerActions(service.getId());
25 <T extends { id: string }>(handler: (data: T) => void) =>
28 if (data.id === service.getId()) {
32 dataExplorerActions.match(action, {
33 SET_PAGE: handleAction(() => {
34 api.dispatch(actions.REQUEST_ITEMS(false));
36 SET_ROWS_PER_PAGE: handleAction(() => {
37 api.dispatch(actions.REQUEST_ITEMS(true));
39 SET_FILTERS: handleAction(() => {
40 api.dispatch(actions.RESET_PAGINATION());
41 api.dispatch(actions.REQUEST_ITEMS(true));
43 TOGGLE_SORT: handleAction(() => {
44 api.dispatch(actions.REQUEST_ITEMS(true));
46 SET_EXPLORER_SEARCH_VALUE: handleAction(() => {
47 api.dispatch(actions.RESET_PAGINATION());
48 api.dispatch(actions.REQUEST_ITEMS(true));
50 REQUEST_ITEMS: handleAction(({ criteriaChanged, background }) => {
51 api.dispatch<any>(async (
53 getState: () => RootState,
54 services: ServiceRepository
57 let de = getDataExplorer(
58 getState().dataExplorer,
61 switch (de.requestState) {
62 case DataTableRequestState.IDLE:
63 // Start a new request.
66 actions.SET_REQUEST_STATE({
67 requestState: DataTableRequestState.PENDING,
70 await service.requestItems(api, criteriaChanged, background);
73 actions.SET_REQUEST_STATE({
74 requestState: DataTableRequestState.NEED_REFRESH,
78 // Now check if the state is still PENDING, if it moved to NEED_REFRESH
79 // then we need to reissue requestItems
81 getState().dataExplorer,
85 de.requestState === DataTableRequestState.PENDING;
87 actions.SET_REQUEST_STATE({
88 requestState: DataTableRequestState.IDLE,
95 case DataTableRequestState.PENDING:
96 // State is PENDING, move it to NEED_REFRESH so that when the current request finishes it starts a new one.
98 actions.SET_REQUEST_STATE({
99 requestState: DataTableRequestState.NEED_REFRESH,
103 case DataTableRequestState.NEED_REFRESH:
104 // Nothing to do right now.
110 default: () => next(action),