Implement breadcrumbs for shared with me panel
[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         const ancestors = getSidePanelTreeNodeAncestorsIds(uuid)(getState().treePicker);
89         if (ancestors.find(uuid => uuid === services.authService.getUuid())) {
90             dispatch(setProjectBreadcrumbs(uuid));
91         } else {
92             dispatch(setSharedWithMeBreadcrumbs(uuid));
93             dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
94         }
95         dispatch(loadDetailsPanel(uuid));
96     };
97
98 export const createProject = (data: projectCreateActions.ProjectCreateFormDialogData) =>
99     async (dispatch: Dispatch) => {
100         const newProject = await dispatch<any>(projectCreateActions.createProject(data));
101         if (newProject) {
102             dispatch(snackbarActions.OPEN_SNACKBAR({
103                 message: "Project has been successfully created.",
104                 hideDuration: 2000
105             }));
106             await dispatch<any>(loadSidePanelTreeProjects(newProject.ownerUuid));
107             dispatch<any>(reloadProjectMatchingUuid([newProject.ownerUuid]));
108         }
109     };
110
111 export const moveProject = (data: MoveToFormDialogData) =>
112     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
113         try {
114             const oldProject = getResource(data.uuid)(getState().resources);
115             const oldOwnerUuid = oldProject ? oldProject.ownerUuid : '';
116             const movedProject = await dispatch<any>(projectMoveActions.moveProject(data));
117             if (movedProject) {
118                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Project has been moved', hideDuration: 2000 }));
119                 if (oldProject) {
120                     await dispatch<any>(loadSidePanelTreeProjects(oldProject.ownerUuid));
121                 }
122                 dispatch<any>(reloadProjectMatchingUuid([oldOwnerUuid, movedProject.ownerUuid, movedProject.uuid]));
123             }
124         } catch (e) {
125             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
126         }
127     };
128
129 export const updateProject = (data: projectUpdateActions.ProjectUpdateFormDialogData) =>
130     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
131         const updatedProject = await dispatch<any>(projectUpdateActions.updateProject(data));
132         if (updatedProject) {
133             dispatch(snackbarActions.OPEN_SNACKBAR({
134                 message: "Project has been successfully updated.",
135                 hideDuration: 2000
136             }));
137             await dispatch<any>(loadSidePanelTreeProjects(updatedProject.ownerUuid));
138             dispatch<any>(reloadProjectMatchingUuid([updatedProject.ownerUuid, updatedProject.uuid]));
139         }
140     };
141
142 export const loadCollection = (uuid: string) =>
143     async (dispatch: Dispatch) => {
144         const collection = await dispatch<any>(loadCollectionPanel(uuid));
145         await dispatch<any>(activateSidePanelTreeItem(collection.ownerUuid));
146         dispatch<any>(setCollectionBreadcrumbs(collection.uuid));
147         dispatch(loadDetailsPanel(uuid));
148     };
149
150 export const createCollection = (data: collectionCreateActions.CollectionCreateFormDialogData) =>
151     async (dispatch: Dispatch) => {
152         const collection = await dispatch<any>(collectionCreateActions.createCollection(data));
153         if (collection) {
154             dispatch(snackbarActions.OPEN_SNACKBAR({
155                 message: "Collection has been successfully created.",
156                 hideDuration: 2000
157             }));
158             dispatch<any>(updateResources([collection]));
159             dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
160         }
161     };
162
163 export const updateCollection = (data: collectionUpdateActions.CollectionUpdateFormDialogData) =>
164     async (dispatch: Dispatch) => {
165         const collection = await dispatch<any>(collectionUpdateActions.updateCollection(data));
166         if (collection) {
167             dispatch(snackbarActions.OPEN_SNACKBAR({
168                 message: "Collection has been successfully updated.",
169                 hideDuration: 2000
170             }));
171             dispatch<any>(updateResources([collection]));
172             dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
173         }
174     };
175
176 export const copyCollection = (data: CopyFormDialogData) =>
177     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
178         try {
179             const collection = await dispatch<any>(collectionCopyActions.copyCollection(data));
180             dispatch<any>(updateResources([collection]));
181             dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
182             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied.', hideDuration: 2000 }));
183         } catch (e) {
184             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
185         }
186     };
187
188 export const moveCollection = (data: MoveToFormDialogData) =>
189     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
190         try {
191             const collection = await dispatch<any>(collectionMoveActions.moveCollection(data));
192             dispatch<any>(updateResources([collection]));
193             dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
194             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been moved.', hideDuration: 2000 }));
195         } catch (e) {
196             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
197         }
198     };
199
200 export const loadProcess = (uuid: string) =>
201     async (dispatch: Dispatch, getState: () => RootState) => {
202         dispatch<any>(loadProcessPanel(uuid));
203         const process = await dispatch<any>(processesActions.loadProcess(uuid));
204         await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
205         dispatch<any>(setProcessBreadcrumbs(uuid));
206         dispatch(loadDetailsPanel(uuid));
207
208     };
209
210 export const updateProcess = (data: processUpdateActions.ProcessUpdateFormDialogData) =>
211     async (dispatch: Dispatch) => {
212         try {
213             const process = await dispatch<any>(processUpdateActions.updateProcess(data));
214             if (process) {
215                 dispatch(snackbarActions.OPEN_SNACKBAR({
216                     message: "Process has been successfully updated.",
217                     hideDuration: 2000
218                 }));
219                 dispatch<any>(updateResources([process]));
220                 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
221             }
222         } catch (e) {
223             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
224         }
225     };
226
227 export const moveProcess = (data: MoveToFormDialogData) =>
228     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
229         try {
230             const process = await dispatch<any>(processMoveActions.moveProcess(data));
231             dispatch<any>(updateResources([process]));
232             dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
233             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been moved.', hideDuration: 2000 }));
234         } catch (e) {
235             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
236         }
237     };
238
239 export const copyProcess = (data: CopyFormDialogData) =>
240     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
241         try {
242             const process = await dispatch<any>(processCopyActions.copyProcess(data));
243             dispatch<any>(updateResources([process]));
244             dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
245             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been copied.', hideDuration: 2000 }));
246         } catch (e) {
247             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
248         }
249     };
250
251 export const loadProcessLog = (uuid: string) =>
252     async (dispatch: Dispatch) => {
253         const process = await dispatch<any>(processesActions.loadProcess(uuid));
254         dispatch<any>(setProcessBreadcrumbs(uuid));
255         dispatch<any>(initProcessLogsPanel(uuid));
256         await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
257     };
258
259 export const resourceIsNotLoaded = (uuid: string) =>
260     snackbarActions.OPEN_SNACKBAR({
261         message: `Resource identified by ${uuid} is not loaded.`
262     });
263
264 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
265     message: 'User is not authenticated'
266 });
267
268 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
269     message: 'Could not load user'
270 });
271
272 export const reloadProjectMatchingUuid = (matchingUuids: string[]) =>
273     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
274         const currentProjectPanelUuid = getProjectPanelCurrentUuid(getState());
275         if (currentProjectPanelUuid && matchingUuids.some(uuid => uuid === currentProjectPanelUuid)) {
276             dispatch<any>(loadProject(currentProjectPanelUuid));
277         }
278     };
279
280 export const loadSharedWithMe = (dispatch: Dispatch) => {
281     dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
282     dispatch<any>(loadSharedWithMePanel());
283     dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.SHARED_WITH_ME));
284 };