import { Button } from "@mui/material"; import React from "react"; import { useNavigate, useParams } from "react-router-dom"; import { VMApi, VMInfo } from "../api/VMApi"; import { useAlert } from "../hooks/providers/AlertDialogProvider"; import { useSnackbar } from "../hooks/providers/SnackbarProvider"; import { AsyncWidget } from "../widgets/AsyncWidget"; import { VirtWebRouteContainer } from "../widgets/VirtWebRouteContainer"; import { VMDetails } from "../widgets/vms/VMDetails"; export function CreateVMRoute(): React.ReactElement { const snackbar = useSnackbar(); const alert = useAlert(); const navigate = useNavigate(); const [vm] = React.useState(VMInfo.NewEmpty); const create = async (v: VMInfo) => { try { const res = await VMApi.Create(v); snackbar("The virtual machine was successfully created!"); v.uuid = res.uuid; navigate(v.ViewURL); } catch (e) { console.error(e); alert(`Failed to create VM!\n${e}`); } }; return ( navigate("/vms")} /> ); } export function EditVMRoute(): React.ReactElement { const navigate = useNavigate(); const alert = useAlert(); const snackbar = useSnackbar(); const { uuid } = useParams(); const [vm, setVM] = React.useState(undefined); const load = async () => { const vm = await VMApi.GetSingle(uuid!); setVM(vm); const state = await VMApi.GetState(vm); if (state !== "Shutdown" && state !== "Shutoff") { await alert("The Virtual machine is running, you cannot edit it!"); navigate(vm.ViewURL); } }; const save = async (v: VMInfo) => { try { await VMApi.UpdateSingle(v); snackbar("The virtual machine was successfully updated!"); navigate(v.ViewURL); } catch (e) { console.error(e); alert("Failed to update VM info!"); } }; return ( ( { navigate(vm!.ViewURL); }} onSave={save} /> )} /> ); } function EditVMInner(p: { vm: VMInfo; isCreating: boolean; onCancel: () => void; onSave: (vm: VMInfo) => Promise; }): React.ReactElement { const [changed, setChanged] = React.useState(false); const [, updateState] = React.useState(); const forceUpdate = React.useCallback(() => updateState({}), []); const valueChanged = () => { setChanged(true); forceUpdate(); }; return ( {changed && ( )} } > ); }