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