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