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 { 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 } 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 * as projectCreateActions from '~/store/projects/project-create-actions';
21 import * as projectMoveActions from '~/store/projects/project-move-actions';
22 import * as projectUpdateActions from '~/store/projects/project-update-actions';
23 import { MoveToFormDialogData } from '~/store/move-to-dialog/move-to-dialog';
24 import { ServiceRepository } from '~/services/services';
25 import { getResource } from '../resources/resources';
26 import { getProjectPanelCurrentUuid } from '../project-panel/project-panel-action';
29 export const loadWorkbench = () =>
30 async (dispatch: Dispatch, getState: () => RootState) => {
31 const { auth, router } = getState();
32 const { user } = auth;
34 const userResource = await dispatch<any>(loadResource(user.uuid));
36 dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
37 dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
38 dispatch<any>(initSidePanelTree());
39 if (router.location) {
40 const match = matchRootRoute(router.location.pathname);
42 dispatch(navigateToProject(userResource.uuid));
46 dispatch(userIsNotAuthenticated);
49 dispatch(userIsNotAuthenticated);
53 export const loadFavorites = () =>
54 (dispatch: Dispatch) => {
55 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
56 dispatch<any>(loadFavoritePanel());
57 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
61 export const loadProject = (uuid: string) =>
62 async (dispatch: Dispatch) => {
63 await dispatch<any>(activateSidePanelTreeItem(uuid));
64 dispatch<any>(setProjectBreadcrumbs(uuid));
65 dispatch<any>(openProjectPanel(uuid));
66 dispatch(loadDetailsPanel(uuid));
69 export const createProject = (data: projectCreateActions.ProjectCreateFormDialogData) =>
70 async (dispatch: Dispatch) => {
71 const newProject = await dispatch<any>(projectCreateActions.createProject(data));
73 dispatch(snackbarActions.OPEN_SNACKBAR({
74 message: "Project has been successfully created.",
77 await dispatch<any>(loadSidePanelTreeProjects(newProject.ownerUuid));
78 dispatch<any>(reloadProjectMatchingUuid([newProject.ownerUuid]));
82 export const moveProject = (data: MoveToFormDialogData) =>
83 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
85 const oldProject = getResource(data.uuid)(getState().resources);
86 const oldOwnerUuid = oldProject ? oldProject.ownerUuid : '';
87 const movedProject = await dispatch<any>(projectMoveActions.moveProject(data));
89 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Project has been moved', hideDuration: 2000 }));
91 await dispatch<any>(loadSidePanelTreeProjects(oldProject.ownerUuid));
93 dispatch<any>(reloadProjectMatchingUuid([oldOwnerUuid, movedProject.ownerUuid, movedProject.uuid]));
96 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
100 export const updateProject = (data: projectUpdateActions.ProjectUpdateFormDialogData) =>
101 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
102 const updatedProject = await dispatch<any>(projectUpdateActions.updateProject(data));
103 if (updatedProject) {
104 dispatch(snackbarActions.OPEN_SNACKBAR({
105 message: "Project has been successfully updated.",
108 await dispatch<any>(loadSidePanelTreeProjects(updatedProject.ownerUuid));
109 dispatch<any>(reloadProjectMatchingUuid([updatedProject.ownerUuid, updatedProject.uuid]));
113 export const loadCollection = (uuid: string) =>
114 async (dispatch: Dispatch) => {
115 const collection = await dispatch<any>(loadCollectionPanel(uuid));
116 await dispatch<any>(activateSidePanelTreeItem(collection.ownerUuid));
117 dispatch<any>(setCollectionBreadcrumbs(collection.uuid));
118 dispatch(loadDetailsPanel(uuid));
121 export const resourceIsNotLoaded = (uuid: string) =>
122 snackbarActions.OPEN_SNACKBAR({
123 message: `Resource identified by ${uuid} is not loaded.`
126 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
127 message: 'User is not authenticated'
130 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
131 message: 'Could not load user'
134 const reloadProjectMatchingUuid = (matchingUuids: string[]) =>
135 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
136 const currentProjectPanelUuid = getProjectPanelCurrentUuid(getState());
137 if (currentProjectPanelUuid && matchingUuids.some(uuid => uuid === currentProjectPanelUuid)) {
138 dispatch<any>(loadProject(currentProjectPanelUuid));