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