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