22141: Refactoring to reduce circular import dependencies
[arvados.git] / services / workbench2 / src / store / data-explorer / data-explorer-action.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { unionize, ofType, UnionOf } from "common/unionize";
6 import { DataColumns } from "components/data-table/data-column";
7 import { DataTableFetchMode } from "components/data-table/data-table";
8 import { DataTableFilters } from "components/data-table-filters/data-table-filters";
9 import { SnackbarKind, snackbarActions } from "store/snackbar/snackbar-actions";
10
11 export enum DataTableRequestState {
12     IDLE,
13     PENDING,
14     NEED_REFRESH,
15 }
16
17 export const dataExplorerActions = unionize({
18     CLEAR: ofType<{ id: string }>(),
19     RESET_PAGINATION: ofType<{ id: string }>(),
20     SET_LOADING_ITEMS_AVAILABLE: ofType<{ id: string, loadingItemsAvailable: boolean }>(),
21     SET_ITEMS_AVAILABLE: ofType<{ id: string, itemsAvailable: number }>(),
22     RESET_ITEMS_AVAILABLE: ofType<{ id: string }>(),
23     REQUEST_ITEMS: ofType<{ id: string; criteriaChanged?: boolean, background?: boolean }>(),
24     REQUEST_COUNT: ofType<{ id: string; criteriaChanged?: boolean, background?: boolean }>(),
25     REQUEST_STATE: ofType<{ id: string; criteriaChanged?: boolean }>(),
26     SET_FETCH_MODE: ofType<{ id: string; fetchMode: DataTableFetchMode }>(),
27     SET_COLUMNS: ofType<{ id: string; columns: DataColumns<any, any> }>(),
28     SET_FILTERS: ofType<{ id: string; columnName: string; filters: DataTableFilters }>(),
29     SET_ITEMS: ofType<{ id: string; items: any[]; page: number; rowsPerPage: number; itemsAvailable?: number }>(),
30     APPEND_ITEMS: ofType<{ id: string; items: any[]; page: number; rowsPerPage: number; itemsAvailable?: number }>(),
31     SET_PAGE: ofType<{ id: string; page: number }>(),
32     SET_ROWS_PER_PAGE: ofType<{ id: string; rowsPerPage: number }>(),
33     TOGGLE_COLUMN: ofType<{ id: string; columnName: string }>(),
34     TOGGLE_SORT: ofType<{ id: string; columnName: string }>(),
35     SET_EXPLORER_SEARCH_VALUE: ofType<{ id: string; searchValue: string }>(),
36     RESET_EXPLORER_SEARCH_VALUE: ofType<{ id: string }>(),
37     SET_REQUEST_STATE: ofType<{ id: string; requestState: DataTableRequestState }>(),
38     SET_COUNT_REQUEST_STATE: ofType<{ id: string; countRequestState: DataTableRequestState }>(),
39     SET_IS_NOT_FOUND: ofType<{ id: string; isNotFound: boolean }>(),
40 });
41
42 export type DataExplorerAction = UnionOf<typeof dataExplorerActions>;
43
44 export const bindDataExplorerActions = (id: string) => ({
45     CLEAR: () => dataExplorerActions.CLEAR({ id }),
46     RESET_PAGINATION: () => dataExplorerActions.RESET_PAGINATION({ id }),
47     SET_LOADING_ITEMS_AVAILABLE: (loadingItemsAvailable: boolean) => dataExplorerActions.SET_LOADING_ITEMS_AVAILABLE({ id, loadingItemsAvailable }),
48     SET_ITEMS_AVAILABLE: (itemsAvailable: number) => dataExplorerActions.SET_ITEMS_AVAILABLE({ id, itemsAvailable }),
49     RESET_ITEMS_AVAILABLE: () => dataExplorerActions.RESET_ITEMS_AVAILABLE({ id }),
50     REQUEST_ITEMS: (criteriaChanged?: boolean, background?: boolean) => dataExplorerActions.REQUEST_ITEMS({ id, criteriaChanged, background }),
51     REQUEST_COUNT: (criteriaChanged?: boolean, background?: boolean) => dataExplorerActions.REQUEST_COUNT({ id, criteriaChanged, background }),
52     SET_FETCH_MODE: (payload: { fetchMode: DataTableFetchMode }) => dataExplorerActions.SET_FETCH_MODE({ ...payload, id }),
53     SET_COLUMNS: (payload: { columns: DataColumns<any, any> }) => dataExplorerActions.SET_COLUMNS({ ...payload, id }),
54     SET_FILTERS: (payload: { columnName: string; filters: DataTableFilters }) => dataExplorerActions.SET_FILTERS({ ...payload, id }),
55     SET_ITEMS: (payload: { items: any[]; page: number; rowsPerPage: number; itemsAvailable?: number }) =>
56         dataExplorerActions.SET_ITEMS({ ...payload, id }),
57     APPEND_ITEMS: (payload: { items: any[]; page: number; rowsPerPage: number; itemsAvailable?: number }) =>
58         dataExplorerActions.APPEND_ITEMS({ ...payload, id }),
59     SET_PAGE: (payload: { page: number }) => dataExplorerActions.SET_PAGE({ ...payload, id }),
60     SET_ROWS_PER_PAGE: (payload: { rowsPerPage: number }) => dataExplorerActions.SET_ROWS_PER_PAGE({ ...payload, id }),
61     TOGGLE_COLUMN: (payload: { columnName: string }) => dataExplorerActions.TOGGLE_COLUMN({ ...payload, id }),
62     TOGGLE_SORT: (payload: { columnName: string }) => dataExplorerActions.TOGGLE_SORT({ ...payload, id }),
63     SET_EXPLORER_SEARCH_VALUE: (payload: { searchValue: string }) => dataExplorerActions.SET_EXPLORER_SEARCH_VALUE({ ...payload, id }),
64     RESET_EXPLORER_SEARCH_VALUE: () => dataExplorerActions.RESET_EXPLORER_SEARCH_VALUE({ id }),
65     SET_REQUEST_STATE: (payload: { requestState: DataTableRequestState }) => dataExplorerActions.SET_REQUEST_STATE({ ...payload, id }),
66     SET_COUNT_REQUEST_STATE: (payload: { countRequestState: DataTableRequestState }) => dataExplorerActions.SET_COUNT_REQUEST_STATE({ ...payload, id }),
67     SET_IS_NOT_FOUND: (payload: { isNotFound: boolean }) => dataExplorerActions.SET_IS_NOT_FOUND({ ...payload, id }),
68 });
69
70 export type BoundDataExplorerActions = ReturnType<typeof bindDataExplorerActions>;
71
72 export const couldNotFetchItemsAvailable = () =>
73     snackbarActions.OPEN_SNACKBAR({
74         message: "Could not fetch total items.",
75         kind: SnackbarKind.ERROR,
76     });