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