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, 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 { getSortColumn } from "~/store/data-explorer/data-explorer-reducer";
13 import { apiClientAuthorizationsActions } from '~/store/api-client-authorizations/api-client-authorizations-actions';
14 import { OrderDirection, OrderBuilder } from '~/services/api/order-builder';
15 import { ListResults } from '~/services/common-service/common-service';
16 import { ApiClientAuthorization } from '~/models/api-client-authorization';
17 import { ApiClientAuthorizationPanelColumnNames } from '~/views/api-client-authorization-panel/api-client-authorization-panel-root';
18 import { SortDirection } from '~/components/data-table/data-column';
20 export class ApiClientAuthorizationMiddlewareService extends DataExplorerMiddlewareService {
21 constructor(private services: ServiceRepository, id: string) {
25 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
26 const state = api.getState();
27 const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
29 const response = await this.services.apiClientAuthorizationService.list(getParams(dataExplorer));
30 api.dispatch(updateResources(response.items));
31 api.dispatch(setItems(response));
33 api.dispatch(couldNotFetchLinks());
38 export const getParams = (dataExplorer: DataExplorer) => ({
39 ...dataExplorerToListParams(dataExplorer),
40 order: getOrder(dataExplorer)
43 const getOrder = (dataExplorer: DataExplorer) => {
44 const sortColumn = getSortColumn(dataExplorer);
45 const order = new OrderBuilder<ApiClientAuthorization>();
47 const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
49 : OrderDirection.DESC;
51 const columnName = sortColumn && sortColumn.name === ApiClientAuthorizationPanelColumnNames.UUID ? "uuid" : "updatedAt";
53 .addOrder(sortDirection, columnName)
56 return order.getOrder();
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