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