c1893f6da1022cc07c7fd7a369099cbd43fe5d73
[arvados-workbench2.git] / src / store / collections-content-address-panel / collections-content-address-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 } 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 { getDataExplorer } from '~/store/data-explorer/data-explorer-reducer';
11 import { resourcesActions } 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 { getSortColumn } from "~/store/data-explorer/data-explorer-reducer";
16 import { FavoritePanelColumnNames } from '~/views/favorite-panel/favorite-panel';
17 import { GroupContentsResource, GroupContentsResourcePrefix } from '~/services/groups-service/groups-service';
18 import { progressIndicatorActions } from '~/store/progress-indicator/progress-indicator-actions';
19 import { collectionsContentAddressActions } from './collections-content-address-panel-actions';
20 import { navigateTo } from '~/store/navigation/navigation-action';
21
22 export class CollectionsWithSameContentAddressMiddlewareService extends DataExplorerMiddlewareService {
23     constructor(private services: ServiceRepository, id: string) {
24         super(id);
25     }
26
27     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
28         const dataExplorer = getDataExplorer(api.getState().dataExplorer, this.getId());
29         if (!dataExplorer) {
30             api.dispatch(collectionPanelDataExplorerIsNotSet());
31         } else {
32             const sortColumn = getSortColumn(dataExplorer);
33
34             const contentOrder = new OrderBuilder<GroupContentsResource>();
35
36             if (sortColumn && sortColumn.name === FavoritePanelColumnNames.NAME) {
37                 const direction = sortColumn.sortDirection === SortDirection.ASC
38                     ? OrderDirection.ASC
39                     : OrderDirection.DESC;
40
41                 contentOrder
42                     .addOrder(direction, "name", GroupContentsResourcePrefix.COLLECTION);
43             }
44             try {
45                 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
46                 const pathname = api.getState().router.location!.pathname;
47                 const contentAddress = pathname.split('/')[2];
48                 const response = await this.services.collectionService.list({
49                     limit: dataExplorer.rowsPerPage,
50                     offset: dataExplorer.page * dataExplorer.rowsPerPage,
51                     filters: new FilterBuilder()
52                         .addEqual('portableDataHash', contentAddress)
53                         .getFilters()
54                 });
55                 if (response.itemsAvailable === 1) {
56                     api.dispatch<any>(navigateTo(response.items[0].uuid));
57                     api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
58                 } else {
59                     api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
60                     api.dispatch(resourcesActions.SET_RESOURCES(response.items));
61                     api.dispatch(collectionsContentAddressActions.SET_ITEMS({
62                         items: response.items.map((resource: any) => resource.uuid),
63                         itemsAvailable: response.itemsAvailable,
64                         page: Math.floor(response.offset / response.limit),
65                         rowsPerPage: response.limit
66                     }));
67                 }
68             } catch (e) {
69                 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
70                 api.dispatch(collectionsContentAddressActions.SET_ITEMS({
71                     items: [],
72                     itemsAvailable: 0,
73                     page: 0,
74                     rowsPerPage: dataExplorer.rowsPerPage
75                 }));
76                 api.dispatch(couldNotFetchCollections());
77             }
78         }
79     }
80 }
81
82 const collectionPanelDataExplorerIsNotSet = () =>
83     snackbarActions.OPEN_SNACKBAR({
84         message: 'Collection panel is not ready.',
85         kind: SnackbarKind.ERROR
86     });
87
88 const couldNotFetchCollections = () =>
89     snackbarActions.OPEN_SNACKBAR({
90         message: 'Could not fetch collection with this content address.',
91         kind: SnackbarKind.ERROR
92     });