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