1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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';
17 export class ApiClientAuthorizationMiddlewareService extends DataExplorerMiddlewareService {
18 constructor(private services: ServiceRepository, id: string) {
22 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
23 const state = api.getState();
24 const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
26 const response = await this.services.apiClientAuthorizationService.list(getParams(dataExplorer));
27 api.dispatch(updateResources(response.items));
28 api.dispatch(setItems(response));
30 api.dispatch(couldNotFetchLinks());
34 async requestCount(api: MiddlewareAPI<Dispatch, RootState>, criteriaChanged?: boolean, background?: boolean) {
35 if (criteriaChanged) {
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));
42 couldNotFetchItemsAvailable();
49 const getParams = (dataExplorer: DataExplorer): ListArguments => ({
50 ...dataExplorerToListParams(dataExplorer),
51 order: getOrder<ApiClientAuthorization>(dataExplorer),
55 const getCountParams = (): ListArguments => ({
60 export const setItems = (listResults: ListResults<ApiClientAuthorization>) =>
61 apiClientAuthorizationsActions.SET_ITEMS({
62 ...listResultsToDataExplorerItemsMeta(listResults),
63 items: listResults.items.map(resource => resource.uuid),
66 const couldNotFetchLinks = () =>
67 snackbarActions.OPEN_SNACKBAR({
68 message: 'Could not fetch api client authorizations.',
69 kind: SnackbarKind.ERROR