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, 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 import * as processesActions from '../processes/processes-actions';
32 import { getProcess } from '../processes/process';
35 export const loadWorkbench = () =>
36 async (dispatch: Dispatch, getState: () => RootState) => {
37 const { auth, router } = getState();
38 const { user } = auth;
40 const userResource = await dispatch<any>(loadResource(user.uuid));
42 dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
43 dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
44 dispatch<any>(initSidePanelTree());
45 if (router.location) {
46 const match = matchRootRoute(router.location.pathname);
48 dispatch(navigateToProject(userResource.uuid));
52 dispatch(userIsNotAuthenticated);
55 dispatch(userIsNotAuthenticated);
59 export const loadFavorites = () =>
60 (dispatch: Dispatch) => {
61 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
62 dispatch<any>(loadFavoritePanel());
63 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
67 export const loadProject = (uuid: string) =>
68 async (dispatch: Dispatch) => {
69 await dispatch<any>(activateSidePanelTreeItem(uuid));
70 dispatch<any>(setProjectBreadcrumbs(uuid));
71 dispatch<any>(openProjectPanel(uuid));
72 dispatch(loadDetailsPanel(uuid));
75 export const createProject = (data: projectCreateActions.ProjectCreateFormDialogData) =>
76 async (dispatch: Dispatch) => {
77 const newProject = await dispatch<any>(projectCreateActions.createProject(data));
79 dispatch(snackbarActions.OPEN_SNACKBAR({
80 message: "Project has been successfully created.",
83 await dispatch<any>(loadSidePanelTreeProjects(newProject.ownerUuid));
84 dispatch<any>(reloadProjectMatchingUuid([newProject.ownerUuid]));
88 export const moveProject = (data: MoveToFormDialogData) =>
89 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
91 const oldProject = getResource(data.uuid)(getState().resources);
92 const oldOwnerUuid = oldProject ? oldProject.ownerUuid : '';
93 const movedProject = await dispatch<any>(projectMoveActions.moveProject(data));
95 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Project has been moved', hideDuration: 2000 }));
97 await dispatch<any>(loadSidePanelTreeProjects(oldProject.ownerUuid));
99 dispatch<any>(reloadProjectMatchingUuid([oldOwnerUuid, movedProject.ownerUuid, movedProject.uuid]));
102 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
106 export const updateProject = (data: projectUpdateActions.ProjectUpdateFormDialogData) =>
107 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
108 const updatedProject = await dispatch<any>(projectUpdateActions.updateProject(data));
109 if (updatedProject) {
110 dispatch(snackbarActions.OPEN_SNACKBAR({
111 message: "Project has been successfully updated.",
114 await dispatch<any>(loadSidePanelTreeProjects(updatedProject.ownerUuid));
115 dispatch<any>(reloadProjectMatchingUuid([updatedProject.ownerUuid, updatedProject.uuid]));
119 export const loadCollection = (uuid: string) =>
120 async (dispatch: Dispatch) => {
121 const collection = await dispatch<any>(loadCollectionPanel(uuid));
122 await dispatch<any>(activateSidePanelTreeItem(collection.ownerUuid));
123 dispatch<any>(setCollectionBreadcrumbs(collection.uuid));
124 dispatch(loadDetailsPanel(uuid));
127 export const createCollection = (data: collectionCreateActions.CollectionCreateFormDialogData) =>
128 async (dispatch: Dispatch) => {
129 const collection = await dispatch<any>(collectionCreateActions.createCollection(data));
131 dispatch(snackbarActions.OPEN_SNACKBAR({
132 message: "Collection has been successfully created.",
135 dispatch<any>(updateResources([collection]));
136 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
140 export const updateCollection = (data: collectionUpdateActions.CollectionUpdateFormDialogData) =>
141 async (dispatch: Dispatch) => {
142 const collection = await dispatch<any>(collectionUpdateActions.updateCollection(data));
144 dispatch(snackbarActions.OPEN_SNACKBAR({
145 message: "Collection has been successfully updated.",
148 dispatch<any>(updateResources([collection]));
149 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
153 export const copyCollection = (data: collectionCopyActions.CollectionCopyFormDialogData) =>
154 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
156 const collection = await dispatch<any>(collectionCopyActions.copyCollection(data));
157 dispatch<any>(updateResources([collection]));
158 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
159 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied.', hideDuration: 2000 }));
161 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
165 export const moveCollection = (data: MoveToFormDialogData) =>
166 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
168 const collection = await dispatch<any>(collectionMoveActions.moveCollection(data));
169 dispatch<any>(updateResources([collection]));
170 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
171 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been moved.', hideDuration: 2000 }));
173 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
177 export const loadProcess = (uuid: string) =>
178 async (dispatch: Dispatch, getState: () => RootState) => {
179 await dispatch<any>(processesActions.loadProcess(uuid));
180 const process = getProcess(uuid)(getState().resources);
182 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
183 dispatch<any>(setCollectionBreadcrumbs(process.containerRequest.ownerUuid));
184 dispatch(loadDetailsPanel(uuid));
188 export const loadProcessLog = (uuid: string) =>
189 async (dispatch: Dispatch, getState: () => RootState) => {
190 dispatch<any>(loadProcess(uuid));
194 export const resourceIsNotLoaded = (uuid: string) =>
195 snackbarActions.OPEN_SNACKBAR({
196 message: `Resource identified by ${uuid} is not loaded.`
199 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
200 message: 'User is not authenticated'
203 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
204 message: 'Could not load user'
207 const reloadProjectMatchingUuid = (matchingUuids: string[]) =>
208 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
209 const currentProjectPanelUuid = getProjectPanelCurrentUuid(getState());
210 if (currentProjectPanelUuid && matchingUuids.some(uuid => uuid === currentProjectPanelUuid)) {
211 dispatch<any>(loadProject(currentProjectPanelUuid));