refs #master Merge branch 'origin/master' into 13856-upload-component
[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 { 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 handleAction = <T extends { id: string }>(handler: (data: T) => void) =>
12         (data: T) => {
13             if (data.id === service.getId()) {
14                 handler(data);
15             }
16         };
17     const actions = bindDataExplorerActions(service.getId());
18
19     return action => {
20         dataExplorerActions.match(action, {
21             SET_PAGE: handleAction(() => {
22                 api.dispatch(actions.REQUEST_ITEMS());
23             }),
24             SET_ROWS_PER_PAGE: handleAction(() => {
25                 api.dispatch(actions.REQUEST_ITEMS());
26             }),
27             SET_FILTERS: handleAction(() => {
28                 api.dispatch(actions.RESET_PAGINATION());
29                 api.dispatch(actions.REQUEST_ITEMS());
30             }),
31             TOGGLE_SORT: handleAction(() => {
32                 api.dispatch(actions.REQUEST_ITEMS());
33             }),
34             SET_SEARCH_VALUE: handleAction(() => {
35                 api.dispatch(actions.RESET_PAGINATION());
36                 api.dispatch(actions.REQUEST_ITEMS());
37             }),
38             REQUEST_ITEMS: handleAction(() => {
39                 service.requestItems(api);
40             }),
41             default: () => next(action)
42         });
43     };
44 };