Merge branch 'master' into 13887-extract-common-functionality-from-project-and-favori...
[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 "../../../node_modules/redux";
7 import { dataExplorerActions } from "./data-explorer-action";
8 import { DataExplorerMiddlewareService } from "./data-explorer-middleware-service";
9
10 export const dataExplorerMiddleware = (service: DataExplorerMiddlewareService): Middleware => api => next => {
11     service.Api = api;
12     next(dataExplorerActions.SET_COLUMNS({ id: service.Id, columns: service.Columns }));
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.Id) {
18                     handler(data);
19                 }
20             };
21         dataExplorerActions.match(action, {
22             SET_PAGE: handleAction(() => {
23                 api.dispatch(dataExplorerActions.REQUEST_ITEMS({ id: service.Id }));
24             }),
25             SET_ROWS_PER_PAGE: handleAction(() => {
26                 api.dispatch(dataExplorerActions.REQUEST_ITEMS({ id: service.Id }));
27             }),
28             SET_FILTERS: handleAction(() => {
29                 api.dispatch(dataExplorerActions.RESET_PAGINATION({ id: service.Id }));
30                 api.dispatch(dataExplorerActions.REQUEST_ITEMS({ id: service.Id }));
31             }),
32             TOGGLE_SORT: handleAction(() => {
33                 api.dispatch(dataExplorerActions.REQUEST_ITEMS({ id: service.Id }));
34             }),
35             SET_SEARCH_VALUE: handleAction(() => {
36                 api.dispatch(dataExplorerActions.RESET_PAGINATION({ id: service.Id }));
37                 api.dispatch(dataExplorerActions.REQUEST_ITEMS({ id: service.Id }));
38             }),
39             REQUEST_ITEMS: handleAction(() => {
40                 service.requestItems(api);
41             }),
42             default: () => next(action)
43         });
44     };
45 };