Merge branch '14470-update-tree-pickers-to-handle-shared-items' into 14470-replace...
[arvados.git] / src / store / processes / processes-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Dispatch } from "redux";
6 import { RootState } from '~/store/store';
7 import { ServiceRepository } from '~/services/services';
8 import { updateResources } from '~/store/resources/resources-actions';
9 import { FilterBuilder } from '~/services/api/filter-builder';
10 import { ContainerRequestResource } from '~/models/container-request';
11 import { Process } from './process';
12 import { dialogActions } from '~/store/dialog/dialog-actions';
13 import { snackbarActions } from '~/store/snackbar/snackbar-actions';
14 import { projectPanelActions } from '~/store/project-panel/project-panel-action';
15
16 export const loadProcess = (containerRequestUuid: string) =>
17     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<Process> => {
18         const containerRequest = await services.containerRequestService.get(containerRequestUuid);
19         dispatch<any>(updateResources([containerRequest]));
20         if (containerRequest.containerUuid) {
21             const container = await services.containerService.get(containerRequest.containerUuid);
22             dispatch<any>(updateResources([container]));
23             await dispatch<any>(loadSubprocesses(containerRequest.containerUuid));
24             return { containerRequest, container };
25         }
26         return { containerRequest };
27     };
28
29 export const loadSubprocesses = (containerUuid: string) =>
30     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
31         const containerRequests = await dispatch<any>(loadContainerRequests(
32             new FilterBuilder().addEqual('requestingContainerUuid', containerUuid).getFilters()
33         )) as ContainerRequestResource[];
34
35         const containerUuids: string[] = containerRequests.reduce((uuids, { containerUuid }) =>
36             containerUuid
37                 ? [...uuids, containerUuid]
38                 : uuids, []);
39
40         if (containerUuids.length > 0) {
41             await dispatch<any>(loadContainers(
42                 new FilterBuilder().addIn('uuid', containerUuids).getFilters()
43             ));
44         }
45     };
46
47 export const loadContainerRequests = (filters: string) =>
48     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
49         const { items } = await services.containerRequestService.list({ filters });
50         dispatch<any>(updateResources(items));
51         return items;
52     };
53
54 export const loadContainers = (filters: string) =>
55     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
56         const { items } = await services.containerService.list({ filters });
57         dispatch<any>(updateResources(items));
58         return items;
59     };
60
61 export const openRemoveProcessDialog = (uuid: string) =>
62     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
63         dispatch(dialogActions.OPEN_DIALOG({
64             id: REMOVE_PROCESS_DIALOG,
65             data: {
66                 title: 'Remove process permanently',
67                 text: 'Are you sure you want to remove this process?',
68                 confirmButtonLabel: 'Remove',
69                 uuid
70             }
71         }));
72     };
73
74 export const REMOVE_PROCESS_DIALOG = 'removeProcessDialog';
75
76 export const removeProcessPermanently = (uuid: string) =>
77     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) =>{
78         dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...' }));
79         await services.containerRequestService.delete(uuid);
80         dispatch(projectPanelActions.REQUEST_ITEMS());
81         dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000 }));
82     };
83         
84