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 { trashPanelColumns } from "~/views/trash-panel/trash-panel";
34 import { loadTrashPanel, trashPanelActions } from "~/store/trash-panel/trash-panel-action";
37 export const loadWorkbench = () =>
38 async (dispatch: Dispatch, getState: () => RootState) => {
39 const { auth, router } = getState();
40 const { user } = auth;
42 const userResource = await dispatch<any>(loadResource(user.uuid));
44 dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
45 dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
46 dispatch(trashPanelActions.SET_COLUMNS({ columns: trashPanelColumns }));
47 dispatch<any>(initSidePanelTree());
48 if (router.location) {
49 const match = matchRootRoute(router.location.pathname);
51 dispatch(navigateToProject(userResource.uuid));
55 dispatch(userIsNotAuthenticated);
58 dispatch(userIsNotAuthenticated);
62 export const loadFavorites = () =>
63 (dispatch: Dispatch) => {
64 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
65 dispatch<any>(loadFavoritePanel());
66 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
69 export const loadTrash = () =>
70 (dispatch: Dispatch) => {
71 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
72 dispatch<any>(loadTrashPanel());
73 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.TRASH));
76 export const loadProject = (uuid: string) =>
77 async (dispatch: Dispatch) => {
78 await dispatch<any>(activateSidePanelTreeItem(uuid));
79 dispatch<any>(setProjectBreadcrumbs(uuid));
80 dispatch<any>(openProjectPanel(uuid));
81 dispatch(loadDetailsPanel(uuid));
84 export const createProject = (data: projectCreateActions.ProjectCreateFormDialogData) =>
85 async (dispatch: Dispatch) => {
86 const newProject = await dispatch<any>(projectCreateActions.createProject(data));
88 dispatch(snackbarActions.OPEN_SNACKBAR({
89 message: "Project has been successfully created.",
92 await dispatch<any>(loadSidePanelTreeProjects(newProject.ownerUuid));
93 dispatch<any>(reloadProjectMatchingUuid([newProject.ownerUuid]));
97 export const moveProject = (data: MoveToFormDialogData) =>
98 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
100 const oldProject = getResource(data.uuid)(getState().resources);
101 const oldOwnerUuid = oldProject ? oldProject.ownerUuid : '';
102 const movedProject = await dispatch<any>(projectMoveActions.moveProject(data));
104 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Project has been moved', hideDuration: 2000 }));
106 await dispatch<any>(loadSidePanelTreeProjects(oldProject.ownerUuid));
108 dispatch<any>(reloadProjectMatchingUuid([oldOwnerUuid, movedProject.ownerUuid, movedProject.uuid]));
111 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
115 export const updateProject = (data: projectUpdateActions.ProjectUpdateFormDialogData) =>
116 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
117 const updatedProject = await dispatch<any>(projectUpdateActions.updateProject(data));
118 if (updatedProject) {
119 dispatch(snackbarActions.OPEN_SNACKBAR({
120 message: "Project has been successfully updated.",
123 await dispatch<any>(loadSidePanelTreeProjects(updatedProject.ownerUuid));
124 dispatch<any>(reloadProjectMatchingUuid([updatedProject.ownerUuid, updatedProject.uuid]));
128 export const loadCollection = (uuid: string) =>
129 async (dispatch: Dispatch) => {
130 const collection = await dispatch<any>(loadCollectionPanel(uuid));
131 await dispatch<any>(activateSidePanelTreeItem(collection.ownerUuid));
132 dispatch<any>(setCollectionBreadcrumbs(collection.uuid));
133 dispatch(loadDetailsPanel(uuid));
136 export const createCollection = (data: collectionCreateActions.CollectionCreateFormDialogData) =>
137 async (dispatch: Dispatch) => {
138 const collection = await dispatch<any>(collectionCreateActions.createCollection(data));
140 dispatch(snackbarActions.OPEN_SNACKBAR({
141 message: "Collection has been successfully created.",
144 dispatch<any>(updateResources([collection]));
145 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
149 export const updateCollection = (data: collectionUpdateActions.CollectionUpdateFormDialogData) =>
150 async (dispatch: Dispatch) => {
151 const collection = await dispatch<any>(collectionUpdateActions.updateCollection(data));
153 dispatch(snackbarActions.OPEN_SNACKBAR({
154 message: "Collection has been successfully updated.",
157 dispatch<any>(updateResources([collection]));
158 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
162 export const copyCollection = (data: collectionCopyActions.CollectionCopyFormDialogData) =>
163 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
165 const collection = await dispatch<any>(collectionCopyActions.copyCollection(data));
166 dispatch<any>(updateResources([collection]));
167 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
168 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied.', hideDuration: 2000 }));
170 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
174 export const moveCollection = (data: MoveToFormDialogData) =>
175 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
177 const collection = await dispatch<any>(collectionMoveActions.moveCollection(data));
178 dispatch<any>(updateResources([collection]));
179 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
180 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been moved.', hideDuration: 2000 }));
182 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
186 export const loadProcess = (uuid: string) =>
187 async (dispatch: Dispatch, getState: () => RootState) => {
188 await dispatch<any>(processesActions.loadProcess(uuid));
189 const process = getProcess(uuid)(getState().resources);
191 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
192 dispatch<any>(setCollectionBreadcrumbs(process.containerRequest.ownerUuid));
193 dispatch(loadDetailsPanel(uuid));
197 export const resourceIsNotLoaded = (uuid: string) =>
198 snackbarActions.OPEN_SNACKBAR({
199 message: `Resource identified by ${uuid} is not loaded.`
202 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
203 message: 'User is not authenticated'
206 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
207 message: 'Could not load user'
210 export const reloadProjectMatchingUuid = (matchingUuids: string[]) =>
211 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
212 const currentProjectPanelUuid = getProjectPanelCurrentUuid(getState());
213 if (currentProjectPanelUuid && matchingUuids.some(uuid => uuid === currentProjectPanelUuid)) {
214 dispatch<any>(loadProject(currentProjectPanelUuid));