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