Use bindDataExplorerActions in middleware, remove api field from service class, updat...
[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 "../../../node_modules/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     next(actions.SET_COLUMNS({ columns: service.getColumns() }));
13
14     return action => {
15         const handleAction = <T extends { id: string }>(handler: (data: T) => void) =>
16             (data: T) => {
17                 next(action);
18                 if (data.id === service.getId()) {
19                     handler(data);
20                 }
21             };
22         dataExplorerActions.match(action, {
23             SET_PAGE: handleAction(() => {
24                 api.dispatch(actions.REQUEST_ITEMS());
25             }),
26             SET_ROWS_PER_PAGE: handleAction(() => {
27                 api.dispatch(actions.REQUEST_ITEMS());
28             }),
29             SET_FILTERS: handleAction(() => {
30                 api.dispatch(actions.RESET_PAGINATION());
31                 api.dispatch(actions.REQUEST_ITEMS());
32             }),
33             TOGGLE_SORT: handleAction(() => {
34                 api.dispatch(actions.REQUEST_ITEMS());
35             }),
36             SET_SEARCH_VALUE: handleAction(() => {
37                 api.dispatch(actions.RESET_PAGINATION());
38                 api.dispatch(actions.REQUEST_ITEMS());
39             }),
40             REQUEST_ITEMS: handleAction(() => {
41                 service.requestItems(api);
42             }),
43             default: () => next(action)
44         });
45     };
46 };