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/store';
7 import { ServiceRepository } from "~/services/services";
8 import { navigateToVirtualMachines } from "../navigation/navigation-action";
9 import { bindDataExplorerActions } from '~/store/data-explorer/data-explorer-action';
10 import { formatDate } from "~/common/formatters";
11 import { unionize, ofType, UnionOf } from "~/common/unionize";
12 import { VirtualMachineLogins } from '~/models/virtual-machines';
13 import { FilterBuilder } from "~/services/api/filter-builder";
14 import { ListResults } from "~/services/common-service/common-service";
15 import { dialogActions } from '~/store/dialog/dialog-actions';
16 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
18 export const virtualMachinesActions = unionize({
19 SET_REQUESTED_DATE: ofType<string>(),
20 SET_VIRTUAL_MACHINES: ofType<ListResults<any>>(),
21 SET_LOGINS: ofType<VirtualMachineLogins>(),
22 SET_LINKS: ofType<ListResults<any>>()
25 export type VirtualMachineActions = UnionOf<typeof virtualMachinesActions>;
27 export const VIRTUAL_MACHINES_PANEL = 'virtualMachinesPanel';
28 export const VIRTUAL_MACHINE_ATTRIBUTES_DIALOG = 'virtualMachineAttributesDialog';
29 export const VIRTUAL_MACHINE_REMOVE_DIALOG = 'virtualMachineRemoveDialog';
31 export const openVirtualMachines = () =>
32 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
33 dispatch<any>(navigateToVirtualMachines);
36 export const openVirtualMachineAttributes = (uuid: string) =>
37 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
38 const virtualMachineData = getState().virtualMachines.virtualMachines.items.find(it => it.uuid === uuid);
39 dispatch(dialogActions.OPEN_DIALOG({ id: VIRTUAL_MACHINE_ATTRIBUTES_DIALOG, data: { virtualMachineData } }));
42 const loadRequestedDate = () =>
43 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
44 const date = services.virtualMachineService.getRequestedDate();
45 dispatch(virtualMachinesActions.SET_REQUESTED_DATE(date));
48 export const loadVirtualMachinesData = () =>
49 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
50 dispatch<any>(loadRequestedDate());
51 const virtualMachines = await services.virtualMachineService.list();
52 const virtualMachinesUuids = virtualMachines.items.map(it => it.uuid);
53 const links = await services.linkService.list({
54 filters: new FilterBuilder()
55 .addIn("headUuid", virtualMachinesUuids)
58 dispatch(virtualMachinesActions.SET_VIRTUAL_MACHINES(virtualMachines));
59 dispatch(virtualMachinesActions.SET_LINKS(links));
60 const getAllLogins = await services.virtualMachineService.getAllLogins();
61 dispatch(virtualMachinesActions.SET_LOGINS(getAllLogins));
64 export const saveRequestedDate = () =>
65 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
66 const date = formatDate((new Date).toISOString());
67 services.virtualMachineService.saveRequestedDate(date);
68 dispatch<any>(loadRequestedDate());
71 export const openRemoveVirtualMachineDialog = (uuid: string) =>
72 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
73 dispatch(dialogActions.OPEN_DIALOG({
74 id: VIRTUAL_MACHINE_REMOVE_DIALOG,
76 title: 'Remove virtual machine',
77 text: 'Are you sure you want to remove this virtual machine?',
78 confirmButtonLabel: 'Remove',
84 export const removeVirtualMachine = (uuid: string) =>
85 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
86 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...' }));
87 await services.virtualMachineService.delete(uuid);
88 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
89 dispatch<any>(loadVirtualMachinesData());
92 const virtualMachinesBindedActions = bindDataExplorerActions(VIRTUAL_MACHINES_PANEL);
94 export const loadVirtualMachinesPanel = () =>
95 (dispatch: Dispatch) => {
96 dispatch(virtualMachinesBindedActions.REQUEST_ITEMS());