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";
54 import { searchResultsPanelActions, loadSearchResultsPanel } from '~/store/search-results-panel/search-results-panel-actions';
55 import { searchResultsPanelColumns } from '~/views/search-results-panel/search-results-panel-view';
56 import * as uuid from 'uuid/v4';
58 export const WORKBENCH_LOADING_SCREEN = 'workbenchLoadingScreen';
60 export const isWorkbenchLoading = (state: RootState) => {
61 const progress = getProgressIndicator(WORKBENCH_LOADING_SCREEN)(state.progressIndicator);
62 return progress ? progress.working : false;
65 const handleFirstTimeLoad = (action: any) =>
66 async (dispatch: Dispatch<any>, getState: () => RootState) => {
68 await dispatch(action);
70 if (isWorkbenchLoading(getState())) {
71 dispatch(progressIndicatorActions.STOP_WORKING(WORKBENCH_LOADING_SCREEN));
77 export const loadWorkbench = () =>
78 async (dispatch: Dispatch, getState: () => RootState) => {
79 dispatch(progressIndicatorActions.START_WORKING(WORKBENCH_LOADING_SCREEN));
80 const { auth, router } = getState();
81 const { user } = auth;
83 const userResource = await dispatch<any>(loadResource(user.uuid));
85 dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
86 dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
87 dispatch(trashPanelActions.SET_COLUMNS({ columns: trashPanelColumns }));
88 dispatch(sharedWithMePanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
89 dispatch(workflowPanelActions.SET_COLUMNS({ columns: workflowPanelColumns }));
90 dispatch(searchResultsPanelActions.SET_COLUMNS({ columns: searchResultsPanelColumns }));
91 dispatch<any>(initSidePanelTree());
92 if (router.location) {
93 const match = matchRootRoute(router.location.pathname);
95 dispatch(navigateToProject(userResource.uuid));
99 dispatch(userIsNotAuthenticated);
102 dispatch(userIsNotAuthenticated);
106 export const loadFavorites = () =>
108 (dispatch: Dispatch) => {
109 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
110 dispatch<any>(loadFavoritePanel());
111 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
114 export const loadTrash = () =>
116 (dispatch: Dispatch) => {
117 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
118 dispatch<any>(loadTrashPanel());
119 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.TRASH));
122 export const loadProject = (uuid: string) =>
124 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
125 const userUuid = services.authService.getUuid();
127 if (userUuid !== uuid) {
128 const match = await loadGroupContentsResource({ uuid, userUuid, services });
130 OWNED: async project => {
131 await dispatch(activateSidePanelTreeItem(uuid));
132 dispatch<any>(setSidePanelBreadcrumbs(uuid));
133 dispatch(finishLoadingProject(project));
136 dispatch<any>(setSharedWithMeBreadcrumbs(uuid));
137 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
138 dispatch(finishLoadingProject(project));
140 TRASHED: project => {
141 dispatch<any>(setTrashBreadcrumbs(uuid));
142 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
143 dispatch(finishLoadingProject(project));
147 await dispatch(activateSidePanelTreeItem(userUuid));
148 dispatch<any>(setSidePanelBreadcrumbs(userUuid));
149 dispatch(finishLoadingProject(userUuid));
154 export const createProject = (data: projectCreateActions.ProjectCreateFormDialogData) =>
155 async (dispatch: Dispatch) => {
156 const newProject = await dispatch<any>(projectCreateActions.createProject(data));
158 dispatch(snackbarActions.OPEN_SNACKBAR({
159 message: "Project has been successfully created.",
162 await dispatch<any>(loadSidePanelTreeProjects(newProject.ownerUuid));
163 dispatch<any>(reloadProjectMatchingUuid([newProject.ownerUuid]));
167 export const moveProject = (data: MoveToFormDialogData) =>
168 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
170 const oldProject = getResource(data.uuid)(getState().resources);
171 const oldOwnerUuid = oldProject ? oldProject.ownerUuid : '';
172 const movedProject = await dispatch<any>(projectMoveActions.moveProject(data));
174 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Project has been moved', hideDuration: 2000 }));
176 await dispatch<any>(loadSidePanelTreeProjects(oldProject.ownerUuid));
178 dispatch<any>(reloadProjectMatchingUuid([oldOwnerUuid, movedProject.ownerUuid, movedProject.uuid]));
181 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
185 export const updateProject = (data: projectUpdateActions.ProjectUpdateFormDialogData) =>
186 async (dispatch: Dispatch) => {
187 const updatedProject = await dispatch<any>(projectUpdateActions.updateProject(data));
188 if (updatedProject) {
189 dispatch(snackbarActions.OPEN_SNACKBAR({
190 message: "Project has been successfully updated.",
193 await dispatch<any>(loadSidePanelTreeProjects(updatedProject.ownerUuid));
194 dispatch<any>(reloadProjectMatchingUuid([updatedProject.ownerUuid, updatedProject.uuid]));
198 export const loadCollection = (uuid: string) =>
200 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
201 const userUuid = services.authService.getUuid();
203 const match = await loadGroupContentsResource({ uuid, userUuid, services });
205 OWNED: async collection => {
206 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
207 dispatch(updateResources([collection]));
208 await dispatch(activateSidePanelTreeItem(collection.ownerUuid));
209 dispatch(setSidePanelBreadcrumbs(collection.ownerUuid));
210 dispatch(loadCollectionFiles(collection.uuid));
212 SHARED: collection => {
213 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
214 dispatch(updateResources([collection]));
215 dispatch<any>(setSharedWithMeBreadcrumbs(collection.ownerUuid));
216 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
217 dispatch(loadCollectionFiles(collection.uuid));
219 TRASHED: collection => {
220 dispatch(collectionPanelActions.SET_COLLECTION(collection as CollectionResource));
221 dispatch(updateResources([collection]));
222 dispatch(setTrashBreadcrumbs(''));
223 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
224 dispatch(loadCollectionFiles(collection.uuid));
231 export const createCollection = (data: collectionCreateActions.CollectionCreateFormDialogData) =>
232 async (dispatch: Dispatch) => {
233 const collection = await dispatch<any>(collectionCreateActions.createCollection(data));
235 dispatch(snackbarActions.OPEN_SNACKBAR({
236 message: "Collection has been successfully created.",
239 dispatch<any>(updateResources([collection]));
240 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
244 export const updateCollection = (data: collectionUpdateActions.CollectionUpdateFormDialogData) =>
245 async (dispatch: Dispatch) => {
246 const collection = await dispatch<any>(collectionUpdateActions.updateCollection(data));
248 dispatch(snackbarActions.OPEN_SNACKBAR({
249 message: "Collection has been successfully updated.",
252 dispatch<any>(updateResources([collection]));
253 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
257 export const copyCollection = (data: CopyFormDialogData) =>
258 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
260 const copyToProject = getResource(data.ownerUuid)(getState().resources);
261 const collection = await dispatch<any>(collectionCopyActions.copyCollection(data));
262 if (copyToProject && collection) {
263 dispatch<any>(reloadProjectMatchingUuid([copyToProject.uuid]));
264 dispatch(snackbarActions.OPEN_SNACKBAR({
265 message: 'Collection has been copied.',
267 kind: SnackbarKind.SUCCESS,
268 link: collection.ownerUuid
272 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000, kind: SnackbarKind.ERROR }));
276 export const moveCollection = (data: MoveToFormDialogData) =>
277 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
279 const collection = await dispatch<any>(collectionMoveActions.moveCollection(data));
280 dispatch<any>(updateResources([collection]));
281 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
282 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been moved.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
284 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
288 export const loadProcess = (uuid: string) =>
290 async (dispatch: Dispatch, getState: () => RootState) => {
291 dispatch<any>(loadProcessPanel(uuid));
292 const process = await dispatch<any>(processesActions.loadProcess(uuid));
293 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
294 dispatch<any>(setProcessBreadcrumbs(uuid));
295 dispatch(loadDetailsPanel(uuid));
298 export const updateProcess = (data: processUpdateActions.ProcessUpdateFormDialogData) =>
299 async (dispatch: Dispatch) => {
301 const process = await dispatch<any>(processUpdateActions.updateProcess(data));
303 dispatch(snackbarActions.OPEN_SNACKBAR({
304 message: "Process has been successfully updated.",
307 dispatch<any>(updateResources([process]));
308 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
311 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
315 export const moveProcess = (data: MoveToFormDialogData) =>
316 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
318 const process = await dispatch<any>(processMoveActions.moveProcess(data));
319 dispatch<any>(updateResources([process]));
320 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
321 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been moved.', hideDuration: 2000 }));
323 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
327 export const copyProcess = (data: CopyFormDialogData) =>
328 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
330 const process = await dispatch<any>(processCopyActions.copyProcess(data));
331 dispatch<any>(updateResources([process]));
332 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
333 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been copied.', hideDuration: 2000 }));
335 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
339 export const loadProcessLog = (uuid: string) =>
341 async (dispatch: Dispatch) => {
342 const process = await dispatch<any>(processesActions.loadProcess(uuid));
343 dispatch<any>(setProcessBreadcrumbs(uuid));
344 dispatch<any>(initProcessLogsPanel(uuid));
345 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
348 export const resourceIsNotLoaded = (uuid: string) =>
349 snackbarActions.OPEN_SNACKBAR({
350 message: `Resource identified by ${uuid} is not loaded.`
353 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
354 message: 'User is not authenticated'
357 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
358 message: 'Could not load user'
361 export const reloadProjectMatchingUuid = (matchingUuids: string[]) =>
362 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
363 const currentProjectPanelUuid = getProjectPanelCurrentUuid(getState());
364 if (currentProjectPanelUuid && matchingUuids.some(uuid => uuid === currentProjectPanelUuid)) {
365 dispatch<any>(loadProject(currentProjectPanelUuid));
369 export const loadSharedWithMe = handleFirstTimeLoad(async (dispatch: Dispatch) => {
370 dispatch<any>(loadSharedWithMePanel());
371 await dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
372 await dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.SHARED_WITH_ME));
375 export const loadRunProcess = handleFirstTimeLoad(
376 async (dispatch: Dispatch) => {
377 await dispatch<any>(loadRunProcessPanel());
381 export const loadWorkflow = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
382 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.WORKFLOWS));
383 await dispatch(loadWorkflowPanel());
384 dispatch(setSidePanelBreadcrumbs(SidePanelTreeCategory.WORKFLOWS));
387 export const loadSearchResults = handleFirstTimeLoad(
388 async (dispatch: Dispatch<any>) => {
389 await dispatch(loadSearchResultsPanel());
392 const finishLoadingProject = (project: GroupContentsResource | string) =>
393 async (dispatch: Dispatch<any>) => {
394 const uuid = typeof project === 'string' ? project : project.uuid;
395 dispatch(openProjectPanel(uuid));
396 dispatch(loadDetailsPanel(uuid));
397 if (typeof project !== 'string') {
398 dispatch(updateResources([project]));
402 const loadGroupContentsResource = async (params: {
405 services: ServiceRepository
407 const filters = new FilterBuilder()
408 .addEqual('uuid', params.uuid)
410 const { items } = await params.services.groupsService.contents(params.userUuid, {
415 const resource = items.shift();
416 let handler: GroupContentsHandler;
418 handler = (resource.kind === ResourceKind.COLLECTION || resource.kind === ResourceKind.PROJECT) && resource.isTrashed
419 ? groupContentsHandlers.TRASHED(resource)
420 : groupContentsHandlers.OWNED(resource);
422 const kind = extractUuidKind(params.uuid);
423 let resource: GroupContentsResource;
424 if (kind === ResourceKind.COLLECTION) {
425 resource = await params.services.collectionService.get(params.uuid);
426 } else if (kind === ResourceKind.PROJECT) {
427 resource = await params.services.projectService.get(params.uuid);
429 resource = await params.services.containerRequestService.get(params.uuid);
431 handler = groupContentsHandlers.SHARED(resource);
433 return (cases: MatchCases<typeof groupContentsHandlersRecord, GroupContentsHandler, void>) =>
434 groupContentsHandlers.match(handler, cases);
438 const groupContentsHandlersRecord = {
439 TRASHED: ofType<GroupContentsResource>(),
440 SHARED: ofType<GroupContentsResource>(),
441 OWNED: ofType<GroupContentsResource>(),
444 const groupContentsHandlers = unionize(groupContentsHandlersRecord);
446 type GroupContentsHandler = UnionOf<typeof groupContentsHandlers>;