PR fixes, extract format start and finished dates
[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 response = await this.services.userService.list(getParams(dataExplorer));
31             api.dispatch(updateResources(response.items));
32             api.dispatch(setItems(response));
33         } catch {
34             api.dispatch(couldNotFetchUsers());
35         }
36     }
37 }
38
39 export const getParams = (dataExplorer: DataExplorer) => ({
40     ...dataExplorerToListParams(dataExplorer),
41     order: getOrder(dataExplorer),
42     filters: getFilters(dataExplorer)
43 });
44
45 export const getFilters = (dataExplorer: DataExplorer) => {
46     const filters = new FilterBuilder()
47         .addILike("username", dataExplorer.searchValue)
48         .getFilters();
49     return filters;
50 };
51
52 export const getOrder = (dataExplorer: DataExplorer) => {
53     const sortColumn = getSortColumn(dataExplorer);
54     const order = new OrderBuilder<UserResource>();
55     if (sortColumn) {
56         const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
57             ? OrderDirection.ASC
58             : OrderDirection.DESC;
59         const columnName = sortColumn && sortColumn.name === UserPanelColumnNames.LAST_NAME ? "lastName" : "firstName";
60         return order
61             .addOrder(sortDirection, columnName)
62             .getOrder();
63     } else {
64         return order.getOrder();
65     }
66 };
67
68 export const setItems = (listResults: ListResults<UserResource>) =>
69     userBindedActions.SET_ITEMS({
70         ...listResultsToDataExplorerItemsMeta(listResults),
71         items: listResults.items.map(resource => resource.uuid),
72     });
73
74 const couldNotFetchUsers = () =>
75     snackbarActions.OPEN_SNACKBAR({
76         message: 'Could not fetch users.',
77         kind: SnackbarKind.ERROR
78     });