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