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