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 { FilterBuilder } from '~/services/api/filter-builder';
13 import { SortDirection } from '~/components/data-table/data-column';
14 import { OrderDirection, OrderBuilder } from '~/services/api/order-builder';
15 import { ListResults } from '~/services/common-service/common-service';
16 import { userBindedActions } from '~/store/users/users-actions';
17 import { getSortColumn } from "~/store/data-explorer/data-explorer-reducer";
18 import { UserResource } from '~/models/user';
19 import { UserPanelColumnNames } from '~/views/user-panel/user-panel';
21 export class UserMiddlewareService extends DataExplorerMiddlewareService {
22 constructor(private services: ServiceRepository, id: string) {
26 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
27 const state = api.getState();
28 const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
30 const response = await this.services.userService.list(getParams(dataExplorer));
31 api.dispatch(updateResources(response.items));
32 api.dispatch(setItems(response));
34 api.dispatch(couldNotFetchUsers());
39 export const getParams = (dataExplorer: DataExplorer) => ({
40 ...dataExplorerToListParams(dataExplorer),
41 order: getOrder(dataExplorer),
42 filters: getFilters(dataExplorer)
45 export const getFilters = (dataExplorer: DataExplorer) => {
46 const filters = new FilterBuilder()
47 .addILike("username", dataExplorer.searchValue)
52 export const getOrder = (dataExplorer: DataExplorer) => {
53 const sortColumn = getSortColumn(dataExplorer);
54 const order = new OrderBuilder<UserResource>();
56 const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
58 : OrderDirection.DESC;
59 const columnName = sortColumn && sortColumn.name === UserPanelColumnNames.LAST_NAME ? "lastName" : "firstName";
61 .addOrder(sortDirection, columnName)
64 return order.getOrder();
68 export const setItems = (listResults: ListResults<UserResource>) =>
69 userBindedActions.SET_ITEMS({
70 ...listResultsToDataExplorerItemsMeta(listResults),
71 items: listResults.items.map(resource => resource.uuid),
74 const couldNotFetchUsers = () =>
75 snackbarActions.OPEN_SNACKBAR({
76 message: 'Could not fetch users.',
77 kind: SnackbarKind.ERROR