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