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