fixed-removing-selected-files
[arvados.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 { Middleware } from "redux";
7 import { dataExplorerActions, bindDataExplorerActions } from "./data-explorer-action";
8 import { DataExplorerMiddlewareService } from "./data-explorer-middleware-service";
9
10 export const dataExplorerMiddleware = (service: DataExplorerMiddlewareService): Middleware => api => next => {
11     const actions = bindDataExplorerActions(service.getId());
12
13     return action => {
14         const handleAction = <T extends { id: string }>(handler: (data: T) => void) =>
15             (data: T) => {
16                 next(action);
17                 if (data.id === service.getId()) {
18                     handler(data);
19                 }
20             };
21         dataExplorerActions.match(action, {
22             SET_PAGE: handleAction(() => {
23                 api.dispatch(actions.REQUEST_ITEMS(false));
24             }),
25             SET_ROWS_PER_PAGE: handleAction(() => {
26                 api.dispatch(actions.REQUEST_ITEMS(true));
27             }),
28             SET_FILTERS: handleAction(() => {
29                 api.dispatch(actions.RESET_PAGINATION());
30                 api.dispatch(actions.REQUEST_ITEMS(true));
31             }),
32             TOGGLE_SORT: handleAction(() => {
33                 api.dispatch(actions.REQUEST_ITEMS(true));
34             }),
35             SET_EXPLORER_SEARCH_VALUE: handleAction(() => {
36                 api.dispatch(actions.RESET_PAGINATION());
37                 api.dispatch(actions.REQUEST_ITEMS(true));
38             }),
39             REQUEST_ITEMS: handleAction(({ criteriaChanged }) => {
40                 service.requestItems(api, criteriaChanged);
41             }),
42             default: () => next(action)
43         });
44     };
45 };