Update loadCollection action to handle trashed and shared collections
[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, setSidePanelBreadcrumbs, setProcessBreadcrumbs, setSharedWithMeBreadcrumbs, setTrashBreadcrumbs } 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 import { CopyFormDialogData } from '~/store/copy-dialog/copy-dialog';
42 import { progressIndicatorActions } from '~/store/progress-indicator/progress-indicator-actions';
43 import { getProgressIndicator } from '../progress-indicator/progress-indicator-reducer';
44 import { ResourceKind, extractUuidKind } from '~/models/resource';
45 import { FilterBuilder } from '~/services/api/filter-builder';
46 import { ProjectResource } from '~/models/project';
47 import { CollectionResource } from '~/models/collection';
48
49 export const WORKBENCH_LOADING_SCREEN = 'workbenchLoadingScreen';
50
51 export const isWorkbenchLoading = (state: RootState) => {
52     const progress = getProgressIndicator(WORKBENCH_LOADING_SCREEN)(state.progressIndicator);
53     return progress ? progress.working : false;
54 };
55
56 const handleFirstTimeLoad = (action: any) =>
57     async (dispatch: Dispatch<any>, getState: () => RootState) => {
58         try {
59             await dispatch(action);
60         } finally {
61             if (isWorkbenchLoading(getState())) {
62                 dispatch(progressIndicatorActions.STOP_WORKING(WORKBENCH_LOADING_SCREEN));
63             }
64         }
65     };
66
67
68 export const loadWorkbench = () =>
69     async (dispatch: Dispatch, getState: () => RootState) => {
70         dispatch(progressIndicatorActions.START_WORKING(WORKBENCH_LOADING_SCREEN));
71         const { auth, router } = getState();
72         const { user } = auth;
73         if (user) {
74             const userResource = await dispatch<any>(loadResource(user.uuid));
75             if (userResource) {
76                 dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
77                 dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
78                 dispatch(trashPanelActions.SET_COLUMNS({ columns: trashPanelColumns }));
79                 dispatch(sharedWithMePanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
80                 dispatch<any>(initSidePanelTree());
81                 if (router.location) {
82                     const match = matchRootRoute(router.location.pathname);
83                     if (match) {
84                         dispatch(navigateToProject(userResource.uuid));
85                     }
86                 }
87             } else {
88                 dispatch(userIsNotAuthenticated);
89             }
90         } else {
91             dispatch(userIsNotAuthenticated);
92         }
93     };
94
95 export const loadFavorites = () =>
96     handleFirstTimeLoad(
97         (dispatch: Dispatch) => {
98             dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
99             dispatch<any>(loadFavoritePanel());
100             dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
101         });
102
103 export const loadTrash = () =>
104     handleFirstTimeLoad(
105         (dispatch: Dispatch) => {
106             dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
107             dispatch<any>(loadTrashPanel());
108             dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.TRASH));
109         });
110
111 export const loadProject = (uuid: string) =>
112     handleFirstTimeLoad(
113         async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
114             const userUuid = services.authService.getUuid();
115             if (userUuid) {
116                 let project: ProjectResource | null = null;
117                 if (userUuid !== uuid) {
118                     /**
119                      * Use of /group/contents API is the only way to get trashed item
120                      * A get method of a service will throw an exception with 404 status for resources that are trashed
121                      */
122                     const resource = await loadGroupContentsResource(uuid, userUuid, services);
123                     if (resource) {
124                         if (resource.kind === ResourceKind.PROJECT) {
125                             project = resource;
126                             if (project.isTrashed) {
127                                 dispatch<any>(setTrashBreadcrumbs(uuid));
128                                 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
129                             } else {
130                                 await dispatch(activateSidePanelTreeItem(uuid));
131                                 dispatch<any>(setSidePanelBreadcrumbs(uuid));
132                             }
133                         }
134                     } else {
135                         /**
136                          * If item is not accesible using loadGroupContentsResource,
137                          * but it can be obtained using the get method of the service
138                          * then it is shared with the user
139                          */
140                         project = await services.projectService.get(uuid);
141                         if (project) {
142                             dispatch<any>(setSharedWithMeBreadcrumbs(uuid));
143                             dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
144                         }
145                     }
146                     if (project) {
147                         dispatch(updateResources([project]));
148                     }
149                 } else {
150                     await dispatch(activateSidePanelTreeItem(userUuid));
151                     dispatch<any>(setSidePanelBreadcrumbs(userUuid));
152                 }
153                 if (project) {
154                     dispatch(openProjectPanel(uuid));
155                     dispatch(loadDetailsPanel(uuid));
156                 }
157             }
158         });
159
160 export const createProject = (data: projectCreateActions.ProjectCreateFormDialogData) =>
161     async (dispatch: Dispatch) => {
162         const newProject = await dispatch<any>(projectCreateActions.createProject(data));
163         if (newProject) {
164             dispatch(snackbarActions.OPEN_SNACKBAR({
165                 message: "Project has been successfully created.",
166                 hideDuration: 2000
167             }));
168             await dispatch<any>(loadSidePanelTreeProjects(newProject.ownerUuid));
169             dispatch<any>(reloadProjectMatchingUuid([newProject.ownerUuid]));
170         }
171     };
172
173 export const moveProject = (data: MoveToFormDialogData) =>
174     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
175         try {
176             const oldProject = getResource(data.uuid)(getState().resources);
177             const oldOwnerUuid = oldProject ? oldProject.ownerUuid : '';
178             const movedProject = await dispatch<any>(projectMoveActions.moveProject(data));
179             if (movedProject) {
180                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Project has been moved', hideDuration: 2000 }));
181                 if (oldProject) {
182                     await dispatch<any>(loadSidePanelTreeProjects(oldProject.ownerUuid));
183                 }
184                 dispatch<any>(reloadProjectMatchingUuid([oldOwnerUuid, movedProject.ownerUuid, movedProject.uuid]));
185             }
186         } catch (e) {
187             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
188         }
189     };
190
191 export const updateProject = (data: projectUpdateActions.ProjectUpdateFormDialogData) =>
192     async (dispatch: Dispatch) => {
193         const updatedProject = await dispatch<any>(projectUpdateActions.updateProject(data));
194         if (updatedProject) {
195             dispatch(snackbarActions.OPEN_SNACKBAR({
196                 message: "Project has been successfully updated.",
197                 hideDuration: 2000
198             }));
199             await dispatch<any>(loadSidePanelTreeProjects(updatedProject.ownerUuid));
200             dispatch<any>(reloadProjectMatchingUuid([updatedProject.ownerUuid, updatedProject.uuid]));
201         }
202     };
203
204 export const loadCollection = (uuid: string) =>
205     handleFirstTimeLoad(
206         async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
207             const userUuid = services.authService.getUuid();
208             if (userUuid) {
209                 let collection: CollectionResource | null = null;
210
211                 if (extractUuidKind(uuid) === ResourceKind.COLLECTION) {
212                     /**
213                      * Use of /group/contents API is the only way to get trashed item
214                      * A get method of a service will throw an exception with 404 status for resources that are trashed
215                      */
216                     const resource = await loadGroupContentsResource(uuid, userUuid, services);
217                     if (resource) {
218                         if (resource.kind === ResourceKind.COLLECTION) {
219                             collection = resource;
220                             if (collection.isTrashed) {
221                                 dispatch(setTrashBreadcrumbs(''));
222                                 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
223                             } else {
224                                 await dispatch(activateSidePanelTreeItem(collection.ownerUuid));
225                                 dispatch(setSidePanelBreadcrumbs(collection.ownerUuid));
226                             }
227                         }
228                     } else {
229                         /**
230                          * If item is not accesible using loadGroupContentsResource,
231                          * but it can be obtained using the get method of the service
232                          * then it is shared with the user
233                          */
234                         collection = await services.collectionService.get(uuid);
235                         if (collection) {
236                             dispatch<any>(setSharedWithMeBreadcrumbs(collection.ownerUuid));
237                             dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
238                         }
239                     }
240                     if (collection) {
241                         dispatch(updateResources([collection]));
242                     }
243                 }
244             }
245         });
246
247 export const createCollection = (data: collectionCreateActions.CollectionCreateFormDialogData) =>
248     async (dispatch: Dispatch) => {
249         const collection = await dispatch<any>(collectionCreateActions.createCollection(data));
250         if (collection) {
251             dispatch(snackbarActions.OPEN_SNACKBAR({
252                 message: "Collection has been successfully created.",
253                 hideDuration: 2000
254             }));
255             dispatch<any>(updateResources([collection]));
256             dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
257         }
258     };
259
260 export const updateCollection = (data: collectionUpdateActions.CollectionUpdateFormDialogData) =>
261     async (dispatch: Dispatch) => {
262         const collection = await dispatch<any>(collectionUpdateActions.updateCollection(data));
263         if (collection) {
264             dispatch(snackbarActions.OPEN_SNACKBAR({
265                 message: "Collection has been successfully updated.",
266                 hideDuration: 2000
267             }));
268             dispatch<any>(updateResources([collection]));
269             dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
270         }
271     };
272
273 export const copyCollection = (data: CopyFormDialogData) =>
274     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
275         try {
276             const collection = await dispatch<any>(collectionCopyActions.copyCollection(data));
277             dispatch<any>(updateResources([collection]));
278             dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
279             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied.', hideDuration: 2000 }));
280         } catch (e) {
281             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
282         }
283     };
284
285 export const moveCollection = (data: MoveToFormDialogData) =>
286     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
287         try {
288             const collection = await dispatch<any>(collectionMoveActions.moveCollection(data));
289             dispatch<any>(updateResources([collection]));
290             dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
291             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been moved.', hideDuration: 2000 }));
292         } catch (e) {
293             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
294         }
295     };
296
297 export const loadProcess = (uuid: string) =>
298     handleFirstTimeLoad(
299         async (dispatch: Dispatch, getState: () => RootState) => {
300             dispatch<any>(loadProcessPanel(uuid));
301             const process = await dispatch<any>(processesActions.loadProcess(uuid));
302             await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
303             dispatch<any>(setProcessBreadcrumbs(uuid));
304             dispatch(loadDetailsPanel(uuid));
305         });
306
307 export const updateProcess = (data: processUpdateActions.ProcessUpdateFormDialogData) =>
308     async (dispatch: Dispatch) => {
309         try {
310             const process = await dispatch<any>(processUpdateActions.updateProcess(data));
311             if (process) {
312                 dispatch(snackbarActions.OPEN_SNACKBAR({
313                     message: "Process has been successfully updated.",
314                     hideDuration: 2000
315                 }));
316                 dispatch<any>(updateResources([process]));
317                 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
318             }
319         } catch (e) {
320             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
321         }
322     };
323
324 export const moveProcess = (data: MoveToFormDialogData) =>
325     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
326         try {
327             const process = await dispatch<any>(processMoveActions.moveProcess(data));
328             dispatch<any>(updateResources([process]));
329             dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
330             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been moved.', hideDuration: 2000 }));
331         } catch (e) {
332             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
333         }
334     };
335
336 export const copyProcess = (data: CopyFormDialogData) =>
337     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
338         try {
339             const process = await dispatch<any>(processCopyActions.copyProcess(data));
340             dispatch<any>(updateResources([process]));
341             dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
342             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been copied.', hideDuration: 2000 }));
343         } catch (e) {
344             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
345         }
346     };
347
348 export const loadProcessLog = (uuid: string) =>
349     handleFirstTimeLoad(
350         async (dispatch: Dispatch) => {
351             const process = await dispatch<any>(processesActions.loadProcess(uuid));
352             dispatch<any>(setProcessBreadcrumbs(uuid));
353             dispatch<any>(initProcessLogsPanel(uuid));
354             await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
355         });
356
357 export const resourceIsNotLoaded = (uuid: string) =>
358     snackbarActions.OPEN_SNACKBAR({
359         message: `Resource identified by ${uuid} is not loaded.`
360     });
361
362 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
363     message: 'User is not authenticated'
364 });
365
366 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
367     message: 'Could not load user'
368 });
369
370 export const reloadProjectMatchingUuid = (matchingUuids: string[]) =>
371     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
372         const currentProjectPanelUuid = getProjectPanelCurrentUuid(getState());
373         if (currentProjectPanelUuid && matchingUuids.some(uuid => uuid === currentProjectPanelUuid)) {
374             dispatch<any>(loadProject(currentProjectPanelUuid));
375         }
376     };
377
378 export const loadSharedWithMe = handleFirstTimeLoad(async (dispatch: Dispatch) => {
379     dispatch<any>(loadSharedWithMePanel());
380     await dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
381     await dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.SHARED_WITH_ME));
382 });
383
384 const loadGroupContentsResource = async (uuid: string, ownerUuid: string, services: ServiceRepository) => {
385
386     const filters = new FilterBuilder()
387         .addEqual('uuid', uuid)
388         .getFilters();
389
390     const { items } = await services.groupsService.contents(ownerUuid, {
391         filters,
392         recursive: true,
393         includeTrash: true,
394     });
395
396     return items.shift();
397
398 };