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