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