1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch, AnyAction } 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, getSidePanelTreeNodeAncestorsIds } 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, setSharedWithMeBreadcrumbs } 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 processUpdateActions from '~/store/processes/process-update-actions';
34 import * as processCopyActions from '~/store/processes/process-copy-actions';
35 import { trashPanelColumns } from "~/views/trash-panel/trash-panel";
36 import { loadTrashPanel, trashPanelActions } from "~/store/trash-panel/trash-panel-action";
37 import { initProcessLogsPanel } from '../process-logs-panel/process-logs-panel-actions';
38 import { loadProcessPanel } from '~/store/process-panel/process-panel-actions';
39 import { sharedWithMePanelActions } from '~/store/shared-with-me-panel/shared-with-me-panel-actions';
40 import { loadSharedWithMePanel } from '../shared-with-me-panel/shared-with-me-panel-actions';
41 import { CopyFormDialogData } from '~/store/copy-dialog/copy-dialog';
42 import { progressIndicatorActions } from '~/store/progress-indicator/progress-indicator-actions';
43 import { getProgressIndicator } from '../progress-indicator/progress-indicator-reducer';
45 export const WORKBENCH_LOADING_SCREEN = 'workbenchLoadingScreen';
47 export const isWorkbenchLoading = (state: RootState) => {
48 const progress = getProgressIndicator(WORKBENCH_LOADING_SCREEN)(state.progressIndicator);
49 return progress ? progress.working : false;
52 const handleFirstTimeLoad = (action: any) =>
53 async (dispatch: Dispatch<any>, getState: () => RootState) => {
55 await dispatch(action);
57 if (isWorkbenchLoading(getState())) {
58 dispatch(progressIndicatorActions.STOP_WORKING(WORKBENCH_LOADING_SCREEN));
64 export const loadWorkbench = () =>
65 async (dispatch: Dispatch, getState: () => RootState) => {
66 dispatch(progressIndicatorActions.START_WORKING(WORKBENCH_LOADING_SCREEN));
67 const { auth, router } = getState();
68 const { user } = auth;
70 const userResource = await dispatch<any>(loadResource(user.uuid));
72 dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
73 dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
74 dispatch(trashPanelActions.SET_COLUMNS({ columns: trashPanelColumns }));
75 dispatch(sharedWithMePanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
76 dispatch<any>(initSidePanelTree());
77 if (router.location) {
78 const match = matchRootRoute(router.location.pathname);
80 dispatch(navigateToProject(userResource.uuid));
84 dispatch(userIsNotAuthenticated);
87 dispatch(userIsNotAuthenticated);
91 export const loadFavorites = () =>
93 (dispatch: Dispatch) => {
94 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
95 dispatch<any>(loadFavoritePanel());
96 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
99 export const loadTrash = () =>
101 (dispatch: Dispatch) => {
102 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
103 dispatch<any>(loadTrashPanel());
104 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.TRASH));
107 export const loadProject = (uuid: string) =>
109 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
110 dispatch(openProjectPanel(uuid));
111 await dispatch(activateSidePanelTreeItem(uuid));
112 dispatch(setProjectBreadcrumbs(uuid));
113 dispatch(loadDetailsPanel(uuid));
116 export const createProject = (data: projectCreateActions.ProjectCreateFormDialogData) =>
117 async (dispatch: Dispatch) => {
118 const newProject = await dispatch<any>(projectCreateActions.createProject(data));
120 dispatch(snackbarActions.OPEN_SNACKBAR({
121 message: "Project has been successfully created.",
124 await dispatch<any>(loadSidePanelTreeProjects(newProject.ownerUuid));
125 dispatch<any>(reloadProjectMatchingUuid([newProject.ownerUuid]));
129 export const moveProject = (data: MoveToFormDialogData) =>
130 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
132 const oldProject = getResource(data.uuid)(getState().resources);
133 const oldOwnerUuid = oldProject ? oldProject.ownerUuid : '';
134 const movedProject = await dispatch<any>(projectMoveActions.moveProject(data));
136 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Project has been moved', hideDuration: 2000 }));
138 await dispatch<any>(loadSidePanelTreeProjects(oldProject.ownerUuid));
140 dispatch<any>(reloadProjectMatchingUuid([oldOwnerUuid, movedProject.ownerUuid, movedProject.uuid]));
143 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
147 export const updateProject = (data: projectUpdateActions.ProjectUpdateFormDialogData) =>
148 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
149 const updatedProject = await dispatch<any>(projectUpdateActions.updateProject(data));
150 if (updatedProject) {
151 dispatch(snackbarActions.OPEN_SNACKBAR({
152 message: "Project has been successfully updated.",
155 await dispatch<any>(loadSidePanelTreeProjects(updatedProject.ownerUuid));
156 dispatch<any>(reloadProjectMatchingUuid([updatedProject.ownerUuid, updatedProject.uuid]));
160 export const loadCollection = (uuid: string) =>
162 async (dispatch: Dispatch) => {
163 const collection = await dispatch<any>(loadCollectionPanel(uuid));
164 await dispatch<any>(activateSidePanelTreeItem(collection.ownerUuid));
165 dispatch<any>(setCollectionBreadcrumbs(collection.uuid));
166 dispatch(loadDetailsPanel(uuid));
169 export const createCollection = (data: collectionCreateActions.CollectionCreateFormDialogData) =>
170 async (dispatch: Dispatch) => {
171 const collection = await dispatch<any>(collectionCreateActions.createCollection(data));
173 dispatch(snackbarActions.OPEN_SNACKBAR({
174 message: "Collection has been successfully created.",
177 dispatch<any>(updateResources([collection]));
178 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
182 export const updateCollection = (data: collectionUpdateActions.CollectionUpdateFormDialogData) =>
183 async (dispatch: Dispatch) => {
184 const collection = await dispatch<any>(collectionUpdateActions.updateCollection(data));
186 dispatch(snackbarActions.OPEN_SNACKBAR({
187 message: "Collection has been successfully updated.",
190 dispatch<any>(updateResources([collection]));
191 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
195 export const copyCollection = (data: CopyFormDialogData) =>
196 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
198 const collection = await dispatch<any>(collectionCopyActions.copyCollection(data));
199 dispatch<any>(updateResources([collection]));
200 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
201 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied.', hideDuration: 2000 }));
203 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
207 export const moveCollection = (data: MoveToFormDialogData) =>
208 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
210 const collection = await dispatch<any>(collectionMoveActions.moveCollection(data));
211 dispatch<any>(updateResources([collection]));
212 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
213 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been moved.', hideDuration: 2000 }));
215 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
219 export const loadProcess = (uuid: string) =>
221 async (dispatch: Dispatch, getState: () => RootState) => {
222 dispatch<any>(loadProcessPanel(uuid));
223 const process = await dispatch<any>(processesActions.loadProcess(uuid));
224 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
225 dispatch<any>(setProcessBreadcrumbs(uuid));
226 dispatch(loadDetailsPanel(uuid));
229 export const updateProcess = (data: processUpdateActions.ProcessUpdateFormDialogData) =>
230 async (dispatch: Dispatch) => {
232 const process = await dispatch<any>(processUpdateActions.updateProcess(data));
234 dispatch(snackbarActions.OPEN_SNACKBAR({
235 message: "Process has been successfully updated.",
238 dispatch<any>(updateResources([process]));
239 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
242 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
246 export const moveProcess = (data: MoveToFormDialogData) =>
247 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
249 const process = await dispatch<any>(processMoveActions.moveProcess(data));
250 dispatch<any>(updateResources([process]));
251 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
252 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been moved.', hideDuration: 2000 }));
254 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
258 export const copyProcess = (data: CopyFormDialogData) =>
259 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
261 const process = await dispatch<any>(processCopyActions.copyProcess(data));
262 dispatch<any>(updateResources([process]));
263 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
264 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been copied.', hideDuration: 2000 }));
266 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
270 export const loadProcessLog = (uuid: string) =>
272 async (dispatch: Dispatch) => {
273 const process = await dispatch<any>(processesActions.loadProcess(uuid));
274 dispatch<any>(setProcessBreadcrumbs(uuid));
275 dispatch<any>(initProcessLogsPanel(uuid));
276 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
279 export const resourceIsNotLoaded = (uuid: string) =>
280 snackbarActions.OPEN_SNACKBAR({
281 message: `Resource identified by ${uuid} is not loaded.`
284 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
285 message: 'User is not authenticated'
288 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
289 message: 'Could not load user'
292 export const reloadProjectMatchingUuid = (matchingUuids: string[]) =>
293 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
294 const currentProjectPanelUuid = getProjectPanelCurrentUuid(getState());
295 if (currentProjectPanelUuid && matchingUuids.some(uuid => uuid === currentProjectPanelUuid)) {
296 dispatch<any>(loadProject(currentProjectPanelUuid));
300 export const loadSharedWithMe = handleFirstTimeLoad(async (dispatch: Dispatch) => {
301 dispatch<any>(loadSharedWithMePanel());
302 await dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
303 await dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.SHARED_WITH_ME));