Merge branch 'master' into 14452-my-account
[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 { loadDetailsPanel } from '~/store/details-panel/details-panel-action';
8 import { snackbarActions } from '~/store/snackbar/snackbar-actions';
9 import { loadFavoritePanel } from '~/store/favorite-panel/favorite-panel-action';
10 import { openProjectPanel, projectPanelActions, setIsProjectPanelTrashed } from '~/store/project-panel/project-panel-action';
11 import { activateSidePanelTreeItem, initSidePanelTree, SidePanelTreeCategory, loadSidePanelTreeProjects } from '~/store/side-panel-tree/side-panel-tree-actions';
12 import { loadResource, updateResources } from '~/store/resources/resources-actions';
13 import { favoritePanelActions } from '~/store/favorite-panel/favorite-panel-action';
14 import { projectPanelColumns } from '~/views/project-panel/project-panel';
15 import { favoritePanelColumns } from '~/views/favorite-panel/favorite-panel';
16 import { matchRootRoute } from '~/routes/routes';
17 import { setSidePanelBreadcrumbs, setProcessBreadcrumbs, setSharedWithMeBreadcrumbs, setTrashBreadcrumbs, setBreadcrumbs } from '~/store/breadcrumbs/breadcrumbs-actions';
18 import { navigateToProject } from '~/store/navigation/navigation-action';
19 import { MoveToFormDialogData } from '~/store/move-to-dialog/move-to-dialog';
20 import { ServiceRepository } from '~/services/services';
21 import { getResource } from '~/store/resources/resources';
22 import { getProjectPanelCurrentUuid } from '~/store/project-panel/project-panel-action';
23 import * as projectCreateActions from '~/store/projects/project-create-actions';
24 import * as projectMoveActions from '~/store/projects/project-move-actions';
25 import * as projectUpdateActions from '~/store/projects/project-update-actions';
26 import * as collectionCreateActions from '~/store/collections/collection-create-actions';
27 import * as collectionCopyActions from '~/store/collections/collection-copy-actions';
28 import * as collectionUpdateActions from '~/store/collections/collection-update-actions';
29 import * as collectionMoveActions from '~/store/collections/collection-move-actions';
30 import * as processesActions from '~/store/processes/processes-actions';
31 import * as processMoveActions from '~/store/processes/process-move-actions';
32 import * as processUpdateActions from '~/store/processes/process-update-actions';
33 import * as processCopyActions from '~/store/processes/process-copy-actions';
34 import { trashPanelColumns } from "~/views/trash-panel/trash-panel";
35 import { loadTrashPanel, trashPanelActions } from "~/store/trash-panel/trash-panel-action";
36 import { initProcessLogsPanel } from '~/store/process-logs-panel/process-logs-panel-actions';
37 import { loadProcessPanel } from '~/store/process-panel/process-panel-actions';
38 import { sharedWithMePanelActions } from '~/store/shared-with-me-panel/shared-with-me-panel-actions';
39 import { loadSharedWithMePanel } from '~/store/shared-with-me-panel/shared-with-me-panel-actions';
40 import { CopyFormDialogData } from '~/store/copy-dialog/copy-dialog';
41 import { loadWorkflowPanel, workflowPanelActions } from '~/store/workflow-panel/workflow-panel-actions';
42 import { loadSshKeysPanel } from '~/store/auth/auth-action';
43 import { loadMyAccountPanel } from '~/store/my-account/my-account-panel-actions';
44 import { workflowPanelColumns } from '~/views/workflow-panel/workflow-panel-view';
45 import { progressIndicatorActions } from '~/store/progress-indicator/progress-indicator-actions';
46 import { getProgressIndicator } from '~/store/progress-indicator/progress-indicator-reducer';
47 import { ResourceKind, extractUuidKind } from '~/models/resource';
48 import { FilterBuilder } from '~/services/api/filter-builder';
49 import { GroupContentsResource } from '~/services/groups-service/groups-service';
50 import { unionize, ofType, UnionOf, MatchCases } from '~/common/unionize';
51 import { loadRunProcessPanel } from '~/store/run-process-panel/run-process-panel-actions';
52 import { loadCollectionFiles } from '~/store/collection-panel/collection-panel-files/collection-panel-files-actions';
53 import { SnackbarKind } from '~/store/snackbar/snackbar-actions';
54 import { collectionPanelActions } from "~/store/collection-panel/collection-panel-action";
55 import { CollectionResource } from "~/models/collection";
56 import { searchResultsPanelActions, loadSearchResultsPanel } from '~/store/search-results-panel/search-results-panel-actions';
57 import { searchResultsPanelColumns } from '~/views/search-results-panel/search-results-panel-view';
58 import { loadVirtualMachinesPanel } from '~/store/virtual-machines/virtual-machines-actions';
59 import { loadRepositoriesPanel } from '~/store/repositories/repositories-actions';
60
61 export const WORKBENCH_LOADING_SCREEN = 'workbenchLoadingScreen';
62
63 export const isWorkbenchLoading = (state: RootState) => {
64     const progress = getProgressIndicator(WORKBENCH_LOADING_SCREEN)(state.progressIndicator);
65     return progress ? progress.working : false;
66 };
67
68 const handleFirstTimeLoad = (action: any) =>
69     async (dispatch: Dispatch<any>, getState: () => RootState) => {
70         try {
71             await dispatch(action);
72         } finally {
73             if (isWorkbenchLoading(getState())) {
74                 dispatch(progressIndicatorActions.STOP_WORKING(WORKBENCH_LOADING_SCREEN));
75             }
76         }
77     };
78
79
80 export const loadWorkbench = () =>
81     async (dispatch: Dispatch, getState: () => RootState) => {
82         dispatch(progressIndicatorActions.START_WORKING(WORKBENCH_LOADING_SCREEN));
83         const { auth, router } = getState();
84         const { user } = auth;
85         if (user) {
86             const userResource = await dispatch<any>(loadResource(user.uuid));
87             if (userResource) {
88                 dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
89                 dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
90                 dispatch(trashPanelActions.SET_COLUMNS({ columns: trashPanelColumns }));
91                 dispatch(sharedWithMePanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
92                 dispatch(workflowPanelActions.SET_COLUMNS({ columns: workflowPanelColumns }));
93                 dispatch(searchResultsPanelActions.SET_COLUMNS({ columns: searchResultsPanelColumns }));
94                 dispatch<any>(initSidePanelTree());
95                 if (router.location) {
96                     const match = matchRootRoute(router.location.pathname);
97                     if (match) {
98                         dispatch(navigateToProject(userResource.uuid));
99                     }
100                 }
101             } else {
102                 dispatch(userIsNotAuthenticated);
103             }
104         } else {
105             dispatch(userIsNotAuthenticated);
106         }
107     };
108
109 export const loadFavorites = () =>
110     handleFirstTimeLoad(
111         (dispatch: Dispatch) => {
112             dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
113             dispatch<any>(loadFavoritePanel());
114             dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
115         });
116
117 export const loadTrash = () =>
118     handleFirstTimeLoad(
119         (dispatch: Dispatch) => {
120             dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
121             dispatch<any>(loadTrashPanel());
122             dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.TRASH));
123         });
124
125 export const loadProject = (uuid: string) =>
126     handleFirstTimeLoad(
127         async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
128             const userUuid = services.authService.getUuid();
129             dispatch(setIsProjectPanelTrashed(false));
130             if (userUuid) {
131                 if (userUuid !== uuid) {
132                     const match = await loadGroupContentsResource({ uuid, userUuid, services });
133                     match({
134                         OWNED: async project => {
135                             await dispatch(activateSidePanelTreeItem(uuid));
136                             dispatch<any>(setSidePanelBreadcrumbs(uuid));
137                             dispatch(finishLoadingProject(project));
138                         },
139                         SHARED: project => {
140                             dispatch<any>(setSharedWithMeBreadcrumbs(uuid));
141                             dispatch(activateSidePanelTreeItem(uuid));
142                             dispatch(finishLoadingProject(project));
143                         },
144                         TRASHED: project => {
145                             dispatch<any>(setTrashBreadcrumbs(uuid));
146                             dispatch(setIsProjectPanelTrashed(true));
147                             dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
148                             dispatch(finishLoadingProject(project));
149                         }
150                     });
151                 } else {
152                     await dispatch(activateSidePanelTreeItem(userUuid));
153                     dispatch<any>(setSidePanelBreadcrumbs(userUuid));
154                     dispatch(finishLoadingProject(userUuid));
155                 }
156             }
157         });
158
159 export const createProject = (data: projectCreateActions.ProjectCreateFormDialogData) =>
160     async (dispatch: Dispatch) => {
161         const newProject = await dispatch<any>(projectCreateActions.createProject(data));
162         if (newProject) {
163             dispatch(snackbarActions.OPEN_SNACKBAR({
164                 message: "Project has been successfully created.",
165                 hideDuration: 2000
166             }));
167             await dispatch<any>(loadSidePanelTreeProjects(newProject.ownerUuid));
168             dispatch<any>(reloadProjectMatchingUuid([newProject.ownerUuid]));
169         }
170     };
171
172 export const moveProject = (data: MoveToFormDialogData) =>
173     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
174         try {
175             const oldProject = getResource(data.uuid)(getState().resources);
176             const oldOwnerUuid = oldProject ? oldProject.ownerUuid : '';
177             const movedProject = await dispatch<any>(projectMoveActions.moveProject(data));
178             if (movedProject) {
179                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Project has been moved', hideDuration: 2000 }));
180                 if (oldProject) {
181                     await dispatch<any>(loadSidePanelTreeProjects(oldProject.ownerUuid));
182                 }
183                 dispatch<any>(reloadProjectMatchingUuid([oldOwnerUuid, movedProject.ownerUuid, movedProject.uuid]));
184             }
185         } catch (e) {
186             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
187         }
188     };
189
190 export const updateProject = (data: projectUpdateActions.ProjectUpdateFormDialogData) =>
191     async (dispatch: Dispatch) => {
192         const updatedProject = await dispatch<any>(projectUpdateActions.updateProject(data));
193         if (updatedProject) {
194             dispatch(snackbarActions.OPEN_SNACKBAR({
195                 message: "Project has been successfully updated.",
196                 hideDuration: 2000
197             }));
198             await dispatch<any>(loadSidePanelTreeProjects(updatedProject.ownerUuid));
199             dispatch<any>(reloadProjectMatchingUuid([updatedProject.ownerUuid, updatedProject.uuid]));
200         }
201     };
202
203 export const loadCollection = (uuid: string) =>
204     handleFirstTimeLoad(
205         async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
206             const userUuid = services.authService.getUuid();
207             if (userUuid) {
208                 const match = await loadGroupContentsResource({ uuid, userUuid, services });
209                 match({
210                     OWNED: async collection => {
211                         dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
212                         dispatch(updateResources([collection]));
213                         await dispatch(activateSidePanelTreeItem(collection.ownerUuid));
214                         dispatch(setSidePanelBreadcrumbs(collection.ownerUuid));
215                         dispatch(loadCollectionFiles(collection.uuid));
216                     },
217                     SHARED: collection => {
218                         dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
219                         dispatch(updateResources([collection]));
220                         dispatch<any>(setSharedWithMeBreadcrumbs(collection.ownerUuid));
221                         dispatch(activateSidePanelTreeItem(collection.ownerUuid));
222                         dispatch(loadCollectionFiles(collection.uuid));
223                     },
224                     TRASHED: collection => {
225                         dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
226                         dispatch(updateResources([collection]));
227                         dispatch(setTrashBreadcrumbs(''));
228                         dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
229                         dispatch(loadCollectionFiles(collection.uuid));
230                     },
231
232                 });
233             }
234         });
235
236 export const createCollection = (data: collectionCreateActions.CollectionCreateFormDialogData) =>
237     async (dispatch: Dispatch) => {
238         const collection = await dispatch<any>(collectionCreateActions.createCollection(data));
239         if (collection) {
240             dispatch(snackbarActions.OPEN_SNACKBAR({
241                 message: "Collection has been successfully created.",
242                 hideDuration: 2000
243             }));
244             dispatch<any>(updateResources([collection]));
245             dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
246         }
247     };
248
249 export const updateCollection = (data: collectionUpdateActions.CollectionUpdateFormDialogData) =>
250     async (dispatch: Dispatch) => {
251         const collection = await dispatch<any>(collectionUpdateActions.updateCollection(data));
252         if (collection) {
253             dispatch(snackbarActions.OPEN_SNACKBAR({
254                 message: "Collection has been successfully updated.",
255                 hideDuration: 2000
256             }));
257             dispatch<any>(updateResources([collection]));
258             dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
259         }
260     };
261
262 export const copyCollection = (data: CopyFormDialogData) =>
263     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
264         try {
265             const copyToProject = getResource(data.ownerUuid)(getState().resources);
266             const collection = await dispatch<any>(collectionCopyActions.copyCollection(data));
267             if (copyToProject && collection) {
268                 dispatch<any>(reloadProjectMatchingUuid([copyToProject.uuid]));
269                 dispatch(snackbarActions.OPEN_SNACKBAR({
270                     message: 'Collection has been copied.',
271                     hideDuration: 3000,
272                     kind: SnackbarKind.SUCCESS,
273                     link: collection.ownerUuid
274                 }));
275             }
276         } catch (e) {
277             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000, kind: SnackbarKind.ERROR }));
278         }
279     };
280
281 export const moveCollection = (data: MoveToFormDialogData) =>
282     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
283         try {
284             const collection = await dispatch<any>(collectionMoveActions.moveCollection(data));
285             dispatch<any>(updateResources([collection]));
286             dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
287             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been moved.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
288         } catch (e) {
289             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
290         }
291     };
292
293 export const loadProcess = (uuid: string) =>
294     handleFirstTimeLoad(
295         async (dispatch: Dispatch, getState: () => RootState) => {
296             dispatch<any>(loadProcessPanel(uuid));
297             const process = await dispatch<any>(processesActions.loadProcess(uuid));
298             await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
299             dispatch<any>(setProcessBreadcrumbs(uuid));
300             dispatch(loadDetailsPanel(uuid));
301         });
302
303 export const updateProcess = (data: processUpdateActions.ProcessUpdateFormDialogData) =>
304     async (dispatch: Dispatch) => {
305         try {
306             const process = await dispatch<any>(processUpdateActions.updateProcess(data));
307             if (process) {
308                 dispatch(snackbarActions.OPEN_SNACKBAR({
309                     message: "Process has been successfully updated.",
310                     hideDuration: 2000
311                 }));
312                 dispatch<any>(updateResources([process]));
313                 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
314             }
315         } catch (e) {
316             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
317         }
318     };
319
320 export const moveProcess = (data: MoveToFormDialogData) =>
321     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
322         try {
323             const process = await dispatch<any>(processMoveActions.moveProcess(data));
324             dispatch<any>(updateResources([process]));
325             dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
326             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been moved.', hideDuration: 2000 }));
327         } catch (e) {
328             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
329         }
330     };
331
332 export const copyProcess = (data: CopyFormDialogData) =>
333     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
334         try {
335             const process = await dispatch<any>(processCopyActions.copyProcess(data));
336             dispatch<any>(updateResources([process]));
337             dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
338             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been copied.', hideDuration: 2000 }));
339         } catch (e) {
340             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
341         }
342     };
343
344 export const loadProcessLog = (uuid: string) =>
345     handleFirstTimeLoad(
346         async (dispatch: Dispatch) => {
347             const process = await dispatch<any>(processesActions.loadProcess(uuid));
348             dispatch<any>(setProcessBreadcrumbs(uuid));
349             dispatch<any>(initProcessLogsPanel(uuid));
350             await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
351         });
352
353 export const resourceIsNotLoaded = (uuid: string) =>
354     snackbarActions.OPEN_SNACKBAR({
355         message: `Resource identified by ${uuid} is not loaded.`
356     });
357
358 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
359     message: 'User is not authenticated'
360 });
361
362 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
363     message: 'Could not load user'
364 });
365
366 export const reloadProjectMatchingUuid = (matchingUuids: string[]) =>
367     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
368         const currentProjectPanelUuid = getProjectPanelCurrentUuid(getState());
369         if (currentProjectPanelUuid && matchingUuids.some(uuid => uuid === currentProjectPanelUuid)) {
370             dispatch<any>(loadProject(currentProjectPanelUuid));
371         }
372     };
373
374 export const loadSharedWithMe = handleFirstTimeLoad(async (dispatch: Dispatch) => {
375     dispatch<any>(loadSharedWithMePanel());
376     await dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
377     await dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.SHARED_WITH_ME));
378 });
379
380 export const loadRunProcess = handleFirstTimeLoad(
381     async (dispatch: Dispatch) => {
382         await dispatch<any>(loadRunProcessPanel());
383     }
384 );
385
386 export const loadWorkflow = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
387     dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.WORKFLOWS));
388     await dispatch(loadWorkflowPanel());
389     dispatch(setSidePanelBreadcrumbs(SidePanelTreeCategory.WORKFLOWS));
390 });
391
392 export const loadSearchResults = handleFirstTimeLoad(
393     async (dispatch: Dispatch<any>) => {
394         await dispatch(loadSearchResultsPanel());
395     });
396
397 export const loadVirtualMachines = handleFirstTimeLoad(
398     async (dispatch: Dispatch<any>) => {
399         await dispatch(loadVirtualMachinesPanel());
400         dispatch(setBreadcrumbs([{ label: 'Virtual Machines' }]));
401     });
402     
403 export const loadRepositories = handleFirstTimeLoad(
404     async (dispatch: Dispatch<any>) => {
405         await dispatch(loadRepositoriesPanel());
406         dispatch(setBreadcrumbs([{ label: 'Repositories' }]));
407     });
408
409 export const loadSshKeys = handleFirstTimeLoad(
410     async (dispatch: Dispatch<any>) => {
411         await dispatch(loadSshKeysPanel());
412     });
413
414 export const loadMyAccount = handleFirstTimeLoad(
415     async (dispatch: Dispatch<any>) => {
416         await dispatch(loadMyAccountPanel());
417     });
418
419 const finishLoadingProject = (project: GroupContentsResource | string) =>
420     async (dispatch: Dispatch<any>) => {
421         const uuid = typeof project === 'string' ? project : project.uuid;
422         dispatch(openProjectPanel(uuid));
423         dispatch(loadDetailsPanel(uuid));
424         if (typeof project !== 'string') {
425             dispatch(updateResources([project]));
426         }
427     };
428
429 const loadGroupContentsResource = async (params: {
430     uuid: string,
431     userUuid: string,
432     services: ServiceRepository
433 }) => {
434     const filters = new FilterBuilder()
435         .addEqual('uuid', params.uuid)
436         .getFilters();
437     const { items } = await params.services.groupsService.contents(params.userUuid, {
438         filters,
439         recursive: true,
440         includeTrash: true,
441     });
442     const resource = items.shift();
443     let handler: GroupContentsHandler;
444     if (resource) {
445         handler = (resource.kind === ResourceKind.COLLECTION || resource.kind === ResourceKind.PROJECT) && resource.isTrashed
446             ? groupContentsHandlers.TRASHED(resource)
447             : groupContentsHandlers.OWNED(resource);
448     } else {
449         const kind = extractUuidKind(params.uuid);
450         let resource: GroupContentsResource;
451         if (kind === ResourceKind.COLLECTION) {
452             resource = await params.services.collectionService.get(params.uuid);
453         } else if (kind === ResourceKind.PROJECT) {
454             resource = await params.services.projectService.get(params.uuid);
455         } else {
456             resource = await params.services.containerRequestService.get(params.uuid);
457         }
458         handler = groupContentsHandlers.SHARED(resource);
459     }
460     return (cases: MatchCases<typeof groupContentsHandlersRecord, GroupContentsHandler, void>) =>
461         groupContentsHandlers.match(handler, cases);
462
463 };
464
465 const groupContentsHandlersRecord = {
466     TRASHED: ofType<GroupContentsResource>(),
467     SHARED: ofType<GroupContentsResource>(),
468     OWNED: ofType<GroupContentsResource>(),
469 };
470
471 const groupContentsHandlers = unionize(groupContentsHandlersRecord);
472
473 type GroupContentsHandler = UnionOf<typeof groupContentsHandlers>;