]> git.arvados.org - arvados.git/blob - services/workbench2/src/store/api-client-authorizations/api-client-authorizations-middleware-service.ts
Merge branch 'main' of git.arvados.org:arvados into 22202-delete-process-navigation
[arvados.git] / services / workbench2 / src / store / api-client-authorizations / api-client-authorizations-middleware-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { ServiceRepository } from 'services/services';
6 import { MiddlewareAPI, Dispatch } from 'redux';
7 import { DataExplorerMiddlewareService, dataExplorerToListParams, getOrder, listResultsToDataExplorerItemsMeta } from 'store/data-explorer/data-explorer-middleware-service';
8 import { RootState } from 'store/store';
9 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
10 import { DataExplorer, getDataExplorer } from 'store/data-explorer/data-explorer-reducer';
11 import { updateResources } from 'store/resources/resources-actions';
12 import { apiClientAuthorizationsActions } from 'store/api-client-authorizations/api-client-authorizations-actions';
13 import { ListArguments, ListResults } from 'services/common-service/common-service';
14 import { ApiClientAuthorization } from 'models/api-client-authorization';
15 import { couldNotFetchItemsAvailable } from 'store/data-explorer/data-explorer-action';
16
17 export class ApiClientAuthorizationMiddlewareService extends DataExplorerMiddlewareService {
18     constructor(private services: ServiceRepository, id: string) {
19         super(id);
20     }
21
22     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
23         const state = api.getState();
24         const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
25         try {
26             const response = await this.services.apiClientAuthorizationService.list(getParams(dataExplorer));
27             api.dispatch(updateResources(response.items));
28             api.dispatch(setItems(response));
29         } catch {
30             api.dispatch(couldNotFetchLinks());
31         }
32     }
33
34     async requestCount(api: MiddlewareAPI<Dispatch, RootState>, criteriaChanged?: boolean, background?: boolean) {
35         if (criteriaChanged) {
36             // Get itemsAvailable
37             return this.services.apiClientAuthorizationService.list(getCountParams())
38                 .then((results: ListResults<ApiClientAuthorization>) => {
39                     if (results.itemsAvailable !== undefined) {
40                         api.dispatch<any>(apiClientAuthorizationsActions.SET_ITEMS_AVAILABLE(results.itemsAvailable));
41                     } else {
42                         couldNotFetchItemsAvailable();
43                     }
44                 });
45         }
46     }
47 }
48
49 const getParams = (dataExplorer: DataExplorer): ListArguments => ({
50     ...dataExplorerToListParams(dataExplorer),
51     order: getOrder<ApiClientAuthorization>(dataExplorer),
52     count: 'none',
53 });
54
55 const getCountParams = (): ListArguments => ({
56     limit: 0,
57     count: 'exact',
58 });
59
60 export const setItems = (listResults: ListResults<ApiClientAuthorization>) =>
61     apiClientAuthorizationsActions.SET_ITEMS({
62         ...listResultsToDataExplorerItemsMeta(listResults),
63         items: listResults.items.map(resource => resource.uuid),
64     });
65
66 const couldNotFetchLinks = () =>
67     snackbarActions.OPEN_SNACKBAR({
68         message: 'Could not fetch api client authorizations.',
69         kind: SnackbarKind.ERROR
70     });