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 { progressIndicatorActions } from '~/store/progress-indicator/progress-indicator-actions';
42 import { getProgressIndicator } from '../progress-indicator/progress-indicator-reducer';
43 import { ResourceKind, extractUuidKind } from '~/models/resource';
44 import { FilterBuilder } from '~/services/api/filter-builder';
45 import { GroupContentsResource } from '~/services/groups-service/groups-service';
46 import { unionize, ofType, UnionOf, MatchCases } from '~/common/unionize';
47 import { loadRunProcessPanel } from '~/store/run-process-panel/run-process-panel-actions';
49 export const WORKBENCH_LOADING_SCREEN = 'workbenchLoadingScreen';
51 export const isWorkbenchLoading = (state: RootState) => {
52 const progress = getProgressIndicator(WORKBENCH_LOADING_SCREEN)(state.progressIndicator);
53 return progress ? progress.working : false;
56 const handleFirstTimeLoad = (action: any) =>
57 async (dispatch: Dispatch<any>, getState: () => RootState) => {
59 await dispatch(action);
61 if (isWorkbenchLoading(getState())) {
62 dispatch(progressIndicatorActions.STOP_WORKING(WORKBENCH_LOADING_SCREEN));
68 export const loadWorkbench = () =>
69 async (dispatch: Dispatch, getState: () => RootState) => {
70 dispatch(progressIndicatorActions.START_WORKING(WORKBENCH_LOADING_SCREEN));
71 const { auth, router } = getState();
72 const { user } = auth;
74 const userResource = await dispatch<any>(loadResource(user.uuid));
76 dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
77 dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
78 dispatch(trashPanelActions.SET_COLUMNS({ columns: trashPanelColumns }));
79 dispatch(sharedWithMePanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
80 dispatch<any>(initSidePanelTree());
81 if (router.location) {
82 const match = matchRootRoute(router.location.pathname);
84 dispatch(navigateToProject(userResource.uuid));
88 dispatch(userIsNotAuthenticated);
91 dispatch(userIsNotAuthenticated);
95 export const loadFavorites = () =>
97 (dispatch: Dispatch) => {
98 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
99 dispatch<any>(loadFavoritePanel());
100 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
103 export const loadTrash = () =>
105 (dispatch: Dispatch) => {
106 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
107 dispatch<any>(loadTrashPanel());
108 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.TRASH));
111 export const loadProject = (uuid: string) =>
113 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
114 const userUuid = services.authService.getUuid();
116 if (userUuid !== uuid) {
117 const match = await loadGroupContentsResource({ uuid, userUuid, services });
119 OWNED: async project => {
120 await dispatch(activateSidePanelTreeItem(uuid));
121 dispatch<any>(setSidePanelBreadcrumbs(uuid));
122 dispatch(finishLoadingProject(project));
125 dispatch<any>(setSharedWithMeBreadcrumbs(uuid));
126 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
127 dispatch(finishLoadingProject(project));
129 TRASHED: project => {
130 dispatch<any>(setTrashBreadcrumbs(uuid));
131 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
132 dispatch(finishLoadingProject(project));
136 await dispatch(activateSidePanelTreeItem(userUuid));
137 dispatch<any>(setSidePanelBreadcrumbs(userUuid));
138 dispatch(finishLoadingProject(userUuid));
143 export const createProject = (data: projectCreateActions.ProjectCreateFormDialogData) =>
144 async (dispatch: Dispatch) => {
145 const newProject = await dispatch<any>(projectCreateActions.createProject(data));
147 dispatch(snackbarActions.OPEN_SNACKBAR({
148 message: "Project has been successfully created.",
151 await dispatch<any>(loadSidePanelTreeProjects(newProject.ownerUuid));
152 dispatch<any>(reloadProjectMatchingUuid([newProject.ownerUuid]));
156 export const moveProject = (data: MoveToFormDialogData) =>
157 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
159 const oldProject = getResource(data.uuid)(getState().resources);
160 const oldOwnerUuid = oldProject ? oldProject.ownerUuid : '';
161 const movedProject = await dispatch<any>(projectMoveActions.moveProject(data));
163 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Project has been moved', hideDuration: 2000 }));
165 await dispatch<any>(loadSidePanelTreeProjects(oldProject.ownerUuid));
167 dispatch<any>(reloadProjectMatchingUuid([oldOwnerUuid, movedProject.ownerUuid, movedProject.uuid]));
170 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
174 export const updateProject = (data: projectUpdateActions.ProjectUpdateFormDialogData) =>
175 async (dispatch: Dispatch) => {
176 const updatedProject = await dispatch<any>(projectUpdateActions.updateProject(data));
177 if (updatedProject) {
178 dispatch(snackbarActions.OPEN_SNACKBAR({
179 message: "Project has been successfully updated.",
182 await dispatch<any>(loadSidePanelTreeProjects(updatedProject.ownerUuid));
183 dispatch<any>(reloadProjectMatchingUuid([updatedProject.ownerUuid, updatedProject.uuid]));
187 export const loadCollection = (uuid: string) =>
189 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
190 const userUuid = services.authService.getUuid();
192 const match = await loadGroupContentsResource({ uuid, userUuid, services });
194 OWNED: async collection => {
195 dispatch(updateResources([collection]));
196 await dispatch(activateSidePanelTreeItem(collection.ownerUuid));
197 dispatch(setSidePanelBreadcrumbs(collection.ownerUuid));
199 SHARED: collection => {
200 dispatch(updateResources([collection]));
201 dispatch<any>(setSharedWithMeBreadcrumbs(collection.ownerUuid));
202 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
204 TRASHED: collection => {
205 dispatch(updateResources([collection]));
206 dispatch(setTrashBreadcrumbs(''));
207 dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.TRASH));
214 export const createCollection = (data: collectionCreateActions.CollectionCreateFormDialogData) =>
215 async (dispatch: Dispatch) => {
216 const collection = await dispatch<any>(collectionCreateActions.createCollection(data));
218 dispatch(snackbarActions.OPEN_SNACKBAR({
219 message: "Collection has been successfully created.",
222 dispatch<any>(updateResources([collection]));
223 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
227 export const updateCollection = (data: collectionUpdateActions.CollectionUpdateFormDialogData) =>
228 async (dispatch: Dispatch) => {
229 const collection = await dispatch<any>(collectionUpdateActions.updateCollection(data));
231 dispatch(snackbarActions.OPEN_SNACKBAR({
232 message: "Collection has been successfully updated.",
235 dispatch<any>(updateResources([collection]));
236 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
240 export const copyCollection = (data: CopyFormDialogData) =>
241 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
243 const collection = await dispatch<any>(collectionCopyActions.copyCollection(data));
244 dispatch<any>(updateResources([collection]));
245 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
246 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied.', hideDuration: 2000 }));
248 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
252 export const moveCollection = (data: MoveToFormDialogData) =>
253 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
255 const collection = await dispatch<any>(collectionMoveActions.moveCollection(data));
256 dispatch<any>(updateResources([collection]));
257 dispatch<any>(reloadProjectMatchingUuid([collection.ownerUuid]));
258 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been moved.', hideDuration: 2000 }));
260 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
264 export const loadProcess = (uuid: string) =>
266 async (dispatch: Dispatch, getState: () => RootState) => {
267 dispatch<any>(loadProcessPanel(uuid));
268 const process = await dispatch<any>(processesActions.loadProcess(uuid));
269 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
270 dispatch<any>(setProcessBreadcrumbs(uuid));
271 dispatch(loadDetailsPanel(uuid));
274 export const updateProcess = (data: processUpdateActions.ProcessUpdateFormDialogData) =>
275 async (dispatch: Dispatch) => {
277 const process = await dispatch<any>(processUpdateActions.updateProcess(data));
279 dispatch(snackbarActions.OPEN_SNACKBAR({
280 message: "Process has been successfully updated.",
283 dispatch<any>(updateResources([process]));
284 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
287 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
291 export const moveProcess = (data: MoveToFormDialogData) =>
292 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
294 const process = await dispatch<any>(processMoveActions.moveProcess(data));
295 dispatch<any>(updateResources([process]));
296 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
297 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been moved.', hideDuration: 2000 }));
299 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
303 export const copyProcess = (data: CopyFormDialogData) =>
304 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
306 const process = await dispatch<any>(processCopyActions.copyProcess(data));
307 dispatch<any>(updateResources([process]));
308 dispatch<any>(reloadProjectMatchingUuid([process.ownerUuid]));
309 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Process has been copied.', hideDuration: 2000 }));
311 dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, hideDuration: 2000 }));
315 export const loadProcessLog = (uuid: string) =>
317 async (dispatch: Dispatch) => {
318 const process = await dispatch<any>(processesActions.loadProcess(uuid));
319 dispatch<any>(setProcessBreadcrumbs(uuid));
320 dispatch<any>(initProcessLogsPanel(uuid));
321 await dispatch<any>(activateSidePanelTreeItem(process.containerRequest.ownerUuid));
324 export const resourceIsNotLoaded = (uuid: string) =>
325 snackbarActions.OPEN_SNACKBAR({
326 message: `Resource identified by ${uuid} is not loaded.`
329 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
330 message: 'User is not authenticated'
333 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
334 message: 'Could not load user'
337 export const reloadProjectMatchingUuid = (matchingUuids: string[]) =>
338 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
339 const currentProjectPanelUuid = getProjectPanelCurrentUuid(getState());
340 if (currentProjectPanelUuid && matchingUuids.some(uuid => uuid === currentProjectPanelUuid)) {
341 dispatch<any>(loadProject(currentProjectPanelUuid));
345 export const loadSharedWithMe = handleFirstTimeLoad(async (dispatch: Dispatch) => {
346 dispatch<any>(loadSharedWithMePanel());
347 await dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.SHARED_WITH_ME));
348 await dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.SHARED_WITH_ME));
351 export const loadRunProcess = handleFirstTimeLoad(
352 async (dispatch: Dispatch) => {
353 dispatch<any>(loadRunProcessPanel());
357 const finishLoadingProject = (project: GroupContentsResource | string) =>
358 async (dispatch: Dispatch<any>) => {
359 const uuid = typeof project === 'string' ? project : project.uuid;
360 dispatch(openProjectPanel(uuid));
361 dispatch(loadDetailsPanel(uuid));
362 if (typeof project !== 'string') {
363 dispatch(updateResources([project]));
367 const loadGroupContentsResource = async (params: {
370 services: ServiceRepository
372 const filters = new FilterBuilder()
373 .addEqual('uuid', params.uuid)
375 const { items } = await params.services.groupsService.contents(params.userUuid, {
380 const resource = items.shift();
381 let handler: GroupContentsHandler;
383 handler = (resource.kind === ResourceKind.COLLECTION || resource.kind === ResourceKind.PROJECT) && resource.isTrashed
384 ? groupContentsHandlers.TRASHED(resource)
385 : groupContentsHandlers.OWNED(resource);
387 const kind = extractUuidKind(params.uuid);
388 let resource: GroupContentsResource;
389 if (kind === ResourceKind.COLLECTION) {
390 resource = await params.services.collectionService.get(params.uuid);
391 } else if (kind === ResourceKind.PROJECT) {
392 resource = await params.services.projectService.get(params.uuid);
394 resource = await params.services.containerRequestService.get(params.uuid);
396 handler = groupContentsHandlers.SHARED(resource);
398 return (cases: MatchCases<typeof groupContentsHandlersRecord, GroupContentsHandler, void>) =>
399 groupContentsHandlers.match(handler, cases);
403 const groupContentsHandlersRecord = {
404 TRASHED: ofType<GroupContentsResource>(),
405 SHARED: ofType<GroupContentsResource>(),
406 OWNED: ofType<GroupContentsResource>(),
409 const groupContentsHandlers = unionize(groupContentsHandlersRecord);
411 type GroupContentsHandler = UnionOf<typeof groupContentsHandlers>;