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 { snackbarActions } from '../snackbar/snackbar-actions';
9 import { loadFavoritePanel } from '../favorite-panel/favorite-panel-action';
10 import { openProjectPanel, projectPanelActions } from '~/store/project-panel/project-panel-action';
11 import { activateSidePanelTreeItem, initSidePanelTree, SidePanelTreeCategory, loadSidePanelTreeProjects } from '../side-panel-tree/side-panel-tree-actions';
12 import { loadResource, updateResources } from '../resources/resources-actions';
13 import { favoritePanelActions } from '~/store/favorite-panel/favorite-panel-action';
14 import { projectPanelColumns } from '~/views/project-panel/project-panel';
15 import { favoritePanelColumns } from '~/views/favorite-panel/favorite-panel';
16 import { matchRootRoute } from '~/routes/routes';
17 import { setSidePanelBreadcrumbs, setProcessBreadcrumbs, setSharedWithMeBreadcrumbs, setTrashBreadcrumbs } from '../breadcrumbs/breadcrumbs-actions';
18 import { navigateToProject } from '../navigation/navigation-action';
19 import { MoveToFormDialogData } from '~/store/move-to-dialog/move-to-dialog';
20 import { ServiceRepository } from '~/services/services';
21 import { getResource } from '../resources/resources';
22 import { getProjectPanelCurrentUuid } from '../project-panel/project-panel-action';
23 import * as projectCreateActions from '~/store/projects/project-create-actions';
24 import * as projectMoveActions from '~/store/projects/project-move-actions';
25 import * as projectUpdateActions from '~/store/projects/project-update-actions';
26 import * as collectionCreateActions from '~/store/collections/collection-create-actions';
27 import * as collectionCopyActions from '~/store/collections/collection-copy-actions';
28 import * as collectionUpdateActions from '~/store/collections/collection-update-actions';
29 import * as collectionMoveActions from '~/store/collections/collection-move-actions';
30 import * as processesActions from '../processes/processes-actions';
31 import * as processMoveActions from '~/store/processes/process-move-actions';
32 import * as processUpdateActions from '~/store/processes/process-update-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 { sharedWithMePanelActions } from '~/store/shared-with-me-panel/shared-with-me-panel-actions';
39 import { loadSharedWithMePanel } from '../shared-with-me-panel/shared-with-me-panel-actions';
40 import { CopyFormDialogData } from '~/store/copy-dialog/copy-dialog';
41 import { loadWorkflowPanel, workflowPanelActions } from '~/store/workflow-panel/workflow-panel-actions';
42 import { workflowPanelColumns } from '~/views/workflow-panel/workflow-panel-view';
43 import { progressIndicatorActions } from '~/store/progress-indicator/progress-indicator-actions';
44 import { getProgressIndicator } from '../progress-indicator/progress-indicator-reducer';
45 import { ResourceKind, extractUuidKind } from '~/models/resource';
46 import { FilterBuilder } from '~/services/api/filter-builder';
47 import { GroupContentsResource } from '~/services/groups-service/groups-service';
48 import { unionize, ofType, UnionOf, MatchCases } from '~/common/unionize';
49 import { loadRunProcessPanel } from '~/store/run-process-panel/run-process-panel-actions';
50 import { loadCollectionFiles } from '~/store/collection-panel/collection-panel-files/collection-panel-files-actions';
52 export const WORKBENCH_LOADING_SCREEN = 'workbenchLoadingScreen';
54 export const isWorkbenchLoading = (state: RootState) => {
55 const progress = getProgressIndicator(WORKBENCH_LOADING_SCREEN)(state.progressIndicator);
56 return progress ? progress.working : false;
59 const handleFirstTimeLoad = (action: any) =>
60 async (dispatch: Dispatch<any>, getState: () => RootState) => {
62 await dispatch(action);
64 if (isWorkbenchLoading(getState())) {
65 dispatch(progressIndicatorActions.STOP_WORKING(WORKBENCH_LOADING_SCREEN));
71 export const loadWorkbench = () =>
72 async (dispatch: Dispatch, getState: () => RootState) => {
73 dispatch(progressIndicatorActions.START_WORKING(WORKBENCH_LOADING_SCREEN));
74 const { auth, router } = getState();
75 const { user } = auth;
77 const userResource = await dispatch<any>(loadResource(user.uuid));
79 dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
80 dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
81 dispatch(trashPanelActions.SET_COLUMNS({ columns: trashPanelColumns }));
82 dispatch(sharedWithMePanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
83 dispatch(workflowPanelActions.SET_COLUMNS({ columns: workflowPanelColumns }));
84 dispatch<any>(initSidePanelTree());
85 if (router.location) {
86 const match = matchRootRoute(router.location.pathname);
88 dispatch(navigateToProject(userResource.uuid));
92 dispatch(userIsNotAuthenticated);
95 dispatch(userIsNotAuthenticated);
99 export const loadFavorites = () =>
101 (dispatch: Dispatch) => {
102 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
103 dispatch<any>(loadFavoritePanel());
104 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
107 export const loadTrash = () =>
109 (dispatch: Dispatch) => {
110 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
111 dispatch<any>(loadTrashPanel());
112 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.TRASH));
115 export const loadProject = (uuid: string) =>
117 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
118 const userUuid = services.authService.getUuid();
120 if (userUuid !== uuid) {
121 const match = await loadGroupContentsResource({ uuid, userUuid, services });
123 OWNED: async project => {
124 await dispatch(activateSidePanelTreeItem(uuid));
125 dispatch<any>(setSidePanelBreadcrumbs(uuid));
126 dispatch(finishLoadingProject(project));
129 dispatch<any>(setSharedWithMeBreadcrumbs(uuid));
130 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
131 dispatch(finishLoadingProject(project));
133 TRASHED: project => {
134 dispatch<any>(setTrashBreadcrumbs(uuid));
135 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
136 dispatch(finishLoadingProject(project));
140 await dispatch(activateSidePanelTreeItem(userUuid));
141 dispatch<any>(setSidePanelBreadcrumbs(userUuid));
142 dispatch(finishLoadingProject(userUuid));
147 export const createProject = (data: projectCreateActions.ProjectCreateFormDialogData) =>
148 async (dispatch: Dispatch) => {
149 const newProject = await dispatch<any>(projectCreateActions.createProject(data));
151 dispatch(snackbarActions.OPEN_SNACKBAR({
152 message: "Project has been successfully created.",
155 await dispatch<any>(loadSidePanelTreeProjects(newProject.ownerUuid));
156 dispatch<any>(reloadProjectMatchingUuid([newProject.ownerUuid]));
160 export const moveProject = (data: MoveToFormDialogData) =>
161 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
163 const oldProject = getResource(data.uuid)(getState().resources);
164 const oldOwnerUuid = oldProject ? oldProject.ownerUuid : '';
165 const movedProject = await dispatch<any>(projectMoveActions.moveProject(data));
167 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Project has been moved', hideDuration: 2000 }));
169 await dispatch<any>(loadSidePanelTreeProjects(oldProject.ownerUuid));
171 dispatch<any>(reloadProjectMatchingUuid([oldOwnerUuid, movedProject.ownerUuid, movedProject.uuid]));
174 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
178 export const updateProject = (data: projectUpdateActions.ProjectUpdateFormDialogData) =>
179 async (dispatch: Dispatch) => {
180 const updatedProject = await dispatch<any>(projectUpdateActions.updateProject(data));
181 if (updatedProject) {
182 dispatch(snackbarActions.OPEN_SNACKBAR({
183 message: "Project has been successfully updated.",
186 await dispatch<any>(loadSidePanelTreeProjects(updatedProject.ownerUuid));
187 dispatch<any>(reloadProjectMatchingUuid([updatedProject.ownerUuid, updatedProject.uuid]));
191 export const loadCollection = (uuid: string) =>
193 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
194 const userUuid = services.authService.getUuid();
196 const match = await loadGroupContentsResource({ uuid, userUuid, services });
198 OWNED: async collection => {
199 dispatch(updateResources([collection]));
200 await dispatch(activateSidePanelTreeItem(collection.ownerUuid));
201 dispatch(setSidePanelBreadcrumbs(collection.ownerUuid));
202 dispatch(loadCollectionFiles(collection.uuid));
204 SHARED: collection => {
205 dispatch(updateResources([collection]));
206 dispatch<any>(setSharedWithMeBreadcrumbs(collection.ownerUuid));
207 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
208 dispatch(loadCollectionFiles(collection.uuid));
210 TRASHED: collection => {
211 dispatch(updateResources([collection]));
212 dispatch(setTrashBreadcrumbs(''));
213 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
214 dispatch(loadCollectionFiles(collection.uuid));
221 export const createCollection = (data: collectionCreateActions.CollectionCreateFormDialogData) =>
222 async (dispatch: Dispatch) => {
223 const collection = await dispatch<any>(collectionCreateActions.createCollection(data));
225 dispatch(snackbarActions.OPEN_SNACKBAR({
226 message: "Collection has been successfully created.",
229 dispatch<any>(updateResources([collection]));
230 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
234 export const updateCollection = (data: collectionUpdateActions.CollectionUpdateFormDialogData) =>
235 async (dispatch: Dispatch) => {
236 const collection = await dispatch<any>(collectionUpdateActions.updateCollection(data));
238 dispatch(snackbarActions.OPEN_SNACKBAR({
239 message: "Collection has been successfully updated.",
242 dispatch<any>(updateResources([collection]));
243 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
247 export const copyCollection = (data: CopyFormDialogData) =>
248 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
250 const collection = await dispatch<any>(collectionCopyActions.copyCollection(data));
251 dispatch<any>(updateResources([collection]));
252 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
253 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied.', hideDuration: 2000 }));
255 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
259 export const moveCollection = (data: MoveToFormDialogData) =>
260 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
262 const collection = await dispatch<any>(collectionMoveActions.moveCollection(data));
263 dispatch<any>(updateResources([collection]));
264 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
265 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been moved.', hideDuration: 2000 }));
267 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
271 export const loadProcess = (uuid: string) =>
273 async (dispatch: Dispatch, getState: () => RootState) => {
274 dispatch<any>(loadProcessPanel(uuid));
275 const process = await dispatch<any>(processesActions.loadProcess(uuid));
276 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
277 dispatch<any>(setProcessBreadcrumbs(uuid));
278 dispatch(loadDetailsPanel(uuid));
281 export const updateProcess = (data: processUpdateActions.ProcessUpdateFormDialogData) =>
282 async (dispatch: Dispatch) => {
284 const process = await dispatch<any>(processUpdateActions.updateProcess(data));
286 dispatch(snackbarActions.OPEN_SNACKBAR({
287 message: "Process has been successfully updated.",
290 dispatch<any>(updateResources([process]));
291 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
294 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
298 export const moveProcess = (data: MoveToFormDialogData) =>
299 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
301 const process = await dispatch<any>(processMoveActions.moveProcess(data));
302 dispatch<any>(updateResources([process]));
303 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
304 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been moved.', hideDuration: 2000 }));
306 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
310 export const copyProcess = (data: CopyFormDialogData) =>
311 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
313 const process = await dispatch<any>(processCopyActions.copyProcess(data));
314 dispatch<any>(updateResources([process]));
315 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
316 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been copied.', hideDuration: 2000 }));
318 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
322 export const loadProcessLog = (uuid: string) =>
324 async (dispatch: Dispatch) => {
325 const process = await dispatch<any>(processesActions.loadProcess(uuid));
326 dispatch<any>(setProcessBreadcrumbs(uuid));
327 dispatch<any>(initProcessLogsPanel(uuid));
328 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
331 export const resourceIsNotLoaded = (uuid: string) =>
332 snackbarActions.OPEN_SNACKBAR({
333 message: `Resource identified by ${uuid} is not loaded.`
336 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
337 message: 'User is not authenticated'
340 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
341 message: 'Could not load user'
344 export const reloadProjectMatchingUuid = (matchingUuids: string[]) =>
345 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
346 const currentProjectPanelUuid = getProjectPanelCurrentUuid(getState());
347 if (currentProjectPanelUuid && matchingUuids.some(uuid => uuid === currentProjectPanelUuid)) {
348 dispatch<any>(loadProject(currentProjectPanelUuid));
352 export const loadSharedWithMe = handleFirstTimeLoad(async (dispatch: Dispatch) => {
353 dispatch<any>(loadSharedWithMePanel());
354 await dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
355 await dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.SHARED_WITH_ME));
358 export const loadRunProcess = handleFirstTimeLoad(
359 async (dispatch: Dispatch) => {
360 dispatch<any>(loadRunProcessPanel());
364 export const loadWorkflow = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
365 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.WORKFLOWS));
366 await dispatch(loadWorkflowPanel());
367 dispatch(setSidePanelBreadcrumbs(SidePanelTreeCategory.WORKFLOWS));
370 const finishLoadingProject = (project: GroupContentsResource | string) =>
371 async (dispatch: Dispatch<any>) => {
372 const uuid = typeof project === 'string' ? project : project.uuid;
373 dispatch(openProjectPanel(uuid));
374 dispatch(loadDetailsPanel(uuid));
375 if (typeof project !== 'string') {
376 dispatch(updateResources([project]));
380 const loadGroupContentsResource = async (params: {
383 services: ServiceRepository
385 const filters = new FilterBuilder()
386 .addEqual('uuid', params.uuid)
388 const { items } = await params.services.groupsService.contents(params.userUuid, {
393 const resource = items.shift();
394 let handler: GroupContentsHandler;
396 handler = (resource.kind === ResourceKind.COLLECTION || resource.kind === ResourceKind.PROJECT) && resource.isTrashed
397 ? groupContentsHandlers.TRASHED(resource)
398 : groupContentsHandlers.OWNED(resource);
400 const kind = extractUuidKind(params.uuid);
401 let resource: GroupContentsResource;
402 if (kind === ResourceKind.COLLECTION) {
403 resource = await params.services.collectionService.get(params.uuid);
404 } else if (kind === ResourceKind.PROJECT) {
405 resource = await params.services.projectService.get(params.uuid);
407 resource = await params.services.containerRequestService.get(params.uuid);
409 handler = groupContentsHandlers.SHARED(resource);
411 return (cases: MatchCases<typeof groupContentsHandlersRecord, GroupContentsHandler, void>) =>
412 groupContentsHandlers.match(handler, cases);
416 const groupContentsHandlersRecord = {
417 TRASHED: ofType<GroupContentsResource>(),
418 SHARED: ofType<GroupContentsResource>(),
419 OWNED: ofType<GroupContentsResource>(),
422 const groupContentsHandlers = unionize(groupContentsHandlersRecord);
424 type GroupContentsHandler = UnionOf<typeof groupContentsHandlers>;