Merge branch '21893-input-collections-virtual-list' into main. Closes #21893
[arvados.git] / services / workbench2 / src / store / workbench / workbench-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Dispatch } from "redux";
6 import { RootState } from "store/store";
7 import { getUserUuid } from "common/getuser";
8 import { loadDetailsPanel } from "store/details-panel/details-panel-action";
9 import { snackbarActions, SnackbarKind } from "store/snackbar/snackbar-actions";
10 import { favoritePanelActions, loadFavoritePanel } from "store/favorite-panel/favorite-panel-action";
11 import { getProjectPanelCurrentUuid, setIsProjectPanelTrashed } from "store/project-panel/project-panel-action";
12 import { projectPanelDataActions, projectPanelRunActions } from "store/project-panel/project-panel-action-bind";
13 import {
14     activateSidePanelTreeItem,
15     initSidePanelTree,
16     loadSidePanelTreeProjects,
17     SidePanelTreeCategory,
18     SIDE_PANEL_TREE,
19 } from "store/side-panel-tree/side-panel-tree-actions";
20 import { updateResources } from "store/resources/resources-actions";
21 import { projectPanelDataColumns } from "views/project-panel/project-panel-data";
22 import { projectPanelRunColumns } from "views/project-panel/project-panel-run";
23 import { favoritePanelColumns } from "views/favorite-panel/favorite-panel";
24 import { matchRootRoute } from "routes/routes";
25 import {
26     setGroupDetailsBreadcrumbs,
27     setGroupsBreadcrumbs,
28     setProcessBreadcrumbs,
29     setSharedWithMeBreadcrumbs,
30     setSidePanelBreadcrumbs,
31     setTrashBreadcrumbs,
32     setUsersBreadcrumbs,
33     setMyAccountBreadcrumbs,
34     setUserProfileBreadcrumbs,
35     setInstanceTypesBreadcrumbs,
36     setVirtualMachinesBreadcrumbs,
37     setVirtualMachinesAdminBreadcrumbs,
38     setRepositoriesBreadcrumbs,
39 } from "store/breadcrumbs/breadcrumbs-actions";
40 import { navigateTo, navigateToRootProject } from "store/navigation/navigation-action";
41 import { MoveToFormDialogData } from "store/move-to-dialog/move-to-dialog";
42 import { ServiceRepository } from "services/services";
43 import { getResource } from "store/resources/resources";
44 import * as projectCreateActions from "store/projects/project-create-actions";
45 import * as projectMoveActions from "store/projects/project-move-actions";
46 import * as projectUpdateActions from "store/projects/project-update-actions";
47 import * as collectionCreateActions from "store/collections/collection-create-actions";
48 import * as collectionCopyActions from "store/collections/collection-copy-actions";
49 import * as collectionMoveActions from "store/collections/collection-move-actions";
50 import * as processMoveActions from "store/processes/process-move-actions";
51 import * as processUpdateActions from "store/processes/process-update-actions";
52 import * as processCopyActions from "store/processes/process-copy-actions";
53 import { trashPanelColumns } from "views/trash-panel/trash-panel";
54 import { loadTrashPanel, trashPanelActions } from "store/trash-panel/trash-panel-action";
55 import { loadProcess as loadProcessResources, loadProcessPanel } from "store/process-panel/process-panel-actions";
56 import { loadSharedWithMePanel, sharedWithMePanelActions } from "store/shared-with-me-panel/shared-with-me-panel-actions";
57 import { sharedWithMePanelColumns } from "views/shared-with-me-panel/shared-with-me-panel";
58 import { CopyFormDialogData } from "store/copy-dialog/copy-dialog";
59 import { workflowPanelActions } from "store/workflow-panel/workflow-panel-actions";
60 import { loadSshKeysPanel } from "store/auth/auth-action-ssh";
61 import { loadLinkAccountPanel, linkAccountPanelActions } from "store/link-account-panel/link-account-panel-actions";
62 import { loadSiteManagerPanel } from "store/auth/auth-action-session";
63 import { workflowPanelColumns } from "views/workflow-panel/workflow-panel-view";
64 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
65 import { getProgressIndicator } from "store/progress-indicator/progress-indicator-reducer";
66 import { extractUuidKind, Resource, ResourceKind } from "models/resource";
67 import { FilterBuilder } from "services/api/filter-builder";
68 import { GroupContentsResource } from "services/groups-service/groups-service";
69 import { MatchCases, ofType, unionize, UnionOf } from "common/unionize";
70 import { loadRunProcessPanel } from "store/run-process-panel/run-process-panel-actions";
71 import { collectionPanelActions, loadCollectionPanel } from "store/collection-panel/collection-panel-action";
72 import { CollectionResource } from "models/collection";
73 import { WorkflowResource } from "models/workflow";
74 import { loadSearchResultsPanel, searchResultsPanelActions } from "store/search-results-panel/search-results-panel-actions";
75 import { searchResultsPanelColumns } from "views/search-results-panel/search-results-panel-view";
76 import { loadVirtualMachinesPanel } from "store/virtual-machines/virtual-machines-actions";
77 import { loadRepositoriesPanel } from "store/repositories/repositories-actions";
78 import { loadKeepServicesPanel } from "store/keep-services/keep-services-actions";
79 import { loadUsersPanel, userBindedActions } from "store/users/users-actions";
80 import * as userProfilePanelActions from "store/user-profile/user-profile-actions";
81 import { linkPanelActions, loadLinkPanel } from "store/link-panel/link-panel-actions";
82 import { linkPanelColumns } from "views/link-panel/link-panel-root";
83 import { userPanelColumns } from "views/user-panel/user-panel";
84 import { loadApiClientAuthorizationsPanel, apiClientAuthorizationsActions } from "store/api-client-authorizations/api-client-authorizations-actions";
85 import { apiClientAuthorizationPanelColumns } from "views/api-client-authorization-panel/api-client-authorization-panel-root";
86 import * as groupPanelActions from "store/groups-panel/groups-panel-actions";
87 import { groupsPanelColumns } from "views/groups-panel/groups-panel";
88 import * as groupDetailsPanelActions from "store/group-details-panel/group-details-panel-actions";
89 import { groupDetailsMembersPanelColumns, groupDetailsPermissionsPanelColumns } from "views/group-details-panel/group-details-panel";
90 import { DataTableFetchMode } from "components/data-table/data-table";
91 import { loadPublicFavoritePanel, publicFavoritePanelActions } from "store/public-favorites-panel/public-favorites-action";
92 import { publicFavoritePanelColumns } from "views/public-favorites-panel/public-favorites-panel";
93 import {
94     loadCollectionsContentAddressPanel,
95     collectionsContentAddressActions,
96 } from "store/collections-content-address-panel/collections-content-address-panel-actions";
97 import { collectionContentAddressPanelColumns } from "views/collection-content-address-panel/collection-content-address-panel";
98 import { subprocessPanelActions } from "store/subprocess-panel/subprocess-panel-actions";
99 import { subprocessPanelColumns } from "views/subprocess-panel/subprocess-panel-root";
100 import { loadAllProcessesPanel, allProcessesPanelActions } from "../all-processes-panel/all-processes-panel-action";
101 import { allProcessesPanelColumns } from "views/all-processes-panel/all-processes-panel";
102 import { userProfileGroupsColumns } from "views/user-profile-panel/user-profile-panel-root";
103 import { selectedToArray, selectedToKindSet } from "components/multiselect-toolbar/MultiselectToolbar";
104 import { deselectOne } from "store/multiselect/multiselect-actions";
105 import { treePickerActions } from "store/tree-picker/tree-picker-actions";
106 import { workflowProcessesPanelColumns } from "views/workflow-panel/workflow-processes-panel-root";
107 import { workflowProcessesPanelActions } from "store/workflow-panel/workflow-panel-actions";
108
109 export const WORKBENCH_LOADING_SCREEN = "workbenchLoadingScreen";
110
111 export const isWorkbenchLoading = (state: RootState) => {
112     const progress = getProgressIndicator(WORKBENCH_LOADING_SCREEN)(state.progressIndicator);
113     return progress ? progress.working : false;
114 };
115
116 export const handleFirstTimeLoad = (action: any) => async (dispatch: Dispatch<any>, getState: () => RootState) => {
117     try {
118         await dispatch(action);
119     } catch (e) {
120         snackbarActions.OPEN_SNACKBAR({
121             message: "Error " + e,
122             hideDuration: 8000,
123             kind: SnackbarKind.WARNING,
124         })
125     } finally {
126         if (isWorkbenchLoading(getState())) {
127             dispatch(progressIndicatorActions.STOP_WORKING(WORKBENCH_LOADING_SCREEN));
128         }
129     }
130 };
131
132 export const loadWorkbench = () => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
133     dispatch(progressIndicatorActions.START_WORKING(WORKBENCH_LOADING_SCREEN));
134     const { auth, router } = getState();
135     const { user } = auth;
136     if (user) {
137         dispatch(projectPanelDataActions.SET_COLUMNS({ columns: projectPanelDataColumns }));
138         dispatch(projectPanelRunActions.SET_COLUMNS({ columns: projectPanelRunColumns }));
139         dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
140         dispatch(
141             allProcessesPanelActions.SET_COLUMNS({
142                 columns: allProcessesPanelColumns,
143             })
144         );
145         dispatch(
146             publicFavoritePanelActions.SET_COLUMNS({
147                 columns: publicFavoritePanelColumns,
148             })
149         );
150         dispatch(trashPanelActions.SET_COLUMNS({ columns: trashPanelColumns }));
151         dispatch(sharedWithMePanelActions.SET_COLUMNS({ columns: sharedWithMePanelColumns }));
152         dispatch(workflowPanelActions.SET_COLUMNS({ columns: workflowPanelColumns }));
153         dispatch(
154             searchResultsPanelActions.SET_FETCH_MODE({
155                 fetchMode: DataTableFetchMode.INFINITE,
156             })
157         );
158         dispatch(
159             searchResultsPanelActions.SET_COLUMNS({
160                 columns: searchResultsPanelColumns,
161             })
162         );
163         dispatch(userBindedActions.SET_COLUMNS({ columns: userPanelColumns }));
164         dispatch(
165             groupPanelActions.GroupsPanelActions.SET_COLUMNS({
166                 columns: groupsPanelColumns,
167             })
168         );
169         dispatch(
170             groupDetailsPanelActions.GroupMembersPanelActions.SET_COLUMNS({
171                 columns: groupDetailsMembersPanelColumns,
172             })
173         );
174         dispatch(
175             groupDetailsPanelActions.GroupPermissionsPanelActions.SET_COLUMNS({
176                 columns: groupDetailsPermissionsPanelColumns,
177             })
178         );
179         dispatch(
180             userProfilePanelActions.UserProfileGroupsActions.SET_COLUMNS({
181                 columns: userProfileGroupsColumns,
182             })
183         );
184         dispatch(linkPanelActions.SET_COLUMNS({ columns: linkPanelColumns }));
185         dispatch(
186             apiClientAuthorizationsActions.SET_COLUMNS({
187                 columns: apiClientAuthorizationPanelColumns,
188             })
189         );
190         dispatch(
191             collectionsContentAddressActions.SET_COLUMNS({
192                 columns: collectionContentAddressPanelColumns,
193             })
194         );
195         dispatch(subprocessPanelActions.SET_COLUMNS({ columns: subprocessPanelColumns }));
196         dispatch(workflowProcessesPanelActions.SET_COLUMNS({ columns: workflowProcessesPanelColumns }));
197
198         if (services.linkAccountService.getAccountToLink()) {
199             dispatch(linkAccountPanelActions.HAS_SESSION_DATA());
200         }
201
202         dispatch<any>(initSidePanelTree());
203         if (router.location) {
204             const match = matchRootRoute(router.location.pathname);
205             if (match) {
206                 dispatch<any>(navigateToRootProject);
207             }
208         }
209     } else {
210         dispatch(userIsNotAuthenticated);
211     }
212 };
213
214 export const loadFavorites = () =>
215     handleFirstTimeLoad((dispatch: Dispatch) => {
216         dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
217         dispatch<any>(loadFavoritePanel());
218         dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
219     });
220
221 export const loadCollectionContentAddress = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
222     await dispatch(loadCollectionsContentAddressPanel());
223 });
224
225 export const loadTrash = () =>
226     handleFirstTimeLoad((dispatch: Dispatch) => {
227         dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
228         dispatch<any>(loadTrashPanel());
229         dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.TRASH));
230     });
231
232 export const loadAllProcesses = () =>
233     handleFirstTimeLoad((dispatch: Dispatch) => {
234         dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.ALL_PROCESSES));
235         dispatch<any>(loadAllProcessesPanel());
236         dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.ALL_PROCESSES));
237     });
238
239 export const loadProject = (uuid: string) =>
240     handleFirstTimeLoad(async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
241         const userUuid = getUserUuid(getState());
242         dispatch(setIsProjectPanelTrashed(false));
243         if (!userUuid) {
244             return;
245         }
246         try {
247             dispatch(progressIndicatorActions.START_WORKING(uuid));
248             if (extractUuidKind(uuid) === ResourceKind.USER && userUuid !== uuid) {
249                 // Load another users home projects
250                 dispatch(finishLoadingProject(uuid));
251                 dispatch<any>(setSidePanelBreadcrumbs(uuid));
252             } else if (userUuid !== uuid) {
253                 await dispatch(finishLoadingProject(uuid));
254                 const match = await loadGroupContentsResource({
255                     uuid,
256                     userUuid,
257                     services,
258                 });
259                 match({
260                     OWNED: async () => {
261                         await dispatch(activateSidePanelTreeItem(uuid));
262                         dispatch<any>(setSidePanelBreadcrumbs(uuid));
263                     },
264                     SHARED: async () => {
265                         await dispatch(activateSidePanelTreeItem(uuid));
266                         dispatch<any>(setSharedWithMeBreadcrumbs(uuid));
267                     },
268                     TRASHED: async () => {
269                         await dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
270                         dispatch<any>(setTrashBreadcrumbs(uuid));
271                         dispatch(setIsProjectPanelTrashed(true));
272                     },
273                 });
274             } else {
275                 await dispatch(finishLoadingProject(userUuid));
276                 await dispatch(activateSidePanelTreeItem(userUuid));
277                 dispatch<any>(setSidePanelBreadcrumbs(userUuid));
278             }
279         } finally {
280             dispatch(progressIndicatorActions.STOP_WORKING(uuid));
281         }
282     });
283
284 export const createProject = (data: projectCreateActions.ProjectCreateFormDialogData) => async (dispatch: Dispatch) => {
285     const newProject = await dispatch<any>(projectCreateActions.createProject(data));
286     if (newProject) {
287         dispatch(
288             snackbarActions.OPEN_SNACKBAR({
289                 message: "Project has been successfully created.",
290                 hideDuration: 2000,
291                 kind: SnackbarKind.SUCCESS,
292             })
293         );
294         await dispatch<any>(loadSidePanelTreeProjects(newProject.ownerUuid));
295         dispatch<any>(navigateTo(newProject.uuid));
296     }
297 };
298
299 export const moveProject =
300     (data: MoveToFormDialogData, isSecondaryMove = false) =>
301         async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
302             const checkedList = getState().multiselect.checkedList;
303             const uuidsToMove: string[] = data.fromContextMenu ? [data.uuid] : selectedToArray(checkedList);
304
305             //if no items in checkedlist default to normal context menu behavior
306             if (!isSecondaryMove && !uuidsToMove.length) uuidsToMove.push(data.uuid);
307
308             const sourceUuid = getResource(data.uuid)(getState().resources)?.ownerUuid;
309             const destinationUuid = data.ownerUuid;
310
311             const projectsToMove: MoveableResource[] = uuidsToMove
312                 .map(uuid => getResource(uuid)(getState().resources) as MoveableResource)
313                 .filter(resource => resource.kind === ResourceKind.PROJECT);
314
315             for (const project of projectsToMove) {
316                 await moveSingleProject(project);
317             }
318
319             //omly propagate if this call is the original
320             if (!isSecondaryMove) {
321                 const kindsToMove: Set<string> = selectedToKindSet(checkedList);
322                 kindsToMove.delete(ResourceKind.PROJECT);
323
324                 kindsToMove.forEach(kind => {
325                     secondaryMove[kind](data, true)(dispatch, getState, services);
326                 });
327             }
328
329             async function moveSingleProject(project: MoveableResource) {
330                 try {
331                     const oldProject: MoveToFormDialogData = { name: project.name, uuid: project.uuid, ownerUuid: data.ownerUuid };
332                     const oldOwnerUuid = oldProject ? oldProject.ownerUuid : "";
333                     const movedProject = await dispatch<any>(projectMoveActions.moveProject(oldProject));
334                     if (movedProject) {
335                         dispatch(
336                             snackbarActions.OPEN_SNACKBAR({
337                                 message: "Project has been moved",
338                                 hideDuration: 2000,
339                                 kind: SnackbarKind.SUCCESS,
340                             })
341                         );
342                         await dispatch<any>(reloadProjectMatchingUuid([oldOwnerUuid, movedProject.ownerUuid, movedProject.uuid]));
343                     }
344                 } catch (e) {
345                     dispatch(
346                         snackbarActions.OPEN_SNACKBAR({
347                             message: !!(project as any).frozenByUuid ? 'Could not move frozen project.' : e.message,
348                             hideDuration: 2000,
349                             kind: SnackbarKind.ERROR,
350                         })
351                     );
352                 }
353             }
354             if (sourceUuid) await dispatch<any>(loadSidePanelTreeProjects(sourceUuid));
355             await dispatch<any>(loadSidePanelTreeProjects(destinationUuid));
356         };
357
358 export const updateProject = (data: projectUpdateActions.ProjectUpdateFormDialogData) => async (dispatch: Dispatch) => {
359     const updatedProject = await dispatch<any>(projectUpdateActions.updateProject(data));
360     if (updatedProject) {
361         dispatch(
362             snackbarActions.OPEN_SNACKBAR({
363                 message: "Project has been successfully updated.",
364                 hideDuration: 2000,
365                 kind: SnackbarKind.SUCCESS,
366             })
367         );
368         await dispatch<any>(loadSidePanelTreeProjects(updatedProject.ownerUuid));
369         dispatch<any>(reloadProjectMatchingUuid([updatedProject.ownerUuid, updatedProject.uuid]));
370     }
371 };
372
373 export const updateGroup = (data: projectUpdateActions.ProjectUpdateFormDialogData) => async (dispatch: Dispatch) => {
374     const updatedGroup = await dispatch<any>(groupPanelActions.updateGroup(data));
375     if (updatedGroup) {
376         dispatch(
377             snackbarActions.OPEN_SNACKBAR({
378                 message: "Group has been successfully updated.",
379                 hideDuration: 2000,
380                 kind: SnackbarKind.SUCCESS,
381             })
382         );
383         await dispatch<any>(loadSidePanelTreeProjects(updatedGroup.ownerUuid));
384         dispatch<any>(reloadProjectMatchingUuid([updatedGroup.ownerUuid, updatedGroup.uuid]));
385     }
386 };
387
388 export const loadCollection = (uuid: string) =>
389     handleFirstTimeLoad(async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
390         const userUuid = getUserUuid(getState());
391         try {
392             dispatch(progressIndicatorActions.START_WORKING(uuid));
393             if (userUuid) {
394                 const match = await loadGroupContentsResource({
395                     uuid,
396                     userUuid,
397                     services,
398                 });
399                 let collection: CollectionResource | undefined;
400                 let breadcrumbfunc:
401                     | ((uuid: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => Promise<void>)
402                     | undefined;
403                 let sidepanel: string | undefined;
404                 match({
405                     OWNED: thecollection => {
406                         collection = thecollection as CollectionResource;
407                         sidepanel = collection.ownerUuid;
408                         breadcrumbfunc = setSidePanelBreadcrumbs;
409                     },
410                     SHARED: thecollection => {
411                         collection = thecollection as CollectionResource;
412                         sidepanel = collection.ownerUuid;
413                         breadcrumbfunc = setSharedWithMeBreadcrumbs;
414                     },
415                     TRASHED: thecollection => {
416                         collection = thecollection as CollectionResource;
417                         sidepanel = SidePanelTreeCategory.TRASH;
418                         breadcrumbfunc = () => setTrashBreadcrumbs("");
419                     },
420                 });
421                 if (collection && breadcrumbfunc && sidepanel) {
422                     dispatch(updateResources([collection]));
423                     await dispatch<any>(finishLoadingProject(collection.ownerUuid));
424                     dispatch(collectionPanelActions.SET_COLLECTION(collection));
425                     await dispatch(activateSidePanelTreeItem(sidepanel));
426                     dispatch(breadcrumbfunc(collection.ownerUuid));
427                     dispatch(loadCollectionPanel(collection.uuid));
428                 }
429             }
430         } finally {
431             dispatch(progressIndicatorActions.STOP_WORKING(uuid));
432         }
433     });
434
435 export const createCollection = (data: collectionCreateActions.CollectionCreateFormDialogData) => async (dispatch: Dispatch) => {
436     const collection = await dispatch<any>(collectionCreateActions.createCollection(data));
437     if (collection) {
438         dispatch(
439             snackbarActions.OPEN_SNACKBAR({
440                 message: "Collection has been successfully created.",
441                 hideDuration: 2000,
442                 kind: SnackbarKind.SUCCESS,
443             })
444         );
445         dispatch<any>(updateResources([collection]));
446         dispatch<any>(navigateTo(collection.uuid));
447     }
448 };
449
450 export const copyCollection = (data: CopyFormDialogData) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
451     const checkedList = getState().multiselect.checkedList;
452     const uuidsToCopy: string[] = data.fromContextMenu ? [data.uuid] : selectedToArray(checkedList);
453
454     //if no items in checkedlist && no items passed in, default to normal context menu behavior
455     if (!uuidsToCopy.length) uuidsToCopy.push(data.uuid);
456
457     const collectionsToCopy: CollectionCopyResource[] = uuidsToCopy
458         .map(uuid => getResource(uuid)(getState().resources) as CollectionCopyResource)
459         .filter(resource => resource.kind === ResourceKind.COLLECTION);
460
461     for (const collection of collectionsToCopy) {
462         await copySingleCollection({ ...collection, ownerUuid: data.ownerUuid } as CollectionCopyResource);
463     }
464
465     async function copySingleCollection(copyToProject: CollectionCopyResource) {
466         const newName = data.fromContextMenu || collectionsToCopy.length === 1 ? data.name : `Copy of: ${copyToProject.name}`;
467         try {
468             const collection = await dispatch<any>(
469                 collectionCopyActions.copyCollection({
470                     ...copyToProject,
471                     name: newName,
472                     fromContextMenu: collectionsToCopy.length === 1 ? true : data.fromContextMenu,
473                 })
474             );
475             if (copyToProject && collection) {
476                 await dispatch<any>(reloadProjectMatchingUuid([copyToProject.uuid]));
477                 dispatch(
478                     snackbarActions.OPEN_SNACKBAR({
479                         message: "Collection has been copied.",
480                         hideDuration: 3000,
481                         kind: SnackbarKind.SUCCESS,
482                         link: collection.ownerUuid,
483                     })
484                 );
485                 dispatch<any>(deselectOne(copyToProject.uuid));
486             }
487         } catch (e) {
488             dispatch(
489                 snackbarActions.OPEN_SNACKBAR({
490                     message: e.message,
491                     hideDuration: 2000,
492                     kind: SnackbarKind.ERROR,
493                 })
494             );
495         }
496     }
497     dispatch(projectPanelDataActions.REQUEST_ITEMS());
498 };
499
500 export const moveCollection =
501     (data: MoveToFormDialogData, isSecondaryMove = false) =>
502         async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
503             const checkedList = getState().multiselect.checkedList;
504             const uuidsToMove: string[] = data.fromContextMenu ? [data.uuid] : selectedToArray(checkedList);
505
506             //if no items in checkedlist && no items passed in, default to normal context menu behavior
507             if (!isSecondaryMove && !uuidsToMove.length) uuidsToMove.push(data.uuid);
508
509             const collectionsToMove: MoveableResource[] = uuidsToMove
510                 .map(uuid => getResource(uuid)(getState().resources) as MoveableResource)
511                 .filter(resource => resource.kind === ResourceKind.COLLECTION);
512
513             for (const collection of collectionsToMove) {
514                 await moveSingleCollection(collection);
515             }
516
517             //omly propagate if this call is the original
518             if (!isSecondaryMove) {
519                 const kindsToMove: Set<string> = selectedToKindSet(checkedList);
520                 kindsToMove.delete(ResourceKind.COLLECTION);
521
522                 kindsToMove.forEach(kind => {
523                     secondaryMove[kind](data, true)(dispatch, getState, services);
524                 });
525             }
526
527             async function moveSingleCollection(collection: MoveableResource) {
528                 try {
529                     const oldCollection: MoveToFormDialogData = { name: collection.name, uuid: collection.uuid, ownerUuid: data.ownerUuid };
530                     const movedCollection = await dispatch<any>(collectionMoveActions.moveCollection(oldCollection));
531                     dispatch<any>(updateResources([movedCollection]));
532                     dispatch<any>(reloadProjectMatchingUuid([movedCollection.ownerUuid]));
533                     dispatch(
534                         snackbarActions.OPEN_SNACKBAR({
535                             message: "Collection has been moved.",
536                             hideDuration: 2000,
537                             kind: SnackbarKind.SUCCESS,
538                         })
539                     );
540                 } catch (e) {
541                     dispatch(
542                         snackbarActions.OPEN_SNACKBAR({
543                             message: e.message,
544                             hideDuration: 2000,
545                             kind: SnackbarKind.ERROR,
546                         })
547                     );
548                 }
549             }
550         };
551
552 export const loadProcess = (uuid: string) =>
553     handleFirstTimeLoad(async (dispatch: Dispatch, getState: () => RootState) => {
554         try {
555             dispatch(progressIndicatorActions.START_WORKING(uuid));
556             dispatch<any>(loadProcessPanel(uuid));
557             const process = await dispatch<any>(loadProcessResources(uuid));
558             if (process) {
559                 await dispatch<any>(finishLoadingProject(process.containerRequest.ownerUuid));
560                 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
561                 dispatch<any>(setProcessBreadcrumbs(uuid));
562                 dispatch<any>(loadDetailsPanel(uuid));
563             }
564         } finally {
565             dispatch(progressIndicatorActions.STOP_WORKING(uuid));
566         }
567     });
568
569 export const loadRegisteredWorkflow = (uuid: string) =>
570     handleFirstTimeLoad(async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
571         const userUuid = getUserUuid(getState());
572         if (userUuid) {
573             const match = await loadGroupContentsResource({
574                 uuid,
575                 userUuid,
576                 services,
577             });
578             let workflow: WorkflowResource | undefined;
579             let breadcrumbfunc:
580                 | ((uuid: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => Promise<void>)
581                 | undefined;
582             match({
583                 OWNED: async theworkflow => {
584                     workflow = theworkflow as WorkflowResource;
585                     breadcrumbfunc = setSidePanelBreadcrumbs;
586                 },
587                 SHARED: async theworkflow => {
588                     workflow = theworkflow as WorkflowResource;
589                     breadcrumbfunc = setSharedWithMeBreadcrumbs;
590                 },
591                 TRASHED: () => { },
592             });
593             if (workflow && breadcrumbfunc) {
594                 dispatch(updateResources([workflow]));
595                 await dispatch<any>(finishLoadingProject(workflow.ownerUuid));
596                 await dispatch<any>(activateSidePanelTreeItem(workflow.ownerUuid));
597                 dispatch<any>(breadcrumbfunc(workflow.ownerUuid));
598                 dispatch(workflowProcessesPanelActions.REQUEST_ITEMS());
599             }
600         }
601     });
602
603 export const updateProcess = (data: processUpdateActions.ProcessUpdateFormDialogData) => async (dispatch: Dispatch) => {
604     try {
605         const process = await dispatch<any>(processUpdateActions.updateProcess(data));
606         if (process) {
607             dispatch(
608                 snackbarActions.OPEN_SNACKBAR({
609                     message: "Process has been successfully updated.",
610                     hideDuration: 2000,
611                     kind: SnackbarKind.SUCCESS,
612                 })
613             );
614             dispatch<any>(updateResources([process]));
615             dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
616         }
617     } catch (e) {
618         dispatch(
619             snackbarActions.OPEN_SNACKBAR({
620                 message: e.message,
621                 hideDuration: 2000,
622                 kind: SnackbarKind.ERROR,
623             })
624         );
625     }
626 };
627
628 export const moveProcess =
629     (data: MoveToFormDialogData, isSecondaryMove = false) =>
630         async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
631             const checkedList = getState().multiselect.checkedList;
632             const uuidsToMove: string[] = data.fromContextMenu ? [data.uuid] : selectedToArray(checkedList);
633
634             //if no items in checkedlist && no items passed in, default to normal context menu behavior
635             if (!isSecondaryMove && !uuidsToMove.length) uuidsToMove.push(data.uuid);
636
637             const processesToMove: MoveableResource[] = uuidsToMove
638                 .map(uuid => getResource(uuid)(getState().resources) as MoveableResource)
639                 .filter(resource => resource.kind === ResourceKind.PROCESS);
640
641             for (const process of processesToMove) {
642                 await moveSingleProcess(process);
643             }
644
645             //omly propagate if this call is the original
646             if (!isSecondaryMove) {
647                 const kindsToMove: Set<string> = selectedToKindSet(checkedList);
648                 kindsToMove.delete(ResourceKind.PROCESS);
649
650                 kindsToMove.forEach(kind => {
651                     secondaryMove[kind](data, true)(dispatch, getState, services);
652                 });
653             }
654
655             async function moveSingleProcess(process: MoveableResource) {
656                 try {
657                     const oldProcess: MoveToFormDialogData = { name: process.name, uuid: process.uuid, ownerUuid: data.ownerUuid };
658                     const movedProcess = await dispatch<any>(processMoveActions.moveProcess(oldProcess));
659                     dispatch<any>(updateResources([movedProcess]));
660                     dispatch<any>(reloadProjectMatchingUuid([movedProcess.ownerUuid]));
661                     dispatch(
662                         snackbarActions.OPEN_SNACKBAR({
663                             message: "Process has been moved.",
664                             hideDuration: 2000,
665                             kind: SnackbarKind.SUCCESS,
666                         })
667                     );
668                 } catch (e) {
669                     dispatch(
670                         snackbarActions.OPEN_SNACKBAR({
671                             message: e.message,
672                             hideDuration: 2000,
673                             kind: SnackbarKind.ERROR,
674                         })
675                     );
676                 }
677             }
678         };
679
680 export const copyProcess = (data: CopyFormDialogData) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
681     try {
682         const process = await dispatch<any>(processCopyActions.copyProcess(data));
683         dispatch<any>(updateResources([process]));
684         dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
685         dispatch(
686             snackbarActions.OPEN_SNACKBAR({
687                 message: "Process has been copied.",
688                 hideDuration: 2000,
689                 kind: SnackbarKind.SUCCESS,
690             })
691         );
692         dispatch<any>(navigateTo(process.uuid));
693     } catch (e) {
694         dispatch(
695             snackbarActions.OPEN_SNACKBAR({
696                 message: e.message,
697                 hideDuration: 2000,
698                 kind: SnackbarKind.ERROR,
699             })
700         );
701     }
702 };
703
704 export const resourceIsNotLoaded = (uuid: string) =>
705     snackbarActions.OPEN_SNACKBAR({
706         message: `Resource identified by ${uuid} is not loaded.`,
707         kind: SnackbarKind.ERROR,
708     });
709
710 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
711     message: "User is not authenticated",
712     kind: SnackbarKind.ERROR,
713 });
714
715 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
716     message: "Could not load user",
717     kind: SnackbarKind.ERROR,
718 });
719
720 export const reloadProjectMatchingUuid =
721     (matchingUuids: string[]) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
722         const currentProjectPanelUuid = getProjectPanelCurrentUuid(getState());
723         if (currentProjectPanelUuid && matchingUuids.some(uuid => uuid === currentProjectPanelUuid)) {
724             dispatch<any>(loadProject(currentProjectPanelUuid));
725         }
726     };
727
728 export const loadSharedWithMe = handleFirstTimeLoad(async (dispatch: Dispatch) => {
729     dispatch<any>(loadSharedWithMePanel());
730     await dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
731     await dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.SHARED_WITH_ME));
732 });
733
734 export const loadRunProcess = handleFirstTimeLoad(async (dispatch: Dispatch) => {
735     await dispatch<any>(loadRunProcessPanel());
736 });
737
738 export const loadPublicFavorites = () =>
739     handleFirstTimeLoad((dispatch: Dispatch) => {
740         dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.PUBLIC_FAVORITES));
741         dispatch<any>(loadPublicFavoritePanel());
742         dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.PUBLIC_FAVORITES));
743     });
744
745 export const loadSearchResults = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
746     await dispatch(loadSearchResultsPanel());
747 });
748
749 export const loadLinks = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
750     await dispatch(loadLinkPanel());
751 });
752
753 export const loadVirtualMachines = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
754     await dispatch(loadVirtualMachinesPanel());
755     dispatch(setVirtualMachinesBreadcrumbs());
756     dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.SHELL_ACCESS));
757 });
758
759 export const loadVirtualMachinesAdmin = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
760     await dispatch(loadVirtualMachinesPanel());
761     dispatch(setVirtualMachinesAdminBreadcrumbs());
762     dispatch(treePickerActions.DEACTIVATE_TREE_PICKER_NODE({ pickerId: SIDE_PANEL_TREE }))
763 });
764
765 export const loadRepositories = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
766     await dispatch(loadRepositoriesPanel());
767     dispatch(setRepositoriesBreadcrumbs());
768 });
769
770 export const loadSshKeys = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
771     await dispatch(loadSshKeysPanel());
772 });
773
774 export const loadInstanceTypes = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
775     dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.INSTANCE_TYPES));
776     dispatch(setInstanceTypesBreadcrumbs());
777 });
778
779 export const loadSiteManager = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
780     await dispatch(loadSiteManagerPanel());
781 });
782
783 export const loadUserProfile = (userUuid?: string) =>
784     handleFirstTimeLoad((dispatch: Dispatch<any>) => {
785         if (userUuid) {
786             dispatch(setUserProfileBreadcrumbs(userUuid));
787             dispatch(userProfilePanelActions.loadUserProfilePanel(userUuid));
788         } else {
789             dispatch(setMyAccountBreadcrumbs());
790             dispatch(userProfilePanelActions.loadUserProfilePanel());
791         }
792     });
793
794 export const loadLinkAccount = handleFirstTimeLoad((dispatch: Dispatch<any>) => {
795     dispatch(loadLinkAccountPanel());
796 });
797
798 export const loadKeepServices = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
799     await dispatch(loadKeepServicesPanel());
800 });
801
802 export const loadUsers = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
803     await dispatch(loadUsersPanel());
804     dispatch(setUsersBreadcrumbs());
805 });
806
807 export const loadApiClientAuthorizations = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
808     await dispatch(loadApiClientAuthorizationsPanel());
809 });
810
811 export const loadGroupsPanel = handleFirstTimeLoad((dispatch: Dispatch<any>) => {
812     dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.GROUPS));
813     dispatch(setGroupsBreadcrumbs());
814     dispatch(groupPanelActions.loadGroupsPanel());
815 });
816
817 export const loadGroupDetailsPanel = (groupUuid: string) =>
818     handleFirstTimeLoad((dispatch: Dispatch<any>) => {
819         dispatch(setGroupDetailsBreadcrumbs(groupUuid));
820         dispatch(groupDetailsPanelActions.loadGroupDetailsPanel(groupUuid));
821     });
822
823 const finishLoadingProject = (project: GroupContentsResource | string) => async (dispatch: Dispatch<any>) => {
824     const uuid = typeof project === "string" ? project : project.uuid;
825     dispatch(loadDetailsPanel(uuid));
826     if (typeof project !== "string") {
827         dispatch(updateResources([project]));
828     }
829 };
830
831 const loadGroupContentsResource = async (params: { uuid: string; userUuid: string; services: ServiceRepository }) => {
832     const filters = new FilterBuilder().addEqual("uuid", params.uuid).getFilters();
833     const { items } = await params.services.groupsService.contents(params.userUuid, {
834         filters,
835         recursive: true,
836         includeTrash: true,
837     });
838     const resource = items.shift();
839     let handler: GroupContentsHandler;
840     if (resource) {
841         handler =
842             (resource.kind === ResourceKind.COLLECTION || resource.kind === ResourceKind.PROJECT) && resource.isTrashed
843                 ? groupContentsHandlers.TRASHED(resource)
844                 : groupContentsHandlers.OWNED(resource);
845     } else {
846         const kind = extractUuidKind(params.uuid);
847         let resource: GroupContentsResource;
848         if (kind === ResourceKind.COLLECTION) {
849             resource = await params.services.collectionService.get(params.uuid);
850         } else if (kind === ResourceKind.PROJECT) {
851             resource = await params.services.projectService.get(params.uuid);
852         } else if (kind === ResourceKind.WORKFLOW) {
853             resource = await params.services.workflowService.get(params.uuid);
854         } else if (kind === ResourceKind.CONTAINER_REQUEST) {
855             resource = await params.services.containerRequestService.get(params.uuid);
856         } else {
857             throw new Error("loadGroupContentsResource unsupported kind " + kind);
858         }
859         handler = groupContentsHandlers.SHARED(resource);
860     }
861     return (cases: MatchCases<typeof groupContentsHandlersRecord, GroupContentsHandler, void>) => groupContentsHandlers.match(handler, cases);
862 };
863
864 const groupContentsHandlersRecord = {
865     TRASHED: ofType<GroupContentsResource>(),
866     SHARED: ofType<GroupContentsResource>(),
867     OWNED: ofType<GroupContentsResource>(),
868 };
869
870 const groupContentsHandlers = unionize(groupContentsHandlersRecord);
871
872 type GroupContentsHandler = UnionOf<typeof groupContentsHandlers>;
873
874 type CollectionCopyResource = Resource & { name: string; fromContextMenu: boolean };
875
876 type MoveableResource = Resource & { name: string };
877
878 type MoveFunc = (
879     data: MoveToFormDialogData,
880     isSecondaryMove?: boolean
881 ) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => Promise<void>;
882
883 const secondaryMove: Record<string, MoveFunc> = {
884     [ResourceKind.PROJECT]: moveProject,
885     [ResourceKind.PROCESS]: moveProcess,
886     [ResourceKind.COLLECTION]: moveCollection,
887 };