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';
33 import { initProcessLogsPanel } from '../process-logs-panel/process-logs-panel-actions';
36 export const loadWorkbench = () =>
37 async (dispatch: Dispatch, getState: () => RootState) => {
38 const { auth, router } = getState();
39 const { user } = auth;
41 const userResource = await dispatch<any>(loadResource(user.uuid));
43 dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
44 dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
45 dispatch<any>(initSidePanelTree());
46 if (router.location) {
47 const match = matchRootRoute(router.location.pathname);
49 dispatch(navigateToProject(userResource.uuid));
53 dispatch(userIsNotAuthenticated);
56 dispatch(userIsNotAuthenticated);
60 export const loadFavorites = () =>
61 (dispatch: Dispatch) => {
62 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
63 dispatch<any>(loadFavoritePanel());
64 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
68 export const loadProject = (uuid: string) =>
69 async (dispatch: Dispatch) => {
70 await dispatch<any>(activateSidePanelTreeItem(uuid));
71 dispatch<any>(setProjectBreadcrumbs(uuid));
72 dispatch<any>(openProjectPanel(uuid));
73 dispatch(loadDetailsPanel(uuid));
76 export const createProject = (data: projectCreateActions.ProjectCreateFormDialogData) =>
77 async (dispatch: Dispatch) => {
78 const newProject = await dispatch<any>(projectCreateActions.createProject(data));
80 dispatch(snackbarActions.OPEN_SNACKBAR({
81 message: "Project has been successfully created.",
84 await dispatch<any>(loadSidePanelTreeProjects(newProject.ownerUuid));
85 dispatch<any>(reloadProjectMatchingUuid([newProject.ownerUuid]));
89 export const moveProject = (data: MoveToFormDialogData) =>
90 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
92 const oldProject = getResource(data.uuid)(getState().resources);
93 const oldOwnerUuid = oldProject ? oldProject.ownerUuid : '';
94 const movedProject = await dispatch<any>(projectMoveActions.moveProject(data));
96 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Project has been moved', hideDuration: 2000 }));
98 await dispatch<any>(loadSidePanelTreeProjects(oldProject.ownerUuid));
100 dispatch<any>(reloadProjectMatchingUuid([oldOwnerUuid, movedProject.ownerUuid, movedProject.uuid]));
103 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
107 export const updateProject = (data: projectUpdateActions.ProjectUpdateFormDialogData) =>
108 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
109 const updatedProject = await dispatch<any>(projectUpdateActions.updateProject(data));
110 if (updatedProject) {
111 dispatch(snackbarActions.OPEN_SNACKBAR({
112 message: "Project has been successfully updated.",
115 await dispatch<any>(loadSidePanelTreeProjects(updatedProject.ownerUuid));
116 dispatch<any>(reloadProjectMatchingUuid([updatedProject.ownerUuid, updatedProject.uuid]));
120 export const loadCollection = (uuid: string) =>
121 async (dispatch: Dispatch) => {
122 const collection = await dispatch<any>(loadCollectionPanel(uuid));
123 await dispatch<any>(activateSidePanelTreeItem(collection.ownerUuid));
124 dispatch<any>(setCollectionBreadcrumbs(collection.uuid));
125 dispatch(loadDetailsPanel(uuid));
128 export const createCollection = (data: collectionCreateActions.CollectionCreateFormDialogData) =>
129 async (dispatch: Dispatch) => {
130 const collection = await dispatch<any>(collectionCreateActions.createCollection(data));
132 dispatch(snackbarActions.OPEN_SNACKBAR({
133 message: "Collection has been successfully created.",
136 dispatch<any>(updateResources([collection]));
137 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
141 export const updateCollection = (data: collectionUpdateActions.CollectionUpdateFormDialogData) =>
142 async (dispatch: Dispatch) => {
143 const collection = await dispatch<any>(collectionUpdateActions.updateCollection(data));
145 dispatch(snackbarActions.OPEN_SNACKBAR({
146 message: "Collection has been successfully updated.",
149 dispatch<any>(updateResources([collection]));
150 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
154 export const copyCollection = (data: collectionCopyActions.CollectionCopyFormDialogData) =>
155 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
157 const collection = await dispatch<any>(collectionCopyActions.copyCollection(data));
158 dispatch<any>(updateResources([collection]));
159 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
160 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied.', hideDuration: 2000 }));
162 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
166 export const moveCollection = (data: MoveToFormDialogData) =>
167 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
169 const collection = await dispatch<any>(collectionMoveActions.moveCollection(data));
170 dispatch<any>(updateResources([collection]));
171 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
172 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been moved.', hideDuration: 2000 }));
174 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
178 export const loadProcess = (uuid: string) =>
179 async (dispatch: Dispatch, getState: () => RootState) => {
180 await dispatch<any>(processesActions.loadProcess(uuid));
181 const process = getProcess(uuid)(getState().resources);
183 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
184 dispatch<any>(setCollectionBreadcrumbs(process.containerRequest.ownerUuid));
185 dispatch(loadDetailsPanel(uuid));
189 export const loadProcessLog = (uuid: string) =>
190 async (dispatch: Dispatch) => {
191 dispatch<any>(initProcessLogsPanel(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));