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