Merge branch '17114-side-panel-tree-as-list'
[arvados.git] / src / store / collections-content-address-panel / collections-content-address-middleware-service.ts
index 53c45d57fbeb666604231355048a3f5b62a8f53a..e18922a75ea0aa8a31c5aedb2f2a2b7be9625ef4 100644 (file)
@@ -6,6 +6,7 @@ import { ServiceRepository } from '~/services/services';
 import { MiddlewareAPI, Dispatch } from 'redux';
 import { DataExplorerMiddlewareService } from '~/store/data-explorer/data-explorer-middleware-service';
 import { RootState } from '~/store/store';
+import { getUserUuid } from "~/common/getuser";
 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
 import { getDataExplorer } from '~/store/data-explorer/data-explorer-reducer';
 import { resourcesActions } from '~/store/resources/resources-actions';
@@ -17,6 +18,13 @@ import { FavoritePanelColumnNames } from '~/views/favorite-panel/favorite-panel'
 import { GroupContentsResource, GroupContentsResourcePrefix } from '~/services/groups-service/groups-service';
 import { progressIndicatorActions } from '~/store/progress-indicator/progress-indicator-actions';
 import { collectionsContentAddressActions } from './collections-content-address-panel-actions';
+import { navigateTo } from '~/store/navigation/navigation-action';
+import { updateFavorites } from '~/store/favorites/favorites-actions';
+import { updatePublicFavorites } from '~/store/public-favorites/public-favorites-actions';
+import { setBreadcrumbs } from '../breadcrumbs/breadcrumbs-actions';
+import { ResourceKind, extractUuidKind } from '~/models/resource';
+import { ownerNameActions } from '~/store/owner-name/owner-name-actions';
+import { getUserDisplayName } from '~/models/user';
 
 export class CollectionsWithSameContentAddressMiddlewareService extends DataExplorerMiddlewareService {
     constructor(private services: ServiceRepository, id: string) {
@@ -42,23 +50,72 @@ export class CollectionsWithSameContentAddressMiddlewareService extends DataExpl
             }
             try {
                 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
+                const userUuid = getUserUuid(api.getState());
                 const pathname = api.getState().router.location!.pathname;
                 const contentAddress = pathname.split('/')[2];
                 const response = await this.services.collectionService.list({
                     limit: dataExplorer.rowsPerPage,
                     offset: dataExplorer.page * dataExplorer.rowsPerPage,
                     filters: new FilterBuilder()
-                        .addEqual('portableDataHash', contentAddress)
-                        .getFilters()
+                        .addEqual('portable_data_hash', contentAddress)
+                        .addILike("name", dataExplorer.searchValue)
+                        .getFilters(),
+                    includeOldVersions: true
                 });
-                api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
-                api.dispatch(resourcesActions.SET_RESOURCES(response.items));
-                api.dispatch(collectionsContentAddressActions.SET_ITEMS({
-                    items: response.items.map((resource: any) => resource.uuid),
-                    itemsAvailable: response.itemsAvailable,
-                    page: Math.floor(response.offset / response.limit),
-                    rowsPerPage: response.limit
-                }));
+                const userUuids = response.items.map(it => {
+                    if (extractUuidKind(it.ownerUuid) === ResourceKind.USER) {
+                        return it.ownerUuid;
+                    } else {
+                        return '';
+                    }
+                }
+                );
+                const groupUuids = response.items.map(it => {
+                    if (extractUuidKind(it.ownerUuid) === ResourceKind.GROUP) {
+                        return it.ownerUuid;
+                    } else {
+                        return '';
+                    }
+                });
+                const responseUsers = await this.services.userService.list({
+                    filters: new FilterBuilder()
+                        .addIn('uuid', userUuids)
+                        .getFilters(),
+                    count: "none"
+                });
+                const responseGroups = await this.services.groupsService.list({
+                    filters: new FilterBuilder()
+                        .addIn('uuid', groupUuids)
+                        .getFilters(),
+                    count: "none"
+                });
+                responseUsers.items.map(it => {
+                    api.dispatch<any>(ownerNameActions.SET_OWNER_NAME({
+                        name: it.uuid === userUuid
+                            ? 'User: Me'
+                            : `User: ${getUserDisplayName(it)}`,
+                        uuid: it.uuid
+                    }));
+                });
+                responseGroups.items.map(it => {
+                    api.dispatch<any>(ownerNameActions.SET_OWNER_NAME({ name: `Project: ${it.name}`, uuid: it.uuid }));
+                });
+                api.dispatch<any>(setBreadcrumbs([{ label: 'Projects', uuid: userUuid }]));
+                api.dispatch<any>(updateFavorites(response.items.map(item => item.uuid)));
+                api.dispatch<any>(updatePublicFavorites(response.items.map(item => item.uuid)));
+                if (response.itemsAvailable === 1) {
+                    api.dispatch<any>(navigateTo(response.items[0].uuid));
+                    api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
+                } else {
+                    api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
+                    api.dispatch(resourcesActions.SET_RESOURCES(response.items));
+                    api.dispatch(collectionsContentAddressActions.SET_ITEMS({
+                        items: response.items.map((resource: any) => resource.uuid),
+                        itemsAvailable: response.itemsAvailable,
+                        page: Math.floor(response.offset / response.limit),
+                        rowsPerPage: response.limit
+                    }));
+                }
             } catch (e) {
                 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
                 api.dispatch(collectionsContentAddressActions.SET_ITEMS({
@@ -83,4 +140,4 @@ const couldNotFetchCollections = () =>
     snackbarActions.OPEN_SNACKBAR({
         message: 'Could not fetch collection with this content address.',
         kind: SnackbarKind.ERROR
-    });
\ No newline at end of file
+    });