Setup routing for process panel
[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 { loadCollectionPanel } from '~/store/collection-panel/collection-panel-action';
9 import { snackbarActions } from '../snackbar/snackbar-actions';
10 import { loadFavoritePanel } from '../favorite-panel/favorite-panel-action';
11 import { openProjectPanel, projectPanelActions } from '~/store/project-panel/project-panel-action';
12 import { activateSidePanelTreeItem, initSidePanelTree, SidePanelTreeCategory, loadSidePanelTreeProjects } from '../side-panel-tree/side-panel-tree-actions';
13 import { loadResource, updateResources } from '../resources/resources-actions';
14 import { favoritePanelActions } from '~/store/favorite-panel/favorite-panel-action';
15 import { projectPanelColumns } from '~/views/project-panel/project-panel';
16 import { favoritePanelColumns } from '~/views/favorite-panel/favorite-panel';
17 import { matchRootRoute } from '~/routes/routes';
18 import { setCollectionBreadcrumbs, setProjectBreadcrumbs, setSidePanelBreadcrumbs } from '../breadcrumbs/breadcrumbs-actions';
19 import { navigateToProject } from '../navigation/navigation-action';
20 import { MoveToFormDialogData } from '~/store/move-to-dialog/move-to-dialog';
21 import { ServiceRepository } from '~/services/services';
22 import { getResource } from '../resources/resources';
23 import { getProjectPanelCurrentUuid } from '../project-panel/project-panel-action';
24 import * as projectCreateActions from '~/store/projects/project-create-actions';
25 import * as projectMoveActions from '~/store/projects/project-move-actions';
26 import * as projectUpdateActions from '~/store/projects/project-update-actions';
27 import * as collectionCreateActions from '~/store/collections/collection-create-actions';
28 import * as collectionCopyActions from '~/store/collections/collection-copy-actions';
29 import * as collectionUpdateActions from '~/store/collections/collection-update-actions';
30 import * as collectionMoveActions from '~/store/collections/collection-move-actions';
31 import * as processesActions from '../processes/processes-actions';
32 import { getProcess } from '../processes/process';
33
34
35 export const loadWorkbench = () =>
36     async (dispatch: Dispatch, getState: () => RootState) => {
37         const { auth, router } = getState();
38         const { user } = auth;
39         if (user) {
40             const userResource = await dispatch<any>(loadResource(user.uuid));
41             if (userResource) {
42                 dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
43                 dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
44                 dispatch<any>(initSidePanelTree());
45                 if (router.location) {
46                     const match = matchRootRoute(router.location.pathname);
47                     if (match) {
48                         dispatch(navigateToProject(userResource.uuid));
49                     }
50                 }
51             } else {
52                 dispatch(userIsNotAuthenticated);
53             }
54         } else {
55             dispatch(userIsNotAuthenticated);
56         }
57     };
58
59 export const loadFavorites = () =>
60     (dispatch: Dispatch) => {
61         dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
62         dispatch<any>(loadFavoritePanel());
63         dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
64     };
65
66
67 export const loadProject = (uuid: string) =>
68     async (dispatch: Dispatch) => {
69         await dispatch<any>(activateSidePanelTreeItem(uuid));
70         dispatch<any>(setProjectBreadcrumbs(uuid));
71         dispatch<any>(openProjectPanel(uuid));
72         dispatch(loadDetailsPanel(uuid));
73     };
74
75 export const createProject = (data: projectCreateActions.ProjectCreateFormDialogData) =>
76     async (dispatch: Dispatch) => {
77         const newProject = await dispatch<any>(projectCreateActions.createProject(data));
78         if (newProject) {
79             dispatch(snackbarActions.OPEN_SNACKBAR({
80                 message: "Project has been successfully created.",
81                 hideDuration: 2000
82             }));
83             await dispatch<any>(loadSidePanelTreeProjects(newProject.ownerUuid));
84             dispatch<any>(reloadProjectMatchingUuid([newProject.ownerUuid]));
85         }
86     };
87
88 export const moveProject = (data: MoveToFormDialogData) =>
89     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
90         try {
91             const oldProject = getResource(data.uuid)(getState().resources);
92             const oldOwnerUuid = oldProject ? oldProject.ownerUuid : '';
93             const movedProject = await dispatch<any>(projectMoveActions.moveProject(data));
94             if (movedProject) {
95                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Project has been moved', hideDuration: 2000 }));
96                 if (oldProject) {
97                     await dispatch<any>(loadSidePanelTreeProjects(oldProject.ownerUuid));
98                 }
99                 dispatch<any>(reloadProjectMatchingUuid([oldOwnerUuid, movedProject.ownerUuid, movedProject.uuid]));
100             }
101         } catch (e) {
102             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
103         }
104     };
105
106 export const updateProject = (data: projectUpdateActions.ProjectUpdateFormDialogData) =>
107     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
108         const updatedProject = await dispatch<any>(projectUpdateActions.updateProject(data));
109         if (updatedProject) {
110             dispatch(snackbarActions.OPEN_SNACKBAR({
111                 message: "Project has been successfully updated.",
112                 hideDuration: 2000
113             }));
114             await dispatch<any>(loadSidePanelTreeProjects(updatedProject.ownerUuid));
115             dispatch<any>(reloadProjectMatchingUuid([updatedProject.ownerUuid, updatedProject.uuid]));
116         }
117     };
118
119 export const loadCollection = (uuid: string) =>
120     async (dispatch: Dispatch) => {
121         const collection = await dispatch<any>(loadCollectionPanel(uuid));
122         await dispatch<any>(activateSidePanelTreeItem(collection.ownerUuid));
123         dispatch<any>(setCollectionBreadcrumbs(collection.uuid));
124         dispatch(loadDetailsPanel(uuid));
125     };
126
127 export const createCollection = (data: collectionCreateActions.CollectionCreateFormDialogData) =>
128     async (dispatch: Dispatch) => {
129         const collection = await dispatch<any>(collectionCreateActions.createCollection(data));
130         if (collection) {
131             dispatch(snackbarActions.OPEN_SNACKBAR({
132                 message: "Collection has been successfully created.",
133                 hideDuration: 2000
134             }));
135             dispatch<any>(updateResources([collection]));
136             dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
137         }
138     };
139
140 export const updateCollection = (data: collectionUpdateActions.CollectionUpdateFormDialogData) =>
141     async (dispatch: Dispatch) => {
142         const collection = await dispatch<any>(collectionUpdateActions.updateCollection(data));
143         if (collection) {
144             dispatch(snackbarActions.OPEN_SNACKBAR({
145                 message: "Collection has been successfully updated.",
146                 hideDuration: 2000
147             }));
148             dispatch<any>(updateResources([collection]));
149             dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
150         }
151     };
152
153 export const copyCollection = (data: collectionCopyActions.CollectionCopyFormDialogData) =>
154     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
155         try {
156             const collection = await dispatch<any>(collectionCopyActions.copyCollection(data));
157             dispatch<any>(updateResources([collection]));
158             dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
159             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied.', hideDuration: 2000 }));
160         } catch (e) {
161             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
162         }
163     };
164
165 export const moveCollection = (data: MoveToFormDialogData) =>
166     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
167         try {
168             const collection = await dispatch<any>(collectionMoveActions.moveCollection(data));
169             dispatch<any>(updateResources([collection]));
170             dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
171             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been moved.', hideDuration: 2000 }));
172         } catch (e) {
173             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
174         }
175     };
176
177 export const loadProcess = (uuid: string) =>
178     async (dispatch: Dispatch, getState: () => RootState) => {
179         await dispatch<any>(processesActions.loadProcess(uuid));
180         const process = getProcess(uuid)(getState().resources);
181         if (process) {
182             await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
183             dispatch<any>(setCollectionBreadcrumbs(process.containerRequest.ownerUuid));
184             dispatch(loadDetailsPanel(uuid));
185         }
186     };
187
188 export const resourceIsNotLoaded = (uuid: string) =>
189     snackbarActions.OPEN_SNACKBAR({
190         message: `Resource identified by ${uuid} is not loaded.`
191     });
192
193 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
194     message: 'User is not authenticated'
195 });
196
197 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
198     message: 'Could not load user'
199 });
200
201 const reloadProjectMatchingUuid = (matchingUuids: string[]) =>
202     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
203         const currentProjectPanelUuid = getProjectPanelCurrentUuid(getState());
204         if (currentProjectPanelUuid && matchingUuids.some(uuid => uuid === currentProjectPanelUuid)) {
205             dispatch<any>(loadProject(currentProjectPanelUuid));
206         }
207     };