Merge branch 'master' into 16848-token-handling-improvements
[arvados.git] / src / store / users / user-panel-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, 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';
20
21 export class UserMiddlewareService extends DataExplorerMiddlewareService {
22     constructor(private services: ServiceRepository, id: string) {
23         super(id);
24     }
25
26     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
27         const state = api.getState();
28         const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
29         try {
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));
34             } else {
35                 const responseLastName = await this.services.userService.list(getParamsLastName(dataExplorer));
36                 api.dispatch(updateResources(responseLastName.items));
37                 api.dispatch(setItems(responseLastName));
38             }
39         } catch {
40             api.dispatch(couldNotFetchUsers());
41         }
42     }
43 }
44
45 const getParamsFirstName = (dataExplorer: DataExplorer) => ({
46     ...dataExplorerToListParams(dataExplorer),
47     order: getOrder(dataExplorer),
48     filters: getFiltersFirstName(dataExplorer)
49 });
50
51 const getParamsLastName = (dataExplorer: DataExplorer) => ({
52     ...dataExplorerToListParams(dataExplorer),
53     order: getOrder(dataExplorer),
54     filters: getFiltersLastName(dataExplorer)
55 });
56
57 const getFiltersFirstName = (dataExplorer: DataExplorer) => {
58     const filters = new FilterBuilder()
59         .addILike("first_name", dataExplorer.searchValue)
60         .getFilters();
61     return filters;
62 };
63
64 const getFiltersLastName = (dataExplorer: DataExplorer) => {
65     const filters = new FilterBuilder()
66         .addILike("last_name", dataExplorer.searchValue)
67         .getFilters();
68     return filters;
69 };
70
71 export const getOrder = (dataExplorer: DataExplorer) => {
72     const sortColumn = getSortColumn(dataExplorer);
73     const order = new OrderBuilder<UserResource>();
74     if (sortColumn) {
75         const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
76             ? OrderDirection.ASC
77             : OrderDirection.DESC;
78         const columnName = sortColumn && sortColumn.name === UserPanelColumnNames.LAST_NAME ? "lastName" : "firstName";
79         return order
80             .addOrder(sortDirection, columnName)
81             .getOrder();
82     } else {
83         return order.getOrder();
84     }
85 };
86
87 export const setItems = (listResults: ListResults<UserResource>) =>
88     userBindedActions.SET_ITEMS({
89         ...listResultsToDataExplorerItemsMeta(listResults),
90         items: listResults.items.map(resource => resource.uuid),
91     });
92
93 const couldNotFetchUsers = () =>
94     snackbarActions.OPEN_SNACKBAR({
95         message: 'Could not fetch users.',
96         kind: SnackbarKind.ERROR
97     });