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';
51 export const WORKBENCH_LOADING_SCREEN = 'workbenchLoadingScreen';
53 export const isWorkbenchLoading = (state: RootState) => {
54 const progress = getProgressIndicator(WORKBENCH_LOADING_SCREEN)(state.progressIndicator);
55 return progress ? progress.working : false;
58 const handleFirstTimeLoad = (action: any) =>
59 async (dispatch: Dispatch<any>, getState: () => RootState) => {
61 await dispatch(action);
63 if (isWorkbenchLoading(getState())) {
64 dispatch(progressIndicatorActions.STOP_WORKING(WORKBENCH_LOADING_SCREEN));
70 export const loadWorkbench = () =>
71 async (dispatch: Dispatch, getState: () => RootState) => {
72 dispatch(progressIndicatorActions.START_WORKING(WORKBENCH_LOADING_SCREEN));
73 const { auth, router } = getState();
74 const { user } = auth;
76 const userResource = await dispatch<any>(loadResource(user.uuid));
78 dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
79 dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
80 dispatch(trashPanelActions.SET_COLUMNS({ columns: trashPanelColumns }));
81 dispatch(sharedWithMePanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
82 dispatch(workflowPanelActions.SET_COLUMNS({ columns: workflowPanelColumns }));
83 dispatch<any>(initSidePanelTree());
84 if (router.location) {
85 const match = matchRootRoute(router.location.pathname);
87 dispatch(navigateToProject(userResource.uuid));
91 dispatch(userIsNotAuthenticated);
94 dispatch(userIsNotAuthenticated);
98 export const loadFavorites = () =>
100 (dispatch: Dispatch) => {
101 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
102 dispatch<any>(loadFavoritePanel());
103 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
106 export const loadTrash = () =>
108 (dispatch: Dispatch) => {
109 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
110 dispatch<any>(loadTrashPanel());
111 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.TRASH));
114 export const loadProject = (uuid: string) =>
116 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
117 const userUuid = services.authService.getUuid();
119 if (userUuid !== uuid) {
120 const match = await loadGroupContentsResource({ uuid, userUuid, services });
122 OWNED: async project => {
123 await dispatch(activateSidePanelTreeItem(uuid));
124 dispatch<any>(setSidePanelBreadcrumbs(uuid));
125 dispatch(finishLoadingProject(project));
128 dispatch<any>(setSharedWithMeBreadcrumbs(uuid));
129 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
130 dispatch(finishLoadingProject(project));
132 TRASHED: project => {
133 dispatch<any>(setTrashBreadcrumbs(uuid));
134 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
135 dispatch(finishLoadingProject(project));
139 await dispatch(activateSidePanelTreeItem(userUuid));
140 dispatch<any>(setSidePanelBreadcrumbs(userUuid));
141 dispatch(finishLoadingProject(userUuid));
146 export const createProject = (data: projectCreateActions.ProjectCreateFormDialogData) =>
147 async (dispatch: Dispatch) => {
148 const newProject = await dispatch<any>(projectCreateActions.createProject(data));
150 dispatch(snackbarActions.OPEN_SNACKBAR({
151 message: "Project has been successfully created.",
154 await dispatch<any>(loadSidePanelTreeProjects(newProject.ownerUuid));
155 dispatch<any>(reloadProjectMatchingUuid([newProject.ownerUuid]));
159 export const moveProject = (data: MoveToFormDialogData) =>
160 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
162 const oldProject = getResource(data.uuid)(getState().resources);
163 const oldOwnerUuid = oldProject ? oldProject.ownerUuid : '';
164 const movedProject = await dispatch<any>(projectMoveActions.moveProject(data));
166 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Project has been moved', hideDuration: 2000 }));
168 await dispatch<any>(loadSidePanelTreeProjects(oldProject.ownerUuid));
170 dispatch<any>(reloadProjectMatchingUuid([oldOwnerUuid, movedProject.ownerUuid, movedProject.uuid]));
173 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
177 export const updateProject = (data: projectUpdateActions.ProjectUpdateFormDialogData) =>
178 async (dispatch: Dispatch) => {
179 const updatedProject = await dispatch<any>(projectUpdateActions.updateProject(data));
180 if (updatedProject) {
181 dispatch(snackbarActions.OPEN_SNACKBAR({
182 message: "Project has been successfully updated.",
185 await dispatch<any>(loadSidePanelTreeProjects(updatedProject.ownerUuid));
186 dispatch<any>(reloadProjectMatchingUuid([updatedProject.ownerUuid, updatedProject.uuid]));
190 export const loadCollection = (uuid: string) =>
192 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
193 const userUuid = services.authService.getUuid();
195 const match = await loadGroupContentsResource({ uuid, userUuid, services });
197 OWNED: async collection => {
198 dispatch(updateResources([collection]));
199 await dispatch(activateSidePanelTreeItem(collection.ownerUuid));
200 dispatch(setSidePanelBreadcrumbs(collection.ownerUuid));
202 SHARED: collection => {
203 dispatch(updateResources([collection]));
204 dispatch<any>(setSharedWithMeBreadcrumbs(collection.ownerUuid));
205 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
207 TRASHED: collection => {
208 dispatch(updateResources([collection]));
209 dispatch(setTrashBreadcrumbs(''));
210 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
217 export const createCollection = (data: collectionCreateActions.CollectionCreateFormDialogData) =>
218 async (dispatch: Dispatch) => {
219 const collection = await dispatch<any>(collectionCreateActions.createCollection(data));
221 dispatch(snackbarActions.OPEN_SNACKBAR({
222 message: "Collection has been successfully created.",
225 dispatch<any>(updateResources([collection]));
226 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
230 export const updateCollection = (data: collectionUpdateActions.CollectionUpdateFormDialogData) =>
231 async (dispatch: Dispatch) => {
232 const collection = await dispatch<any>(collectionUpdateActions.updateCollection(data));
234 dispatch(snackbarActions.OPEN_SNACKBAR({
235 message: "Collection has been successfully updated.",
238 dispatch<any>(updateResources([collection]));
239 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
243 export const copyCollection = (data: CopyFormDialogData) =>
244 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
246 const collection = await dispatch<any>(collectionCopyActions.copyCollection(data));
247 dispatch<any>(updateResources([collection]));
248 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
249 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied.', hideDuration: 2000 }));
251 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
255 export const moveCollection = (data: MoveToFormDialogData) =>
256 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
258 const collection = await dispatch<any>(collectionMoveActions.moveCollection(data));
259 dispatch<any>(updateResources([collection]));
260 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
261 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been moved.', hideDuration: 2000 }));
263 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
267 export const loadProcess = (uuid: string) =>
269 async (dispatch: Dispatch, getState: () => RootState) => {
270 dispatch<any>(loadProcessPanel(uuid));
271 const process = await dispatch<any>(processesActions.loadProcess(uuid));
272 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
273 dispatch<any>(setProcessBreadcrumbs(uuid));
274 dispatch(loadDetailsPanel(uuid));
277 export const updateProcess = (data: processUpdateActions.ProcessUpdateFormDialogData) =>
278 async (dispatch: Dispatch) => {
280 const process = await dispatch<any>(processUpdateActions.updateProcess(data));
282 dispatch(snackbarActions.OPEN_SNACKBAR({
283 message: "Process has been successfully updated.",
286 dispatch<any>(updateResources([process]));
287 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
290 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
294 export const moveProcess = (data: MoveToFormDialogData) =>
295 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
297 const process = await dispatch<any>(processMoveActions.moveProcess(data));
298 dispatch<any>(updateResources([process]));
299 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
300 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been moved.', hideDuration: 2000 }));
302 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
306 export const copyProcess = (data: CopyFormDialogData) =>
307 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
309 const process = await dispatch<any>(processCopyActions.copyProcess(data));
310 dispatch<any>(updateResources([process]));
311 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
312 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been copied.', hideDuration: 2000 }));
314 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
318 export const loadProcessLog = (uuid: string) =>
320 async (dispatch: Dispatch) => {
321 const process = await dispatch<any>(processesActions.loadProcess(uuid));
322 dispatch<any>(setProcessBreadcrumbs(uuid));
323 dispatch<any>(initProcessLogsPanel(uuid));
324 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
327 export const resourceIsNotLoaded = (uuid: string) =>
328 snackbarActions.OPEN_SNACKBAR({
329 message: `Resource identified by ${uuid} is not loaded.`
332 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
333 message: 'User is not authenticated'
336 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
337 message: 'Could not load user'
340 export const reloadProjectMatchingUuid = (matchingUuids: string[]) =>
341 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
342 const currentProjectPanelUuid = getProjectPanelCurrentUuid(getState());
343 if (currentProjectPanelUuid && matchingUuids.some(uuid => uuid === currentProjectPanelUuid)) {
344 dispatch<any>(loadProject(currentProjectPanelUuid));
348 export const loadSharedWithMe = handleFirstTimeLoad(async (dispatch: Dispatch) => {
349 dispatch<any>(loadSharedWithMePanel());
350 await dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
351 await dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.SHARED_WITH_ME));
354 export const loadRunProcess = handleFirstTimeLoad(
355 async (dispatch: Dispatch) => {
356 await dispatch<any>(loadRunProcessPanel());
360 export const loadWorkflow = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
361 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.WORKFLOWS));
362 await dispatch(loadWorkflowPanel());
363 dispatch(setSidePanelBreadcrumbs(SidePanelTreeCategory.WORKFLOWS));
366 const finishLoadingProject = (project: GroupContentsResource | string) =>
367 async (dispatch: Dispatch<any>) => {
368 const uuid = typeof project === 'string' ? project : project.uuid;
369 dispatch(openProjectPanel(uuid));
370 dispatch(loadDetailsPanel(uuid));
371 if (typeof project !== 'string') {
372 dispatch(updateResources([project]));
376 const loadGroupContentsResource = async (params: {
379 services: ServiceRepository
381 const filters = new FilterBuilder()
382 .addEqual('uuid', params.uuid)
384 const { items } = await params.services.groupsService.contents(params.userUuid, {
389 const resource = items.shift();
390 let handler: GroupContentsHandler;
392 handler = (resource.kind === ResourceKind.COLLECTION || resource.kind === ResourceKind.PROJECT) && resource.isTrashed
393 ? groupContentsHandlers.TRASHED(resource)
394 : groupContentsHandlers.OWNED(resource);
396 const kind = extractUuidKind(params.uuid);
397 let resource: GroupContentsResource;
398 if (kind === ResourceKind.COLLECTION) {
399 resource = await params.services.collectionService.get(params.uuid);
400 } else if (kind === ResourceKind.PROJECT) {
401 resource = await params.services.projectService.get(params.uuid);
403 resource = await params.services.containerRequestService.get(params.uuid);
405 handler = groupContentsHandlers.SHARED(resource);
407 return (cases: MatchCases<typeof groupContentsHandlersRecord, GroupContentsHandler, void>) =>
408 groupContentsHandlers.match(handler, cases);
412 const groupContentsHandlersRecord = {
413 TRASHED: ofType<GroupContentsResource>(),
414 SHARED: ofType<GroupContentsResource>(),
415 OWNED: ofType<GroupContentsResource>(),
418 const groupContentsHandlers = unionize(groupContentsHandlersRecord);
420 type GroupContentsHandler = UnionOf<typeof groupContentsHandlers>;