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 }) => {
54 getState: () => RootState,
55 services: ServiceRepository
58 let de = getDataExplorer(
59 getState().dataExplorer,
62 switch (de.requestState) {
63 case DataTableRequestState.IDLE:
64 // Start a new request.
67 actions.SET_REQUEST_STATE({
68 requestState: DataTableRequestState.PENDING,
71 await service.requestItems(api, criteriaChanged);
74 actions.SET_REQUEST_STATE({
75 requestState: DataTableRequestState.NEED_REFRESH,
79 // Now check if the state is still PENDING, if it moved to NEED_REFRESH
80 // then we need to reissue requestItems
82 getState().dataExplorer,
86 de.requestState === DataTableRequestState.PENDING;
88 actions.SET_REQUEST_STATE({
89 requestState: DataTableRequestState.IDLE,
96 case DataTableRequestState.PENDING:
97 // State is PENDING, move it to NEED_REFRESH so that when the current request finishes it starts a new one.
99 actions.SET_REQUEST_STATE({
100 requestState: DataTableRequestState.NEED_REFRESH,
104 case DataTableRequestState.NEED_REFRESH:
105 // Nothing to do right now.
112 default: () => next(action),