1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { DataExplorerMiddlewareService, listResultsToDataExplorerItemsMeta, dataExplorerToListParams } from '../data-explorer/data-explorer-middleware-service';
6 import { ServiceRepository } from '~/services/services';
7 import { MiddlewareAPI, Dispatch } from 'redux';
8 import { RootState } from '~/store/store';
9 import { getDataExplorer, DataExplorer } from '~/store/data-explorer/data-explorer-reducer';
10 import { updateFavorites } from '~/store/favorites/favorites-actions';
11 import { updateResources } from '~/store/resources/resources-actions';
12 import { loadMissingProcessesInformation } from '~/store/project-panel/project-panel-middleware-service';
13 import { snackbarActions } from '~/store/snackbar/snackbar-actions';
14 import { sharedWithMePanelActions } from './shared-with-me-panel-actions';
15 import { ListResults } from '~/services/common-service/common-resource-service';
16 import { GroupContentsResource } from '~/services/groups-service/groups-service';
17 import { SortDirection } from '~/components/data-table/data-column';
18 import { OrderBuilder, OrderDirection } from '~/services/api/order-builder';
19 import { ProjectResource } from '~/models/project';
20 import { ProjectPanelColumnNames } from '~/views/project-panel/project-panel';
21 import { FilterBuilder } from '~/services/api/filter-builder';
23 export class SharedWithMeMiddlewareService extends DataExplorerMiddlewareService {
24 constructor(private services: ServiceRepository, id: string) {
28 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
29 const state = api.getState();
30 const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
32 const response = await this.services.groupsService.shared(getParams(dataExplorer));
33 api.dispatch<any>(updateFavorites(response.items.map(item => item.uuid)));
34 api.dispatch(updateResources(response.items));
35 await api.dispatch<any>(loadMissingProcessesInformation(response.items));
36 api.dispatch(setItems(response));
38 api.dispatch(couldNotFetchSharedItems());
43 export const getParams = (dataExplorer: DataExplorer) => ({
44 ...dataExplorerToListParams(dataExplorer),
45 order: getOrder(dataExplorer),
46 filters: getFilters(dataExplorer),
49 export const getFilters = (dataExplorer: DataExplorer) => {
50 const filters = new FilterBuilder()
51 .addILike("name", dataExplorer.searchValue)
53 return `[${filters}]`;
56 export const getOrder = (dataExplorer: DataExplorer) => {
57 const sortColumn = dataExplorer.columns.find(c => c.sortDirection !== SortDirection.NONE);
58 const order = new OrderBuilder<ProjectResource>();
60 const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
62 : OrderDirection.DESC;
63 const columnName = sortColumn && sortColumn.name === ProjectPanelColumnNames.NAME ? "name" : "createdAt";
65 .addOrder(sortDirection, columnName)
68 return order.getOrder();
72 export const setItems = (listResults: ListResults<GroupContentsResource>) =>
73 sharedWithMePanelActions.SET_ITEMS({
74 ...listResultsToDataExplorerItemsMeta(listResults),
75 items: listResults.items.map(resource => resource.uuid),
78 const couldNotFetchSharedItems = () =>
79 snackbarActions.OPEN_SNACKBAR({
80 message: 'Could not fetch shared items.'