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, setProcessBreadcrumbs } 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 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 { CopyFormDialogData } from '~/store/copy-dialog/copy-dialog';
41 export const loadWorkbench = () =>
42 async (dispatch: Dispatch, getState: () => RootState) => {
43 const { auth, router } = getState();
44 const { user } = auth;
46 const userResource = await dispatch<any>(loadResource(user.uuid));
48 dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
49 dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
50 dispatch(trashPanelActions.SET_COLUMNS({ columns: trashPanelColumns }));
51 dispatch<any>(initSidePanelTree());
52 if (router.location) {
53 const match = matchRootRoute(router.location.pathname);
55 dispatch(navigateToProject(userResource.uuid));
59 dispatch(userIsNotAuthenticated);
62 dispatch(userIsNotAuthenticated);
66 export const loadFavorites = () =>
67 (dispatch: Dispatch) => {
68 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
69 dispatch<any>(loadFavoritePanel());
70 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
73 export const loadTrash = () =>
74 (dispatch: Dispatch) => {
75 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
76 dispatch<any>(loadTrashPanel());
77 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.TRASH));
80 export const loadProject = (uuid: string) =>
81 async (dispatch: Dispatch) => {
82 await dispatch<any>(activateSidePanelTreeItem(uuid));
83 dispatch<any>(setProjectBreadcrumbs(uuid));
84 dispatch<any>(openProjectPanel(uuid));
85 dispatch(loadDetailsPanel(uuid));
88 export const createProject = (data: projectCreateActions.ProjectCreateFormDialogData) =>
89 async (dispatch: Dispatch) => {
90 const newProject = await dispatch<any>(projectCreateActions.createProject(data));
92 dispatch(snackbarActions.OPEN_SNACKBAR({
93 message: "Project has been successfully created.",
96 await dispatch<any>(loadSidePanelTreeProjects(newProject.ownerUuid));
97 dispatch<any>(reloadProjectMatchingUuid([newProject.ownerUuid]));
101 export const moveProject = (data: MoveToFormDialogData) =>
102 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
104 const oldProject = getResource(data.uuid)(getState().resources);
105 const oldOwnerUuid = oldProject ? oldProject.ownerUuid : '';
106 const movedProject = await dispatch<any>(projectMoveActions.moveProject(data));
108 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Project has been moved', hideDuration: 2000 }));
110 await dispatch<any>(loadSidePanelTreeProjects(oldProject.ownerUuid));
112 dispatch<any>(reloadProjectMatchingUuid([oldOwnerUuid, movedProject.ownerUuid, movedProject.uuid]));
115 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
119 export const updateProject = (data: projectUpdateActions.ProjectUpdateFormDialogData) =>
120 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
121 const updatedProject = await dispatch<any>(projectUpdateActions.updateProject(data));
122 if (updatedProject) {
123 dispatch(snackbarActions.OPEN_SNACKBAR({
124 message: "Project has been successfully updated.",
127 await dispatch<any>(loadSidePanelTreeProjects(updatedProject.ownerUuid));
128 dispatch<any>(reloadProjectMatchingUuid([updatedProject.ownerUuid, updatedProject.uuid]));
132 export const loadCollection = (uuid: string) =>
133 async (dispatch: Dispatch) => {
134 const collection = await dispatch<any>(loadCollectionPanel(uuid));
135 await dispatch<any>(activateSidePanelTreeItem(collection.ownerUuid));
136 dispatch<any>(setCollectionBreadcrumbs(collection.uuid));
137 dispatch(loadDetailsPanel(uuid));
140 export const createCollection = (data: collectionCreateActions.CollectionCreateFormDialogData) =>
141 async (dispatch: Dispatch) => {
142 const collection = await dispatch<any>(collectionCreateActions.createCollection(data));
144 dispatch(snackbarActions.OPEN_SNACKBAR({
145 message: "Collection has been successfully created.",
148 dispatch<any>(updateResources([collection]));
149 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
153 export const updateCollection = (data: collectionUpdateActions.CollectionUpdateFormDialogData) =>
154 async (dispatch: Dispatch) => {
155 const collection = await dispatch<any>(collectionUpdateActions.updateCollection(data));
157 dispatch(snackbarActions.OPEN_SNACKBAR({
158 message: "Collection has been successfully updated.",
161 dispatch<any>(updateResources([collection]));
162 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
166 export const copyCollection = (data: CopyFormDialogData) =>
167 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
169 const collection = await dispatch<any>(collectionCopyActions.copyCollection(data));
170 dispatch<any>(updateResources([collection]));
171 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
172 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied.', hideDuration: 2000 }));
174 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
178 export const moveCollection = (data: MoveToFormDialogData) =>
179 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
181 const collection = await dispatch<any>(collectionMoveActions.moveCollection(data));
182 dispatch<any>(updateResources([collection]));
183 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
184 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been moved.', hideDuration: 2000 }));
186 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
190 export const loadProcess = (uuid: string) =>
191 async (dispatch: Dispatch, getState: () => RootState) => {
192 dispatch<any>(loadProcessPanel(uuid));
193 const process = await dispatch<any>(processesActions.loadProcess(uuid));
194 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
195 dispatch<any>(setProcessBreadcrumbs(uuid));
196 dispatch(loadDetailsPanel(uuid));
200 export const moveProcess = (data: MoveToFormDialogData) =>
201 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
203 const process = await dispatch<any>(processMoveActions.moveProcess(data));
204 dispatch<any>(updateResources([process]));
205 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
206 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been moved.', hideDuration: 2000 }));
208 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
212 export const copyProcess = (data: CopyFormDialogData) =>
213 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
215 const process = await dispatch<any>(processCopyActions.copyProcess(data));
216 dispatch<any>(updateResources([process]));
217 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
218 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been copied.', hideDuration: 2000 }));
220 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
224 export const loadProcessLog = (uuid: string) =>
225 async (dispatch: Dispatch) => {
226 const process = await dispatch<any>(processesActions.loadProcess(uuid));
227 dispatch<any>(setProcessBreadcrumbs(uuid));
228 dispatch<any>(initProcessLogsPanel(uuid));
229 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
232 export const resourceIsNotLoaded = (uuid: string) =>
233 snackbarActions.OPEN_SNACKBAR({
234 message: `Resource identified by ${uuid} is not loaded.`
237 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
238 message: 'User is not authenticated'
241 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
242 message: 'Could not load user'
245 export const reloadProjectMatchingUuid = (matchingUuids: string[]) =>
246 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
247 const currentProjectPanelUuid = getProjectPanelCurrentUuid(getState());
248 if (currentProjectPanelUuid && matchingUuids.some(uuid => uuid === currentProjectPanelUuid)) {
249 dispatch<any>(loadProject(currentProjectPanelUuid));