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