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 oldProject = getResource(data.ownerUuid)(getState().resources);
261 const collection = await dispatch<any>(collectionCopyActions.copyCollection(data));
262 // dispatch<any>(updateResources([collection]));
263 if (oldProject && collection) {
264 dispatch<any>(reloadProjectMatchingUuid([oldProject.uuid]));
265 dispatch(snackbarActions.OPEN_SNACKBAR({
266 message: 'Collection has been copied.',
268 kind: SnackbarKind.SUCCESS,
269 link: collection.ownerUuid
273 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000, kind: SnackbarKind.ERROR }));
277 export const moveCollection = (data: MoveToFormDialogData) =>
278 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
280 const collection = await dispatch<any>(collectionMoveActions.moveCollection(data));
281 dispatch<any>(updateResources([collection]));
282 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
283 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been moved.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
285 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
289 export const loadProcess = (uuid: string) =>
291 async (dispatch: Dispatch, getState: () => RootState) => {
292 dispatch<any>(loadProcessPanel(uuid));
293 const process = await dispatch<any>(processesActions.loadProcess(uuid));
294 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
295 dispatch<any>(setProcessBreadcrumbs(uuid));
296 dispatch(loadDetailsPanel(uuid));
299 export const updateProcess = (data: processUpdateActions.ProcessUpdateFormDialogData) =>
300 async (dispatch: Dispatch) => {
302 const process = await dispatch<any>(processUpdateActions.updateProcess(data));
304 dispatch(snackbarActions.OPEN_SNACKBAR({
305 message: "Process has been successfully updated.",
308 dispatch<any>(updateResources([process]));
309 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
312 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
316 export const moveProcess = (data: MoveToFormDialogData) =>
317 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
319 const process = await dispatch<any>(processMoveActions.moveProcess(data));
320 dispatch<any>(updateResources([process]));
321 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
322 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been moved.', hideDuration: 2000 }));
324 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
328 export const copyProcess = (data: CopyFormDialogData) =>
329 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
331 const process = await dispatch<any>(processCopyActions.copyProcess(data));
332 dispatch<any>(updateResources([process]));
333 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
334 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been copied.', hideDuration: 2000 }));
336 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
340 export const loadProcessLog = (uuid: string) =>
342 async (dispatch: Dispatch) => {
343 const process = await dispatch<any>(processesActions.loadProcess(uuid));
344 dispatch<any>(setProcessBreadcrumbs(uuid));
345 dispatch<any>(initProcessLogsPanel(uuid));
346 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
349 export const resourceIsNotLoaded = (uuid: string) =>
350 snackbarActions.OPEN_SNACKBAR({
351 message: `Resource identified by ${uuid} is not loaded.`
354 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
355 message: 'User is not authenticated'
358 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
359 message: 'Could not load user'
362 export const reloadProjectMatchingUuid = (matchingUuids: string[]) =>
363 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
364 const currentProjectPanelUuid = getProjectPanelCurrentUuid(getState());
365 if (currentProjectPanelUuid && matchingUuids.some(uuid => uuid === currentProjectPanelUuid)) {
366 dispatch<any>(loadProject(currentProjectPanelUuid));
370 export const loadSharedWithMe = handleFirstTimeLoad(async (dispatch: Dispatch) => {
371 dispatch<any>(loadSharedWithMePanel());
372 await dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
373 await dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.SHARED_WITH_ME));
376 export const loadRunProcess = handleFirstTimeLoad(
377 async (dispatch: Dispatch) => {
378 await dispatch<any>(loadRunProcessPanel());
382 export const loadWorkflow = handleFirstTimeLoad(async (dispatch: Dispatch<any>) => {
383 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.WORKFLOWS));
384 await dispatch(loadWorkflowPanel());
385 dispatch(setSidePanelBreadcrumbs(SidePanelTreeCategory.WORKFLOWS));
388 export const loadSearchResults = handleFirstTimeLoad(
389 async (dispatch: Dispatch<any>) => {
390 await dispatch(loadSearchResultsPanel());
393 const finishLoadingProject = (project: GroupContentsResource | string) =>
394 async (dispatch: Dispatch<any>) => {
395 const uuid = typeof project === 'string' ? project : project.uuid;
396 dispatch(openProjectPanel(uuid));
397 dispatch(loadDetailsPanel(uuid));
398 if (typeof project !== 'string') {
399 dispatch(updateResources([project]));
403 const loadGroupContentsResource = async (params: {
406 services: ServiceRepository
408 const filters = new FilterBuilder()
409 .addEqual('uuid', params.uuid)
411 const { items } = await params.services.groupsService.contents(params.userUuid, {
416 const resource = items.shift();
417 let handler: GroupContentsHandler;
419 handler = (resource.kind === ResourceKind.COLLECTION || resource.kind === ResourceKind.PROJECT) && resource.isTrashed
420 ? groupContentsHandlers.TRASHED(resource)
421 : groupContentsHandlers.OWNED(resource);
423 const kind = extractUuidKind(params.uuid);
424 let resource: GroupContentsResource;
425 if (kind === ResourceKind.COLLECTION) {
426 resource = await params.services.collectionService.get(params.uuid);
427 } else if (kind === ResourceKind.PROJECT) {
428 resource = await params.services.projectService.get(params.uuid);
430 resource = await params.services.containerRequestService.get(params.uuid);
432 handler = groupContentsHandlers.SHARED(resource);
434 return (cases: MatchCases<typeof groupContentsHandlersRecord, GroupContentsHandler, void>) =>
435 groupContentsHandlers.match(handler, cases);
439 const groupContentsHandlersRecord = {
440 TRASHED: ofType<GroupContentsResource>(),
441 SHARED: ofType<GroupContentsResource>(),
442 OWNED: ofType<GroupContentsResource>(),
445 const groupContentsHandlers = unionize(groupContentsHandlersRecord);
447 type GroupContentsHandler = UnionOf<typeof groupContentsHandlers>;