1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch } from 'redux';
6 import { RootState } from "../store";
7 import { loadDetailsPanel } from '~/store/details-panel/details-panel-action';
8 import { snackbarActions } from '../snackbar/snackbar-actions';
9 import { loadFavoritePanel } from '../favorite-panel/favorite-panel-action';
10 import { openProjectPanel, projectPanelActions, setIsProjectPanelTrashed } from '~/store/project-panel/project-panel-action';
11 import { activateSidePanelTreeItem, initSidePanelTree, SidePanelTreeCategory, loadSidePanelTreeProjects } from '../side-panel-tree/side-panel-tree-actions';
12 import { loadResource, updateResources } from '../resources/resources-actions';
13 import { favoritePanelActions } from '~/store/favorite-panel/favorite-panel-action';
14 import { projectPanelColumns } from '~/views/project-panel/project-panel';
15 import { favoritePanelColumns } from '~/views/favorite-panel/favorite-panel';
16 import { matchRootRoute } from '~/routes/routes';
17 import { setSidePanelBreadcrumbs, setProcessBreadcrumbs, setSharedWithMeBreadcrumbs, setTrashBreadcrumbs } from '../breadcrumbs/breadcrumbs-actions';
18 import { navigateToProject } from '../navigation/navigation-action';
19 import { MoveToFormDialogData } from '~/store/move-to-dialog/move-to-dialog';
20 import { ServiceRepository } from '~/services/services';
21 import { getResource } from '../resources/resources';
22 import { getProjectPanelCurrentUuid } from '../project-panel/project-panel-action';
23 import * as projectCreateActions from '~/store/projects/project-create-actions';
24 import * as projectMoveActions from '~/store/projects/project-move-actions';
25 import * as projectUpdateActions from '~/store/projects/project-update-actions';
26 import * as collectionCreateActions from '~/store/collections/collection-create-actions';
27 import * as collectionCopyActions from '~/store/collections/collection-copy-actions';
28 import * as collectionUpdateActions from '~/store/collections/collection-update-actions';
29 import * as collectionMoveActions from '~/store/collections/collection-move-actions';
30 import * as processesActions from '../processes/processes-actions';
31 import * as processMoveActions from '~/store/processes/process-move-actions';
32 import * as processUpdateActions from '~/store/processes/process-update-actions';
33 import * as processCopyActions from '~/store/processes/process-copy-actions';
34 import { trashPanelColumns } from "~/views/trash-panel/trash-panel";
35 import { loadTrashPanel, trashPanelActions } from "~/store/trash-panel/trash-panel-action";
36 import { initProcessLogsPanel } from '../process-logs-panel/process-logs-panel-actions';
37 import { loadProcessPanel } from '~/store/process-panel/process-panel-actions';
38 import { sharedWithMePanelActions } from '~/store/shared-with-me-panel/shared-with-me-panel-actions';
39 import { loadSharedWithMePanel } from '../shared-with-me-panel/shared-with-me-panel-actions';
40 import { CopyFormDialogData } from '~/store/copy-dialog/copy-dialog';
41 import { loadWorkflowPanel, workflowPanelActions } from '~/store/workflow-panel/workflow-panel-actions';
42 import { workflowPanelColumns } from '~/views/workflow-panel/workflow-panel-view';
43 import { progressIndicatorActions } from '~/store/progress-indicator/progress-indicator-actions';
44 import { getProgressIndicator } from '../progress-indicator/progress-indicator-reducer';
45 import { ResourceKind, extractUuidKind } from '~/models/resource';
46 import { FilterBuilder } from '~/services/api/filter-builder';
47 import { GroupContentsResource } from '~/services/groups-service/groups-service';
48 import { unionize, ofType, UnionOf, MatchCases } from '~/common/unionize';
49 import { loadRunProcessPanel } from '~/store/run-process-panel/run-process-panel-actions';
50 import { loadCollectionFiles } from '~/store/collection-panel/collection-panel-files/collection-panel-files-actions';
51 import { SnackbarKind } from '~/store/snackbar/snackbar-actions';
52 import { collectionPanelActions } from "~/store/collection-panel/collection-panel-action";
53 import { CollectionResource } from "~/models/collection";
54 import { searchResultsPanelActions, loadSearchResultsPanel } from '~/store/search-results-panel/search-results-panel-actions';
55 import { searchResultsPanelColumns } from '~/views/search-results-panel/search-results-panel-view';
57 export const WORKBENCH_LOADING_SCREEN = 'workbenchLoadingScreen';
59 export const isWorkbenchLoading = (state: RootState) => {
60 const progress = getProgressIndicator(WORKBENCH_LOADING_SCREEN)(state.progressIndicator);
61 return progress ? progress.working : false;
64 const handleFirstTimeLoad = (action: any) =>
65 async (dispatch: Dispatch<any>, getState: () => RootState) => {
67 await dispatch(action);
69 if (isWorkbenchLoading(getState())) {
70 dispatch(progressIndicatorActions.STOP_WORKING(WORKBENCH_LOADING_SCREEN));
76 export const loadWorkbench = () =>
77 async (dispatch: Dispatch, getState: () => RootState) => {
78 dispatch(progressIndicatorActions.START_WORKING(WORKBENCH_LOADING_SCREEN));
79 const { auth, router } = getState();
80 const { user } = auth;
82 const userResource = await dispatch<any>(loadResource(user.uuid));
84 dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
85 dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
86 dispatch(trashPanelActions.SET_COLUMNS({ columns: trashPanelColumns }));
87 dispatch(sharedWithMePanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
88 dispatch(workflowPanelActions.SET_COLUMNS({ columns: workflowPanelColumns }));
89 dispatch(searchResultsPanelActions.SET_COLUMNS({ columns: searchResultsPanelColumns }));
90 dispatch<any>(initSidePanelTree());
91 if (router.location) {
92 const match = matchRootRoute(router.location.pathname);
94 dispatch(navigateToProject(userResource.uuid));
98 dispatch(userIsNotAuthenticated);
101 dispatch(userIsNotAuthenticated);
105 export const loadFavorites = () =>
107 (dispatch: Dispatch) => {
108 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
109 dispatch<any>(loadFavoritePanel());
110 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
113 export const loadTrash = () =>
115 (dispatch: Dispatch) => {
116 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
117 dispatch<any>(loadTrashPanel());
118 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.TRASH));
121 export const loadProject = (uuid: string) =>
123 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
124 const userUuid = services.authService.getUuid();
125 dispatch(setIsProjectPanelTrashed(false));
127 if (userUuid !== uuid) {
128 const match = await loadGroupContentsResource({ uuid, userUuid, services });
130 OWNED: async project => {
131 await dispatch(activateSidePanelTreeItem(uuid));
132 dispatch<any>(setSidePanelBreadcrumbs(uuid));
133 dispatch(finishLoadingProject(project));
136 dispatch<any>(setSharedWithMeBreadcrumbs(uuid));
137 dispatch(activateSidePanelTreeItem(uuid));
138 dispatch(finishLoadingProject(project));
140 TRASHED: project => {
141 dispatch<any>(setTrashBreadcrumbs(uuid));
142 dispatch(setIsProjectPanelTrashed(true));
143 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
144 dispatch(finishLoadingProject(project));
148 await dispatch(activateSidePanelTreeItem(userUuid));
149 dispatch<any>(setSidePanelBreadcrumbs(userUuid));
150 dispatch(finishLoadingProject(userUuid));
155 export const createProject = (data: projectCreateActions.ProjectCreateFormDialogData) =>
156 async (dispatch: Dispatch) => {
157 const newProject = await dispatch<any>(projectCreateActions.createProject(data));
159 dispatch(snackbarActions.OPEN_SNACKBAR({
160 message: "Project has been successfully created.",
163 await dispatch<any>(loadSidePanelTreeProjects(newProject.ownerUuid));
164 dispatch<any>(reloadProjectMatchingUuid([newProject.ownerUuid]));
168 export const moveProject = (data: MoveToFormDialogData) =>
169 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
171 const oldProject = getResource(data.uuid)(getState().resources);
172 const oldOwnerUuid = oldProject ? oldProject.ownerUuid : '';
173 const movedProject = await dispatch<any>(projectMoveActions.moveProject(data));
175 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Project has been moved', hideDuration: 2000 }));
177 await dispatch<any>(loadSidePanelTreeProjects(oldProject.ownerUuid));
179 dispatch<any>(reloadProjectMatchingUuid([oldOwnerUuid, movedProject.ownerUuid, movedProject.uuid]));
182 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
186 export const updateProject = (data: projectUpdateActions.ProjectUpdateFormDialogData) =>
187 async (dispatch: Dispatch) => {
188 const updatedProject = await dispatch<any>(projectUpdateActions.updateProject(data));
189 if (updatedProject) {
190 dispatch(snackbarActions.OPEN_SNACKBAR({
191 message: "Project has been successfully updated.",
194 await dispatch<any>(loadSidePanelTreeProjects(updatedProject.ownerUuid));
195 dispatch<any>(reloadProjectMatchingUuid([updatedProject.ownerUuid, updatedProject.uuid]));
199 export const loadCollection = (uuid: string) =>
201 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
202 const userUuid = services.authService.getUuid();
204 const match = await loadGroupContentsResource({ uuid, userUuid, services });
206 OWNED: async collection => {
207 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
208 dispatch(updateResources([collection]));
209 await dispatch(activateSidePanelTreeItem(collection.ownerUuid));
210 dispatch(setSidePanelBreadcrumbs(collection.ownerUuid));
211 dispatch(loadCollectionFiles(collection.uuid));
213 SHARED: collection => {
214 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
215 dispatch(updateResources([collection]));
216 dispatch<any>(setSharedWithMeBreadcrumbs(collection.ownerUuid));
217 dispatch(activateSidePanelTreeItem(collection.ownerUuid));
218 dispatch(loadCollectionFiles(collection.uuid));
220 TRASHED: collection => {
221 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
222 dispatch(updateResources([collection]));
223 dispatch(setTrashBreadcrumbs(''));
224 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
225 dispatch(loadCollectionFiles(collection.uuid));
232 export const createCollection = (data: collectionCreateActions.CollectionCreateFormDialogData) =>
233 async (dispatch: Dispatch) => {
234 const collection = await dispatch<any>(collectionCreateActions.createCollection(data));
236 dispatch(snackbarActions.OPEN_SNACKBAR({
237 message: "Collection has been successfully created.",
240 dispatch<any>(updateResources([collection]));
241 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
245 export const updateCollection = (data: collectionUpdateActions.CollectionUpdateFormDialogData) =>
246 async (dispatch: Dispatch) => {
247 const collection = await dispatch<any>(collectionUpdateActions.updateCollection(data));
249 dispatch(snackbarActions.OPEN_SNACKBAR({
250 message: "Collection has been successfully updated.",
253 dispatch<any>(updateResources([collection]));
254 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
258 export const copyCollection = (data: CopyFormDialogData) =>
259 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
261 const copyToProject = getResource(data.ownerUuid)(getState().resources);
262 const collection = await dispatch<any>(collectionCopyActions.copyCollection(data));
263 if (copyToProject && collection) {
264 dispatch<any>(reloadProjectMatchingUuid([copyToProject.uuid]));
265 dispatch(snackbarActions.OPEN_SNACKBAR({
266 message: 'Collection has been copied.',
268 kind: SnackbarKind.SUCCESS,
269 link: collection.ownerUuid
273 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000, kind: SnackbarKind.ERROR }));
277 export const moveCollection = (data: MoveToFormDialogData) =>
278 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
280 const collection = await dispatch<any>(collectionMoveActions.moveCollection(data));
281 dispatch<any>(updateResources([collection]));
282 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
283 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been moved.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
285 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
289 export const loadProcess = (uuid: string) =>
291 async (dispatch: Dispatch, getState: () => RootState) => {
292 dispatch<any>(loadProcessPanel(uuid));
293 const process = await dispatch<any>(processesActions.loadProcess(uuid));
294 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
295 dispatch<any>(setProcessBreadcrumbs(uuid));
296 dispatch(loadDetailsPanel(uuid));
299 export const updateProcess = (data: processUpdateActions.ProcessUpdateFormDialogData) =>
300 async (dispatch: Dispatch) => {
302 const process = await dispatch<any>(processUpdateActions.updateProcess(data));
304 dispatch(snackbarActions.OPEN_SNACKBAR({
305 message: "Process has been successfully updated.",
308 dispatch<any>(updateResources([process]));
309 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
312 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
316 export const moveProcess = (data: MoveToFormDialogData) =>
317 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
319 const process = await dispatch<any>(processMoveActions.moveProcess(data));
320 dispatch<any>(updateResources([process]));
321 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
322 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been moved.', hideDuration: 2000 }));
324 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
328 export const copyProcess = (data: CopyFormDialogData) =>
329 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
331 const process = await dispatch<any>(processCopyActions.copyProcess(data));
332 dispatch<any>(updateResources([process]));
333 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
334 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been copied.', hideDuration: 2000 }));
336 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
340 export const loadProcessLog = (uuid: string) =>
342 async (dispatch: Dispatch) => {
343 const process = await dispatch<any>(processesActions.loadProcess(uuid));
344 dispatch<any>(setProcessBreadcrumbs(uuid));
345 dispatch<any>(initProcessLogsPanel(uuid));
346 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
349 export const resourceIsNotLoaded = (uuid: string) =>
350 snackbarActions.OPEN_SNACKBAR({
351 message: `Resource identified by ${uuid} is not loaded.`
354 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
355 message: 'User is not authenticated'
358 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
359 message: 'Could not load user'
362 export const reloadProjectMatchingUuid = (matchingUuids: string[]) =>
363 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
364 const currentProjectPanelUuid = getProjectPanelCurrentUuid(getState());
365 if (currentProjectPanelUuid && matchingUuids.some(uuid => uuid === currentProjectPanelUuid)) {
366 dispatch<any>(loadProject(currentProjectPanelUuid));
370 export const loadSharedWithMe = handleFirstTimeLoad(async (dispatch: Dispatch) => {
371 dispatch<any>(loadSharedWithMePanel());
372 await dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
373 await dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.SHARED_WITH_ME));
376 export const loadRunProcess = handleFirstTimeLoad(
377 async (dispatch: Dispatch) => {
378 await dispatch<any>(loadRunProcessPanel());
382 export const loadWorkflow = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
383 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.WORKFLOWS));
384 await dispatch(loadWorkflowPanel());
385 dispatch(setSidePanelBreadcrumbs(SidePanelTreeCategory.WORKFLOWS));
388 export const loadSearchResults = handleFirstTimeLoad(
389 async (dispatch: Dispatch<any>) => {
390 await dispatch(loadSearchResultsPanel());
393 const finishLoadingProject = (project: GroupContentsResource | string) =>
394 async (dispatch: Dispatch<any>) => {
395 const uuid = typeof project === 'string' ? project : project.uuid;
396 dispatch(openProjectPanel(uuid));
397 dispatch(loadDetailsPanel(uuid));
398 if (typeof project !== 'string') {
399 dispatch(updateResources([project]));
403 const loadGroupContentsResource = async (params: {
406 services: ServiceRepository
408 const filters = new FilterBuilder()
409 .addEqual('uuid', params.uuid)
411 const { items } = await params.services.groupsService.contents(params.userUuid, {
416 const resource = items.shift();
417 let handler: GroupContentsHandler;
419 handler = (resource.kind === ResourceKind.COLLECTION || resource.kind === ResourceKind.PROJECT) && resource.isTrashed
420 ? groupContentsHandlers.TRASHED(resource)
421 : groupContentsHandlers.OWNED(resource);
423 const kind = extractUuidKind(params.uuid);
424 let resource: GroupContentsResource;
425 if (kind === ResourceKind.COLLECTION) {
426 resource = await params.services.collectionService.get(params.uuid);
427 } else if (kind === ResourceKind.PROJECT) {
428 resource = await params.services.projectService.get(params.uuid);
430 resource = await params.services.containerRequestService.get(params.uuid);
432 handler = groupContentsHandlers.SHARED(resource);
434 return (cases: MatchCases<typeof groupContentsHandlersRecord, GroupContentsHandler, void>) =>
435 groupContentsHandlers.match(handler, cases);
439 const groupContentsHandlersRecord = {
440 TRASHED: ofType<GroupContentsResource>(),
441 SHARED: ofType<GroupContentsResource>(),
442 OWNED: ofType<GroupContentsResource>(),
445 const groupContentsHandlers = unionize(groupContentsHandlersRecord);
447 type GroupContentsHandler = UnionOf<typeof groupContentsHandlers>;