1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { DataExplorerMiddlewareService, dataExplorerToListParams, listResultsToDataExplorerItemsMeta } from "store/data-explorer/data-explorer-middleware-service";
6 import { FavoritePanelColumnNames } from "views/favorite-panel/favorite-panel";
7 import { RootState } from "../store";
8 import { getUserUuid } from "common/getuser";
9 import { DataColumns } from "components/data-table/data-table";
10 import { ServiceRepository } from "services/services";
11 import { FilterBuilder } from "services/api/filter-builder";
12 import { updateFavorites } from "../favorites/favorites-actions";
13 import { favoritePanelActions } from "./favorite-panel-action";
14 import { Dispatch, MiddlewareAPI } from "redux";
15 import { resourcesActions } from "store/resources/resources-actions";
16 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
17 import { progressIndicatorActions } from 'store/progress-indicator/progress-indicator-actions';
18 import { DataExplorer, getDataExplorer } from "store/data-explorer/data-explorer-reducer";
19 import { loadMissingProcessesInformation } from "store/project-panel/project-panel-run-middleware-service";
20 import { getDataExplorerColumnFilters } from 'store/data-explorer/data-explorer-middleware-service';
21 import { serializeSimpleObjectTypeFilters } from '../resource-type-filters/resource-type-filters';
22 import { ResourceKind } from "models/resource";
23 import { LinkClass, LinkResource } from "models/link";
24 import { GroupContentsResource } from "services/groups-service/groups-service";
25 import { ListArguments, ListResults } from "services/common-service/common-service";
26 import { couldNotFetchItemsAvailable } from "store/data-explorer/data-explorer-action";
28 export class FavoritePanelMiddlewareService extends DataExplorerMiddlewareService {
29 constructor(private services: ServiceRepository, id: string) {
33 getTypeFilters(dataExplorer: DataExplorer) {
34 const columns = dataExplorer.columns as DataColumns<string, GroupContentsResource>;
35 return serializeSimpleObjectTypeFilters(getDataExplorerColumnFilters(columns, FavoritePanelColumnNames.TYPE));
38 getLinkFilters(dataExplorer: DataExplorer, uuid: string): string {
39 return new FilterBuilder()
40 .addEqual("link_class", LinkClass.STAR)
41 .addEqual('tail_uuid', uuid)
42 .addEqual('tail_kind', ResourceKind.USER)
43 .addIsA("head_uuid", this.getTypeFilters(dataExplorer))
47 getResourceFilters(dataExplorer: DataExplorer, uuids: string[]): string {
48 return new FilterBuilder()
50 .addILike("name", dataExplorer.searchValue)
51 .addIsA("uuid", this.getTypeFilters(dataExplorer))
55 getLinkParams(dataExplorer: DataExplorer, uuid: string): ListArguments {
57 ...dataExplorerToListParams(dataExplorer),
58 filters: this.getLinkFilters(dataExplorer, uuid),
63 getCountParams(dataExplorer: DataExplorer, uuid: string): ListArguments {
65 filters: this.getLinkFilters(dataExplorer, uuid),
71 async requestItems(api: MiddlewareAPI<Dispatch, RootState>, criteriaChanged?: boolean, background?: boolean) {
72 const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
73 const uuid = getUserUuid(api.getState());
75 api.dispatch(favoritesPanelDataExplorerIsNotSet());
76 } else if (!uuid || !uuid.length) {
80 if (!background) { api.dispatch(progressIndicatorActions.START_WORKING(this.getId())); }
83 const responseLinks = await this.services.linkService.list(this.getLinkParams(dataExplorer, uuid));
84 const uuids = responseLinks.items.map(it => it.headUuid);
86 const groupItems = await this.services.groupsService.list({
87 filters: this.getResourceFilters(dataExplorer, uuids),
89 const collectionItems = await this.services.collectionService.list({
90 filters: this.getResourceFilters(dataExplorer, uuids),
92 const processItems = await this.services.containerRequestService.list({
93 filters: this.getResourceFilters(dataExplorer, uuids),
96 const orderedItems = [
98 ...collectionItems.items,
102 api.dispatch(resourcesActions.SET_RESOURCES(orderedItems));
103 await api.dispatch<any>(loadMissingProcessesInformation(processItems.items));
104 api.dispatch(favoritePanelActions.SET_ITEMS({
105 ...listResultsToDataExplorerItemsMeta(responseLinks),
106 items: orderedItems.map((resource: any) => resource.uuid),
108 api.dispatch<any>(updateFavorites(uuids));
110 api.dispatch(favoritePanelActions.SET_ITEMS({
114 rowsPerPage: dataExplorer.rowsPerPage
116 api.dispatch(couldNotFetchFavoritesContents());
118 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
123 async requestCount(api: MiddlewareAPI<Dispatch, RootState>, criteriaChanged?: boolean, background?: boolean) {
124 const state = api.getState();
125 const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
126 const uuid = getUserUuid(api.getState());
128 if (criteriaChanged && uuid && uuid.length) {
129 // Get itemsAvailable
130 return this.services.linkService.list(this.getCountParams(dataExplorer, uuid))
131 .then((results: ListResults<LinkResource>) => {
132 if (results.itemsAvailable !== undefined) {
133 api.dispatch<any>(favoritePanelActions.SET_ITEMS_AVAILABLE(results.itemsAvailable));
135 couldNotFetchItemsAvailable();
142 const favoritesPanelDataExplorerIsNotSet = () =>
143 snackbarActions.OPEN_SNACKBAR({
144 message: 'Favorites panel is not ready.',
145 kind: SnackbarKind.ERROR
148 const couldNotFetchFavoritesContents = () =>
149 snackbarActions.OPEN_SNACKBAR({
150 message: 'Could not fetch favorites contents.',
151 kind: SnackbarKind.ERROR
154 const userNotAvailable = () =>
155 snackbarActions.OPEN_SNACKBAR({
156 message: 'User favorites not available.',
157 kind: SnackbarKind.ERROR