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