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