isAdmin prop
[arvados-workbench2.git] / src / store / virtual-machines / virtual-machines-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 { 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";
15
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>>()
21 });
22
23 export type VirtualMachineActions = UnionOf<typeof virtualMachinesActions>;
24
25 export const VIRTUAL_MACHINES_PANEL = 'virtualMachinesPanel';
26
27 export const openVirtualMachines = () =>
28     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
29         dispatch<any>(navigateToVirtualMachines);
30     };
31
32 const loadRequestedDate = () =>
33     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
34         const date = services.virtualMachineService.getRequestedDate();
35         dispatch(virtualMachinesActions.SET_REQUESTED_DATE(date));
36     };
37
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)
46                 .getFilters()
47         });
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));
53     };
54
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());
60     };
61
62 const virtualMachinesBindedActions = bindDataExplorerActions(VIRTUAL_MACHINES_PANEL);
63
64 export const loadVirtualMachinesPanel = () =>
65     (dispatch: Dispatch) => {
66         dispatch(virtualMachinesBindedActions.REQUEST_ITEMS());
67     };