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