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';
51 import { SnackbarKind } from '~/store/snackbar/snackbar-actions';
52 import { collectionPanelActions } from "~/store/collection-panel/collection-panel-action";
53 import { CollectionResource } from "~/models/collection";
55 export const WORKBENCH_LOADING_SCREEN = 'workbenchLoadingScreen';
57 export const isWorkbenchLoading = (state: RootState) => {
58 const progress = getProgressIndicator(WORKBENCH_LOADING_SCREEN)(state.progressIndicator);
59 return progress ? progress.working : false;
62 const handleFirstTimeLoad = (action: any) =>
63 async (dispatch: Dispatch<any>, getState: () => RootState) => {
65 await dispatch(action);
67 if (isWorkbenchLoading(getState())) {
68 dispatch(progressIndicatorActions.STOP_WORKING(WORKBENCH_LOADING_SCREEN));
74 export const loadWorkbench = () =>
75 async (dispatch: Dispatch, getState: () => RootState) => {
76 dispatch(progressIndicatorActions.START_WORKING(WORKBENCH_LOADING_SCREEN));
77 const { auth, router } = getState();
78 const { user } = auth;
80 const userResource = await dispatch<any>(loadResource(user.uuid));
82 dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
83 dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
84 dispatch(trashPanelActions.SET_COLUMNS({ columns: trashPanelColumns }));
85 dispatch(sharedWithMePanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
86 dispatch(workflowPanelActions.SET_COLUMNS({ columns: workflowPanelColumns }));
87 dispatch<any>(initSidePanelTree());
88 if (router.location) {
89 const match = matchRootRoute(router.location.pathname);
91 dispatch(navigateToProject(userResource.uuid));
95 dispatch(userIsNotAuthenticated);
98 dispatch(userIsNotAuthenticated);
102 export const loadFavorites = () =>
104 (dispatch: Dispatch) => {
105 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
106 dispatch<any>(loadFavoritePanel());
107 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
110 export const loadTrash = () =>
112 (dispatch: Dispatch) => {
113 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
114 dispatch<any>(loadTrashPanel());
115 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.TRASH));
118 export const loadProject = (uuid: string) =>
120 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
121 const userUuid = services.authService.getUuid();
123 if (userUuid !== uuid) {
124 const match = await loadGroupContentsResource({ uuid, userUuid, services });
126 OWNED: async project => {
127 await dispatch(activateSidePanelTreeItem(uuid));
128 dispatch<any>(setSidePanelBreadcrumbs(uuid));
129 dispatch(finishLoadingProject(project));
132 dispatch<any>(setSharedWithMeBreadcrumbs(uuid));
133 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
134 dispatch(finishLoadingProject(project));
136 TRASHED: project => {
137 dispatch<any>(setTrashBreadcrumbs(uuid));
138 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
139 dispatch(finishLoadingProject(project));
143 await dispatch(activateSidePanelTreeItem(userUuid));
144 dispatch<any>(setSidePanelBreadcrumbs(userUuid));
145 dispatch(finishLoadingProject(userUuid));
150 export const createProject = (data: projectCreateActions.ProjectCreateFormDialogData) =>
151 async (dispatch: Dispatch) => {
152 const newProject = await dispatch<any>(projectCreateActions.createProject(data));
154 dispatch(snackbarActions.OPEN_SNACKBAR({
155 message: "Project has been successfully created.",
158 await dispatch<any>(loadSidePanelTreeProjects(newProject.ownerUuid));
159 dispatch<any>(reloadProjectMatchingUuid([newProject.ownerUuid]));
163 export const moveProject = (data: MoveToFormDialogData) =>
164 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
166 const oldProject = getResource(data.uuid)(getState().resources);
167 const oldOwnerUuid = oldProject ? oldProject.ownerUuid : '';
168 const movedProject = await dispatch<any>(projectMoveActions.moveProject(data));
170 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Project has been moved', hideDuration: 2000 }));
172 await dispatch<any>(loadSidePanelTreeProjects(oldProject.ownerUuid));
174 dispatch<any>(reloadProjectMatchingUuid([oldOwnerUuid, movedProject.ownerUuid, movedProject.uuid]));
177 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
181 export const updateProject = (data: projectUpdateActions.ProjectUpdateFormDialogData) =>
182 async (dispatch: Dispatch) => {
183 const updatedProject = await dispatch<any>(projectUpdateActions.updateProject(data));
184 if (updatedProject) {
185 dispatch(snackbarActions.OPEN_SNACKBAR({
186 message: "Project has been successfully updated.",
189 await dispatch<any>(loadSidePanelTreeProjects(updatedProject.ownerUuid));
190 dispatch<any>(reloadProjectMatchingUuid([updatedProject.ownerUuid, updatedProject.uuid]));
194 export const loadCollection = (uuid: string) =>
196 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
197 const userUuid = services.authService.getUuid();
199 const match = await loadGroupContentsResource({ uuid, userUuid, services });
201 OWNED: async collection => {
202 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
203 dispatch(updateResources([collection]));
204 await dispatch(activateSidePanelTreeItem(collection.ownerUuid));
205 dispatch(setSidePanelBreadcrumbs(collection.ownerUuid));
206 dispatch(loadCollectionFiles(collection.uuid));
208 SHARED: collection => {
209 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
210 dispatch(updateResources([collection]));
211 dispatch<any>(setSharedWithMeBreadcrumbs(collection.ownerUuid));
212 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
213 dispatch(loadCollectionFiles(collection.uuid));
215 TRASHED: collection => {
216 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
217 dispatch(updateResources([collection]));
218 dispatch(setTrashBreadcrumbs(''));
219 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
220 dispatch(loadCollectionFiles(collection.uuid));
227 export const createCollection = (data: collectionCreateActions.CollectionCreateFormDialogData) =>
228 async (dispatch: Dispatch) => {
229 const collection = await dispatch<any>(collectionCreateActions.createCollection(data));
231 dispatch(snackbarActions.OPEN_SNACKBAR({
232 message: "Collection has been successfully created.",
235 dispatch<any>(updateResources([collection]));
236 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
240 export const updateCollection = (data: collectionUpdateActions.CollectionUpdateFormDialogData) =>
241 async (dispatch: Dispatch) => {
242 const collection = await dispatch<any>(collectionUpdateActions.updateCollection(data));
244 dispatch(snackbarActions.OPEN_SNACKBAR({
245 message: "Collection has been successfully updated.",
248 dispatch<any>(updateResources([collection]));
249 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
253 export const copyCollection = (data: CopyFormDialogData) =>
254 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
256 const collection = await dispatch<any>(collectionCopyActions.copyCollection(data));
257 dispatch<any>(updateResources([collection]));
258 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
259 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied.', hideDuration: 2000 }));
261 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
265 export const moveCollection = (data: MoveToFormDialogData) =>
266 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
268 const collection = await dispatch<any>(collectionMoveActions.moveCollection(data));
269 dispatch<any>(updateResources([collection]));
270 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
271 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been moved.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
273 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
277 export const loadProcess = (uuid: string) =>
279 async (dispatch: Dispatch, getState: () => RootState) => {
280 dispatch<any>(loadProcessPanel(uuid));
281 const process = await dispatch<any>(processesActions.loadProcess(uuid));
282 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
283 dispatch<any>(setProcessBreadcrumbs(uuid));
284 dispatch(loadDetailsPanel(uuid));
287 export const updateProcess = (data: processUpdateActions.ProcessUpdateFormDialogData) =>
288 async (dispatch: Dispatch) => {
290 const process = await dispatch<any>(processUpdateActions.updateProcess(data));
292 dispatch(snackbarActions.OPEN_SNACKBAR({
293 message: "Process has been successfully updated.",
296 dispatch<any>(updateResources([process]));
297 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
300 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
304 export const moveProcess = (data: MoveToFormDialogData) =>
305 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
307 const process = await dispatch<any>(processMoveActions.moveProcess(data));
308 dispatch<any>(updateResources([process]));
309 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
310 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been moved.', hideDuration: 2000 }));
312 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
316 export const copyProcess = (data: CopyFormDialogData) =>
317 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
319 const process = await dispatch<any>(processCopyActions.copyProcess(data));
320 dispatch<any>(updateResources([process]));
321 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
322 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been copied.', hideDuration: 2000 }));
324 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
328 export const loadProcessLog = (uuid: string) =>
330 async (dispatch: Dispatch) => {
331 const process = await dispatch<any>(processesActions.loadProcess(uuid));
332 dispatch<any>(setProcessBreadcrumbs(uuid));
333 dispatch<any>(initProcessLogsPanel(uuid));
334 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
337 export const resourceIsNotLoaded = (uuid: string) =>
338 snackbarActions.OPEN_SNACKBAR({
339 message: `Resource identified by ${uuid} is not loaded.`
342 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
343 message: 'User is not authenticated'
346 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
347 message: 'Could not load user'
350 export const reloadProjectMatchingUuid = (matchingUuids: string[]) =>
351 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
352 const currentProjectPanelUuid = getProjectPanelCurrentUuid(getState());
353 if (currentProjectPanelUuid && matchingUuids.some(uuid => uuid === currentProjectPanelUuid)) {
354 dispatch<any>(loadProject(currentProjectPanelUuid));
358 export const loadSharedWithMe = handleFirstTimeLoad(async (dispatch: Dispatch) => {
359 dispatch<any>(loadSharedWithMePanel());
360 await dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
361 await dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.SHARED_WITH_ME));
364 export const loadRunProcess = handleFirstTimeLoad(
365 async (dispatch: Dispatch) => {
366 await dispatch<any>(loadRunProcessPanel());
370 export const loadWorkflow = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
371 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.WORKFLOWS));
372 await dispatch(loadWorkflowPanel());
373 dispatch(setSidePanelBreadcrumbs(SidePanelTreeCategory.WORKFLOWS));
376 const finishLoadingProject = (project: GroupContentsResource | string) =>
377 async (dispatch: Dispatch<any>) => {
378 const uuid = typeof project === 'string' ? project : project.uuid;
379 dispatch(openProjectPanel(uuid));
380 dispatch(loadDetailsPanel(uuid));
381 if (typeof project !== 'string') {
382 dispatch(updateResources([project]));
386 const loadGroupContentsResource = async (params: {
389 services: ServiceRepository
391 const filters = new FilterBuilder()
392 .addEqual('uuid', params.uuid)
394 const { items } = await params.services.groupsService.contents(params.userUuid, {
399 const resource = items.shift();
400 let handler: GroupContentsHandler;
402 handler = (resource.kind === ResourceKind.COLLECTION || resource.kind === ResourceKind.PROJECT) && resource.isTrashed
403 ? groupContentsHandlers.TRASHED(resource)
404 : groupContentsHandlers.OWNED(resource);
406 const kind = extractUuidKind(params.uuid);
407 let resource: GroupContentsResource;
408 if (kind === ResourceKind.COLLECTION) {
409 resource = await params.services.collectionService.get(params.uuid);
410 } else if (kind === ResourceKind.PROJECT) {
411 resource = await params.services.projectService.get(params.uuid);
413 resource = await params.services.containerRequestService.get(params.uuid);
415 handler = groupContentsHandlers.SHARED(resource);
417 return (cases: MatchCases<typeof groupContentsHandlersRecord, GroupContentsHandler, void>) =>
418 groupContentsHandlers.match(handler, cases);
422 const groupContentsHandlersRecord = {
423 TRASHED: ofType<GroupContentsResource>(),
424 SHARED: ofType<GroupContentsResource>(),
425 OWNED: ofType<GroupContentsResource>(),
428 const groupContentsHandlers = unionize(groupContentsHandlersRecord);
430 type GroupContentsHandler = UnionOf<typeof groupContentsHandlers>;