import { useNavigate, useParams } from "react-router-dom"; import { NetworkApi, NetworkInfo, NetworkURL } from "../api/NetworksApi"; import { useAlert } from "../hooks/providers/AlertDialogProvider"; import { useSnackbar } from "../hooks/providers/SnackbarProvider"; import React from "react"; import { AsyncWidget } from "../widgets/AsyncWidget"; import { NetworkDetails } from "../widgets/net/NetworkDetails"; import { VirtWebRouteContainer } from "../widgets/VirtWebRouteContainer"; import { Button } from "@mui/material"; import { ConfigImportExportButtons } from "../widgets/ConfigImportExportButtons"; export function CreateNetworkRoute(): React.ReactElement { const alert = useAlert(); const snackbar = useSnackbar(); const navigate = useNavigate(); const [network, setNetwork] = React.useState({ name: "NewNetwork", forward_mode: "Isolated", }); const createNetwork = async (n: NetworkInfo) => { try { const res = await NetworkApi.Create(n); snackbar("The network was successfully created!"); navigate(`/net/${res.uid}`); } catch (e) { console.error(e); alert(`Failed to create network!\n${e}`); } }; return ( navigate("/net")} onSave={createNetwork} onReplace={setNetwork} /> ); } export function EditNetworkRoute(): React.ReactElement { const alert = useAlert(); const snackbar = useSnackbar(); const { uuid } = useParams(); const navigate = useNavigate(); const [network, setNetwork] = React.useState(); const load = async () => { setNetwork(await NetworkApi.GetSingle(uuid!)); }; const updateNetwork = async (n: NetworkInfo) => { try { await NetworkApi.Update(n); snackbar("The network was successfully updated!"); navigate(NetworkURL(network!)); } catch (e) { console.error(e); alert(`Failed to update network!\n${e}`); } }; return ( ( navigate(`/net/${uuid}`)} onSave={updateNetwork} onReplace={setNetwork} /> )} /> ); } function EditNetworkRouteInner(p: { network: NetworkInfo; creating: boolean; onCancel: () => void; onSave: (vm: NetworkInfo) => Promise; onReplace: (vm: NetworkInfo) => void; }): React.ReactElement { const [changed, setChanged] = React.useState(false); const [, updateState] = React.useState(); const forceUpdate = React.useCallback(() => updateState({}), []); const valueChanged = () => { setChanged(true); forceUpdate(); }; return ( { p.onReplace(c); valueChanged(); }} /> {changed && ( )} } > ); }