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 { trashPanelColumns } from "~/views/trash-panel/trash-panel";
33 import { loadTrashPanel, trashPanelActions } from "~/store/trash-panel/trash-panel-action";
34 import { initProcessLogsPanel } from '../process-logs-panel/process-logs-panel-actions';
35 import { loadProcessPanel } from '~/store/process-panel/process-panel-actions';
38 export const loadWorkbench = () =>
39 async (dispatch: Dispatch, getState: () => RootState) => {
40 const { auth, router } = getState();
41 const { user } = auth;
43 const userResource = await dispatch<any>(loadResource(user.uuid));
45 dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
46 dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
47 dispatch(trashPanelActions.SET_COLUMNS({ columns: trashPanelColumns }));
48 dispatch<any>(initSidePanelTree());
49 if (router.location) {
50 const match = matchRootRoute(router.location.pathname);
52 dispatch(navigateToProject(userResource.uuid));
56 dispatch(userIsNotAuthenticated);
59 dispatch(userIsNotAuthenticated);
63 export const loadFavorites = () =>
64 (dispatch: Dispatch) => {
65 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
66 dispatch<any>(loadFavoritePanel());
67 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
70 export const loadTrash = () =>
71 (dispatch: Dispatch) => {
72 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
73 dispatch<any>(loadTrashPanel());
74 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.TRASH));
77 export const loadProject = (uuid: string) =>
78 async (dispatch: Dispatch) => {
79 await dispatch<any>(activateSidePanelTreeItem(uuid));
80 dispatch<any>(setProjectBreadcrumbs(uuid));
81 dispatch<any>(openProjectPanel(uuid));
82 dispatch(loadDetailsPanel(uuid));
85 export const createProject = (data: projectCreateActions.ProjectCreateFormDialogData) =>
86 async (dispatch: Dispatch) => {
87 const newProject = await dispatch<any>(projectCreateActions.createProject(data));
89 dispatch(snackbarActions.OPEN_SNACKBAR({
90 message: "Project has been successfully created.",
93 await dispatch<any>(loadSidePanelTreeProjects(newProject.ownerUuid));
94 dispatch<any>(reloadProjectMatchingUuid([newProject.ownerUuid]));
98 export const moveProject = (data: MoveToFormDialogData) =>
99 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
101 const oldProject = getResource(data.uuid)(getState().resources);
102 const oldOwnerUuid = oldProject ? oldProject.ownerUuid : '';
103 const movedProject = await dispatch<any>(projectMoveActions.moveProject(data));
105 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Project has been moved', hideDuration: 2000 }));
107 await dispatch<any>(loadSidePanelTreeProjects(oldProject.ownerUuid));
109 dispatch<any>(reloadProjectMatchingUuid([oldOwnerUuid, movedProject.ownerUuid, movedProject.uuid]));
112 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
116 export const updateProject = (data: projectUpdateActions.ProjectUpdateFormDialogData) =>
117 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
118 const updatedProject = await dispatch<any>(projectUpdateActions.updateProject(data));
119 if (updatedProject) {
120 dispatch(snackbarActions.OPEN_SNACKBAR({
121 message: "Project has been successfully updated.",
124 await dispatch<any>(loadSidePanelTreeProjects(updatedProject.ownerUuid));
125 dispatch<any>(reloadProjectMatchingUuid([updatedProject.ownerUuid, updatedProject.uuid]));
129 export const loadCollection = (uuid: string) =>
130 async (dispatch: Dispatch) => {
131 const collection = await dispatch<any>(loadCollectionPanel(uuid));
132 await dispatch<any>(activateSidePanelTreeItem(collection.ownerUuid));
133 dispatch<any>(setCollectionBreadcrumbs(collection.uuid));
134 dispatch(loadDetailsPanel(uuid));
137 export const createCollection = (data: collectionCreateActions.CollectionCreateFormDialogData) =>
138 async (dispatch: Dispatch) => {
139 const collection = await dispatch<any>(collectionCreateActions.createCollection(data));
141 dispatch(snackbarActions.OPEN_SNACKBAR({
142 message: "Collection has been successfully created.",
145 dispatch<any>(updateResources([collection]));
146 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
150 export const updateCollection = (data: collectionUpdateActions.CollectionUpdateFormDialogData) =>
151 async (dispatch: Dispatch) => {
152 const collection = await dispatch<any>(collectionUpdateActions.updateCollection(data));
154 dispatch(snackbarActions.OPEN_SNACKBAR({
155 message: "Collection has been successfully updated.",
158 dispatch<any>(updateResources([collection]));
159 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
163 export const copyCollection = (data: collectionCopyActions.CollectionCopyFormDialogData) =>
164 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
166 const collection = await dispatch<any>(collectionCopyActions.copyCollection(data));
167 dispatch<any>(updateResources([collection]));
168 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
169 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied.', hideDuration: 2000 }));
171 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
175 export const moveCollection = (data: MoveToFormDialogData) =>
176 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
178 const collection = await dispatch<any>(collectionMoveActions.moveCollection(data));
179 dispatch<any>(updateResources([collection]));
180 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
181 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been moved.', hideDuration: 2000 }));
183 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
187 export const loadProcess = (uuid: string) =>
188 async (dispatch: Dispatch, getState: () => RootState) => {
189 dispatch<any>(loadProcessPanel(uuid));
190 const process = await dispatch<any>(processesActions.loadProcess(uuid));
191 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
192 dispatch<any>(setProcessBreadcrumbs(uuid));
193 dispatch(loadDetailsPanel(uuid));
197 export const loadProcessLog = (uuid: string) =>
198 async (dispatch: Dispatch) => {
199 const process = await dispatch<any>(processesActions.loadProcess(uuid));
200 dispatch<any>(setProcessBreadcrumbs(uuid));
201 dispatch<any>(initProcessLogsPanel(uuid));
202 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
205 export const resourceIsNotLoaded = (uuid: string) =>
206 snackbarActions.OPEN_SNACKBAR({
207 message: `Resource identified by ${uuid} is not loaded.`
210 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
211 message: 'User is not authenticated'
214 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
215 message: 'Could not load user'
218 export const reloadProjectMatchingUuid = (matchingUuids: string[]) =>
219 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
220 const currentProjectPanelUuid = getProjectPanelCurrentUuid(getState());
221 if (currentProjectPanelUuid && matchingUuids.some(uuid => uuid === currentProjectPanelUuid)) {
222 dispatch<any>(loadProject(currentProjectPanelUuid));