workflow-view-without-working-services
[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, getSidePanelTreeNodeAncestorsIds } 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, setProcessBreadcrumbs, setSharedWithMeBreadcrumbs } 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 * as processMoveActions from '~/store/processes/process-move-actions';
33 import * as processUpdateActions from '~/store/processes/process-update-actions';
34 import * as processCopyActions from '~/store/processes/process-copy-actions';
35 import { trashPanelColumns } from "~/views/trash-panel/trash-panel";
36 import { loadTrashPanel, trashPanelActions } from "~/store/trash-panel/trash-panel-action";
37 import { initProcessLogsPanel } from '../process-logs-panel/process-logs-panel-actions';
38 import { loadProcessPanel } from '~/store/process-panel/process-panel-actions';
39 import { sharedWithMePanelActions } from '~/store/shared-with-me-panel/shared-with-me-panel-actions';
40 import { loadSharedWithMePanel } from '../shared-with-me-panel/shared-with-me-panel-actions';
41 import { CopyFormDialogData } from '~/store/copy-dialog/copy-dialog';
42 import { loadWorkflowPanel, workflowPanelActions } from '~/store/workflow-panel/workflow-panel-actions';
43 import { workflowPanelColumns } from '~/views/workflow-panel/workflow-panel';
44
45 export const loadWorkbench = () =>
46     async (dispatch: Dispatch, getState: () => RootState) => {
47         const { auth, router } = getState();
48         const { user } = auth;
49         if (user) {
50             const userResource = await dispatch<any>(loadResource(user.uuid));
51             if (userResource) {
52                 dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
53                 dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
54                 dispatch(trashPanelActions.SET_COLUMNS({ columns: trashPanelColumns }));
55                 dispatch(sharedWithMePanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
56                 dispatch(workflowPanelActions.SET_COLUMNS({ columns: workflowPanelColumns}));
57                 dispatch<any>(initSidePanelTree());
58                 if (router.location) {
59                     const match = matchRootRoute(router.location.pathname);
60                     if (match) {
61                         dispatch(navigateToProject(userResource.uuid));
62                     }
63                 }
64             } else {
65                 dispatch(userIsNotAuthenticated);
66             }
67         } else {
68             dispatch(userIsNotAuthenticated);
69         }
70     };
71
72 export const loadFavorites = () =>
73     (dispatch: Dispatch) => {
74         dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
75         dispatch<any>(loadFavoritePanel());
76         dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
77     };
78
79 export const loadTrash = () =>
80     (dispatch: Dispatch) => {
81         dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
82         dispatch<any>(loadTrashPanel());
83         dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.TRASH));
84     };
85
86 export const loadProject = (uuid: string) =>
87     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
88         dispatch(openProjectPanel(uuid));
89         await dispatch(activateSidePanelTreeItem(uuid));
90         dispatch(setProjectBreadcrumbs(uuid));
91         dispatch(loadDetailsPanel(uuid));
92     };
93
94 export const createProject = (data: projectCreateActions.ProjectCreateFormDialogData) =>
95     async (dispatch: Dispatch) => {
96         const newProject = await dispatch<any>(projectCreateActions.createProject(data));
97         if (newProject) {
98             dispatch(snackbarActions.OPEN_SNACKBAR({
99                 message: "Project has been successfully created.",
100                 hideDuration: 2000
101             }));
102             await dispatch<any>(loadSidePanelTreeProjects(newProject.ownerUuid));
103             dispatch<any>(reloadProjectMatchingUuid([newProject.ownerUuid]));
104         }
105     };
106
107 export const moveProject = (data: MoveToFormDialogData) =>
108     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
109         try {
110             const oldProject = getResource(data.uuid)(getState().resources);
111             const oldOwnerUuid = oldProject ? oldProject.ownerUuid : '';
112             const movedProject = await dispatch<any>(projectMoveActions.moveProject(data));
113             if (movedProject) {
114                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Project has been moved', hideDuration: 2000 }));
115                 if (oldProject) {
116                     await dispatch<any>(loadSidePanelTreeProjects(oldProject.ownerUuid));
117                 }
118                 dispatch<any>(reloadProjectMatchingUuid([oldOwnerUuid, movedProject.ownerUuid, movedProject.uuid]));
119             }
120         } catch (e) {
121             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
122         }
123     };
124
125 export const updateProject = (data: projectUpdateActions.ProjectUpdateFormDialogData) =>
126     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
127         const updatedProject = await dispatch<any>(projectUpdateActions.updateProject(data));
128         if (updatedProject) {
129             dispatch(snackbarActions.OPEN_SNACKBAR({
130                 message: "Project has been successfully updated.",
131                 hideDuration: 2000
132             }));
133             await dispatch<any>(loadSidePanelTreeProjects(updatedProject.ownerUuid));
134             dispatch<any>(reloadProjectMatchingUuid([updatedProject.ownerUuid, updatedProject.uuid]));
135         }
136     };
137
138 export const loadCollection = (uuid: string) =>
139     async (dispatch: Dispatch) => {
140         const collection = await dispatch<any>(loadCollectionPanel(uuid));
141         await dispatch<any>(activateSidePanelTreeItem(collection.ownerUuid));
142         dispatch<any>(setCollectionBreadcrumbs(collection.uuid));
143         dispatch(loadDetailsPanel(uuid));
144     };
145
146 export const createCollection = (data: collectionCreateActions.CollectionCreateFormDialogData) =>
147     async (dispatch: Dispatch) => {
148         const collection = await dispatch<any>(collectionCreateActions.createCollection(data));
149         if (collection) {
150             dispatch(snackbarActions.OPEN_SNACKBAR({
151                 message: "Collection has been successfully created.",
152                 hideDuration: 2000
153             }));
154             dispatch<any>(updateResources([collection]));
155             dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
156         }
157     };
158
159 export const updateCollection = (data: collectionUpdateActions.CollectionUpdateFormDialogData) =>
160     async (dispatch: Dispatch) => {
161         const collection = await dispatch<any>(collectionUpdateActions.updateCollection(data));
162         if (collection) {
163             dispatch(snackbarActions.OPEN_SNACKBAR({
164                 message: "Collection has been successfully updated.",
165                 hideDuration: 2000
166             }));
167             dispatch<any>(updateResources([collection]));
168             dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
169         }
170     };
171
172 export const copyCollection = (data: CopyFormDialogData) =>
173     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
174         try {
175             const collection = await dispatch<any>(collectionCopyActions.copyCollection(data));
176             dispatch<any>(updateResources([collection]));
177             dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
178             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied.', hideDuration: 2000 }));
179         } catch (e) {
180             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
181         }
182     };
183
184 export const moveCollection = (data: MoveToFormDialogData) =>
185     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
186         try {
187             const collection = await dispatch<any>(collectionMoveActions.moveCollection(data));
188             dispatch<any>(updateResources([collection]));
189             dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
190             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been moved.', hideDuration: 2000 }));
191         } catch (e) {
192             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
193         }
194     };
195
196 export const loadProcess = (uuid: string) =>
197     async (dispatch: Dispatch, getState: () => RootState) => {
198         dispatch<any>(loadProcessPanel(uuid));
199         const process = await dispatch<any>(processesActions.loadProcess(uuid));
200         await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
201         dispatch<any>(setProcessBreadcrumbs(uuid));
202         dispatch(loadDetailsPanel(uuid));
203
204     };
205
206 export const updateProcess = (data: processUpdateActions.ProcessUpdateFormDialogData) =>
207     async (dispatch: Dispatch) => {
208         try {
209             const process = await dispatch<any>(processUpdateActions.updateProcess(data));
210             if (process) {
211                 dispatch(snackbarActions.OPEN_SNACKBAR({
212                     message: "Process has been successfully updated.",
213                     hideDuration: 2000
214                 }));
215                 dispatch<any>(updateResources([process]));
216                 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
217             }
218         } catch (e) {
219             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
220         }
221     };
222
223 export const moveProcess = (data: MoveToFormDialogData) =>
224     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
225         try {
226             const process = await dispatch<any>(processMoveActions.moveProcess(data));
227             dispatch<any>(updateResources([process]));
228             dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
229             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been moved.', hideDuration: 2000 }));
230         } catch (e) {
231             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
232         }
233     };
234
235 export const copyProcess = (data: CopyFormDialogData) =>
236     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
237         try {
238             const process = await dispatch<any>(processCopyActions.copyProcess(data));
239             dispatch<any>(updateResources([process]));
240             dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
241             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been copied.', hideDuration: 2000 }));
242         } catch (e) {
243             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
244         }
245     };
246
247 export const loadProcessLog = (uuid: string) =>
248     async (dispatch: Dispatch) => {
249         const process = await dispatch<any>(processesActions.loadProcess(uuid));
250         dispatch<any>(setProcessBreadcrumbs(uuid));
251         dispatch<any>(initProcessLogsPanel(uuid));
252         await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
253     };
254
255 export const resourceIsNotLoaded = (uuid: string) =>
256     snackbarActions.OPEN_SNACKBAR({
257         message: `Resource identified by ${uuid} is not loaded.`
258     });
259
260 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
261     message: 'User is not authenticated'
262 });
263
264 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
265     message: 'Could not load user'
266 });
267
268 export const reloadProjectMatchingUuid = (matchingUuids: string[]) =>
269     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
270         const currentProjectPanelUuid = getProjectPanelCurrentUuid(getState());
271         if (currentProjectPanelUuid && matchingUuids.some(uuid => uuid === currentProjectPanelUuid)) {
272             dispatch<any>(loadProject(currentProjectPanelUuid));
273         }
274     };
275
276 export const loadSharedWithMe = (dispatch: Dispatch) => {
277     dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
278     dispatch<any>(loadSharedWithMePanel());
279     dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.SHARED_WITH_ME));
280 };
281
282 export const loadWorkflow = (dispatch: Dispatch<any>) => {
283     dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.WORKFLOWS));
284     dispatch(loadWorkflowPanel());
285     dispatch(setSidePanelBreadcrumbs(SidePanelTreeCategory.WORKFLOWS));
286 };