2 // Copyright (C) The Arvados Authors. All rights reserved.
4 // SPDX-License-Identifier: AGPL-3.0
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";
14 export const dataExplorerMiddleware = (service: DataExplorerMiddlewareService): Middleware => api => next => {
15 const actions = bindDataExplorerActions(service.getId());
18 const handleAction = <T extends { id: string }>(handler: (data: T) => void) =>
21 if (data.id === service.getId()) {
25 dataExplorerActions.match(action, {
26 SET_PAGE: handleAction(() => {
27 api.dispatch(actions.REQUEST_ITEMS(false));
29 SET_ROWS_PER_PAGE: handleAction(() => {
30 api.dispatch(actions.REQUEST_ITEMS(true));
32 SET_FILTERS: handleAction(() => {
33 api.dispatch(actions.RESET_PAGINATION());
34 api.dispatch(actions.REQUEST_ITEMS(true));
36 TOGGLE_SORT: handleAction(() => {
37 api.dispatch(actions.REQUEST_ITEMS(true));
39 SET_EXPLORER_SEARCH_VALUE: handleAction(() => {
40 api.dispatch(actions.RESET_PAGINATION());
41 api.dispatch(actions.REQUEST_ITEMS(true));
43 REQUEST_ITEMS: handleAction(({ criteriaChanged }) => {
44 api.dispatch<any>(async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
46 let de = getDataExplorer(getState().dataExplorer, service.getId());
47 switch (de.requestState) {
48 case DataTableRequestState.IDLE:
49 // Start a new request.
51 dispatch(actions.SET_REQUEST_STATE({ requestState: DataTableRequestState.PENDING }));
52 await service.requestItems(api, criteriaChanged);
54 dispatch(actions.SET_REQUEST_STATE({ requestState: DataTableRequestState.NEED_REFRESH }));
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 }));
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 }));
69 case DataTableRequestState.NEED_REFRESH:
70 // Nothing to do right now.
76 default: () => next(action)