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