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