17074: Various DE middleware cleanup
[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
16 export class ApiClientAuthorizationMiddlewareService extends DataExplorerMiddlewareService {
17     constructor(private services: ServiceRepository, id: string) {
18         super(id);
19     }
20
21     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
22         const state = api.getState();
23         const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
24         try {
25             const response = await this.services.apiClientAuthorizationService.list(getParams(dataExplorer));
26             api.dispatch(updateResources(response.items));
27             api.dispatch(setItems(response));
28         } catch {
29             api.dispatch(couldNotFetchLinks());
30         }
31     }
32 }
33
34 const getParams = (dataExplorer: DataExplorer): ListArguments => ({
35     ...dataExplorerToListParams(dataExplorer),
36     order: getOrder<ApiClientAuthorization>(dataExplorer)
37 });
38
39 export const setItems = (listResults: ListResults<ApiClientAuthorization>) =>
40     apiClientAuthorizationsActions.SET_ITEMS({
41         ...listResultsToDataExplorerItemsMeta(listResults),
42         items: listResults.items.map(resource => resource.uuid),
43     });
44
45 const couldNotFetchLinks = () =>
46     snackbarActions.OPEN_SNACKBAR({
47         message: 'Could not fetch api client authorizations.',
48         kind: SnackbarKind.ERROR
49     });