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, getFilters } from 'store/project-panel/project-panel-middleware-service';
13 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
14 import { sharedWithMePanelActions } from './shared-with-me-panel-actions';
15 import { ListResults } from 'services/common-service/common-service';
16 import { GroupContentsResource, GroupContentsResourcePrefix } 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 { getSortColumn } from "store/data-explorer/data-explorer-reducer";
22 import { updatePublicFavorites } from 'store/public-favorites/public-favorites-actions';
23 import { FilterBuilder } from 'services/api/filter-builder';
24 import { progressIndicatorActions } from 'store/progress-indicator/progress-indicator-actions';
26 export class SharedWithMeMiddlewareService extends DataExplorerMiddlewareService {
27 constructor(private services: ServiceRepository, id: string) {
31 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
32 const state = api.getState();
33 const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
35 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
36 const response = await this.services.groupsService
38 ...getParams(dataExplorer),
39 excludeHomeProject: true,
40 filters: new FilterBuilder().addDistinct('uuid', `${state.auth.config.uuidPrefix}-j7d0g-publicfavorites`).getFilters()
42 api.dispatch<any>(updateFavorites(response.items.map(item => item.uuid)));
43 api.dispatch<any>(updatePublicFavorites(response.items.map(item => item.uuid)));
44 api.dispatch(updateResources(response.items));
45 await api.dispatch<any>(loadMissingProcessesInformation(response.items));
46 api.dispatch(setItems(response));
48 api.dispatch(couldNotFetchSharedItems());
50 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
55 export const getParams = (dataExplorer: DataExplorer) => ({
56 ...dataExplorerToListParams(dataExplorer),
57 order: getOrder(dataExplorer),
58 filters: getFilters(dataExplorer),
61 export const getOrder = (dataExplorer: DataExplorer) => {
62 const sortColumn = getSortColumn(dataExplorer);
63 const order = new OrderBuilder<ProjectResource>();
65 const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
67 : OrderDirection.DESC;
68 const columnName = sortColumn && sortColumn.name === ProjectPanelColumnNames.NAME ? "name" : "createdAt";
69 if (columnName === 'name') {
71 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.COLLECTION)
72 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROCESS)
73 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROJECT)
77 .addOrder(sortDirection, columnName)
81 return order.getOrder();
85 export const setItems = (listResults: ListResults<GroupContentsResource>) =>
86 sharedWithMePanelActions.SET_ITEMS({
87 ...listResultsToDataExplorerItemsMeta(listResults),
88 items: listResults.items.map(resource => resource.uuid),
91 const couldNotFetchSharedItems = () =>
92 snackbarActions.OPEN_SNACKBAR({
93 message: 'Could not fetch shared items.',
94 kind: SnackbarKind.ERROR