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 { VirtualMachinesLoginsResource } from '~/models/virtual-machines';
13 import { FilterBuilder } from "~/services/api/filter-builder";
14 import { ListResults } from "~/services/common-service/common-resource-service";
16 export const virtualMachinesActions = unionize({
17 SET_REQUESTED_DATE: ofType<string>(),
18 SET_VIRTUAL_MACHINES: ofType<ListResults<any>>(),
19 SET_LOGINS: ofType<VirtualMachinesLoginsResource[]>(),
20 SET_LINKS: ofType<ListResults<any>>()
23 export type VirtualMachineActions = UnionOf<typeof virtualMachinesActions>;
25 export const VIRTUAL_MACHINES_PANEL = 'virtualMachinesPanel';
27 export const openVirtualMachines = () =>
28 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
29 dispatch<any>(navigateToVirtualMachines);
32 const loadRequestedDate = () =>
33 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
34 const date = services.virtualMachineService.getRequestedDate();
35 dispatch(virtualMachinesActions.SET_REQUESTED_DATE(date));
38 export const loadVirtualMachinesData = () =>
39 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
40 dispatch<any>(loadRequestedDate());
41 const virtualMachines = await services.virtualMachineService.list();
42 const virtualMachinesUuids = virtualMachines.items.map(it => it.uuid);
43 const links = await services.linkService.list({
44 filters: new FilterBuilder()
45 .addIn("headUuid", virtualMachinesUuids)
48 dispatch(virtualMachinesActions.SET_VIRTUAL_MACHINES(virtualMachines));
49 dispatch(virtualMachinesActions.SET_LINKS(links));
50 const getAllLogins = await services.virtualMachineService.getAllLogins();
51 console.log(getAllLogins);
52 dispatch(virtualMachinesActions.SET_LOGINS(getAllLogins));
55 export const saveRequestedDate = () =>
56 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
57 const date = formatDate((new Date).toISOString());
58 services.virtualMachineService.saveRequestedDate(date);
59 dispatch<any>(loadRequestedDate());
62 const virtualMachinesBindedActions = bindDataExplorerActions(VIRTUAL_MACHINES_PANEL);
64 export const loadVirtualMachinesPanel = () =>
65 (dispatch: Dispatch) => {
66 dispatch(virtualMachinesBindedActions.REQUEST_ITEMS());