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 responseFirstName = await this.services.userService.list(getParamsFirstName(dataExplorer));
31 if (responseFirstName.itemsAvailable) {
32 api.dispatch(updateResources(responseFirstName.items));
33 api.dispatch(setItems(responseFirstName));
35 const responseLastName = await this.services.userService.list(getParamsLastName(dataExplorer));
36 api.dispatch(updateResources(responseLastName.items));
37 api.dispatch(setItems(responseLastName));
40 api.dispatch(couldNotFetchUsers());
45 const getParamsFirstName = (dataExplorer: DataExplorer) => ({
46 ...dataExplorerToListParams(dataExplorer),
47 order: getOrder(dataExplorer),
48 filters: getFiltersFirstName(dataExplorer)
51 const getParamsLastName = (dataExplorer: DataExplorer) => ({
52 ...dataExplorerToListParams(dataExplorer),
53 order: getOrder(dataExplorer),
54 filters: getFiltersLastName(dataExplorer)
57 const getFiltersFirstName = (dataExplorer: DataExplorer) => {
58 const filters = new FilterBuilder()
59 .addILike("first_name", dataExplorer.searchValue)
64 const getFiltersLastName = (dataExplorer: DataExplorer) => {
65 const filters = new FilterBuilder()
66 .addILike("last_name", dataExplorer.searchValue)
71 export const getOrder = (dataExplorer: DataExplorer) => {
72 const sortColumn = getSortColumn(dataExplorer);
73 const order = new OrderBuilder<UserResource>();
75 const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
77 : OrderDirection.DESC;
78 const columnName = sortColumn && sortColumn.name === UserPanelColumnNames.LAST_NAME ? "lastName" : "firstName";
80 .addOrder(sortDirection, columnName)
83 return order.getOrder();
87 export const setItems = (listResults: ListResults<UserResource>) =>
88 userBindedActions.SET_ITEMS({
89 ...listResultsToDataExplorerItemsMeta(listResults),
90 items: listResults.items.map(resource => resource.uuid),
93 const couldNotFetchUsers = () =>
94 snackbarActions.OPEN_SNACKBAR({
95 message: 'Could not fetch users.',
96 kind: SnackbarKind.ERROR