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