2023-12-04 19:16:32 +00:00
|
|
|
import { useNavigate, useParams } from "react-router-dom";
|
2023-12-06 14:30:30 +00:00
|
|
|
import { NetworkApi, NetworkInfo, NetworkURL } from "../api/NetworksApi";
|
2023-12-04 19:16:32 +00:00
|
|
|
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";
|
2023-12-23 16:39:52 +00:00
|
|
|
import { ConfigImportExportButtons } from "../widgets/ConfigImportExportButtons";
|
2023-12-04 19:16:32 +00:00
|
|
|
|
|
|
|
export function CreateNetworkRoute(): React.ReactElement {
|
|
|
|
const alert = useAlert();
|
|
|
|
const snackbar = useSnackbar();
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
2023-12-23 16:39:52 +00:00
|
|
|
const [network, setNetwork] = React.useState<NetworkInfo>({
|
2023-12-04 19:16:32 +00:00
|
|
|
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);
|
2023-12-12 00:32:18 +00:00
|
|
|
alert(`Failed to create network!\n${e}`);
|
2023-12-04 19:16:32 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<EditNetworkRouteInner
|
|
|
|
network={network}
|
|
|
|
creating={true}
|
|
|
|
onCancel={() => navigate("/net")}
|
|
|
|
onSave={createNetwork}
|
2023-12-23 16:39:52 +00:00
|
|
|
onReplace={setNetwork}
|
2023-12-04 19:16:32 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function EditNetworkRoute(): React.ReactElement {
|
|
|
|
const alert = useAlert();
|
|
|
|
const snackbar = useSnackbar();
|
|
|
|
|
|
|
|
const { uuid } = useParams();
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
|
|
|
const [network, setNetwork] = React.useState<NetworkInfo | undefined>();
|
|
|
|
|
|
|
|
const load = async () => {
|
|
|
|
setNetwork(await NetworkApi.GetSingle(uuid!));
|
|
|
|
};
|
|
|
|
|
|
|
|
const updateNetwork = async (n: NetworkInfo) => {
|
|
|
|
try {
|
|
|
|
await NetworkApi.Update(n);
|
|
|
|
snackbar("The network was successfully updated!");
|
2023-12-06 14:30:30 +00:00
|
|
|
navigate(NetworkURL(network!));
|
2023-12-04 19:16:32 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
2023-12-12 00:32:18 +00:00
|
|
|
alert(`Failed to update network!\n${e}`);
|
2023-12-04 19:16:32 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<AsyncWidget
|
|
|
|
loadKey={uuid}
|
|
|
|
ready={network !== undefined}
|
|
|
|
errMsg="Failed to fetch network information!"
|
|
|
|
load={load}
|
|
|
|
build={() => (
|
|
|
|
<EditNetworkRouteInner
|
|
|
|
network={network!}
|
|
|
|
creating={false}
|
|
|
|
onCancel={() => navigate(`/net/${uuid}`)}
|
|
|
|
onSave={updateNetwork}
|
2023-12-23 16:39:52 +00:00
|
|
|
onReplace={setNetwork}
|
2023-12-04 19:16:32 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function EditNetworkRouteInner(p: {
|
|
|
|
network: NetworkInfo;
|
|
|
|
creating: boolean;
|
|
|
|
onCancel: () => void;
|
|
|
|
onSave: (vm: NetworkInfo) => Promise<void>;
|
2023-12-23 16:39:52 +00:00
|
|
|
onReplace: (vm: NetworkInfo) => void;
|
2023-12-04 19:16:32 +00:00
|
|
|
}): React.ReactElement {
|
|
|
|
const [changed, setChanged] = React.useState(false);
|
|
|
|
|
|
|
|
const [, updateState] = React.useState<any>();
|
|
|
|
const forceUpdate = React.useCallback(() => updateState({}), []);
|
|
|
|
|
|
|
|
const valueChanged = () => {
|
|
|
|
setChanged(true);
|
|
|
|
forceUpdate();
|
|
|
|
};
|
|
|
|
return (
|
|
|
|
<VirtWebRouteContainer
|
|
|
|
label={p.creating ? "Create a Network" : "Edit Network"}
|
|
|
|
actions={
|
|
|
|
<span>
|
2023-12-23 16:39:52 +00:00
|
|
|
<ConfigImportExportButtons
|
|
|
|
currentConf={p.network}
|
|
|
|
filename={`net-${p.network.name}.json`}
|
|
|
|
importConf={(c) => {
|
|
|
|
p.onReplace(c);
|
|
|
|
valueChanged();
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
|
2023-12-04 19:16:32 +00:00
|
|
|
{changed && (
|
|
|
|
<Button
|
|
|
|
variant="contained"
|
|
|
|
onClick={() => p.onSave(p.network)}
|
|
|
|
style={{ marginRight: "10px" }}
|
|
|
|
>
|
|
|
|
{p.creating ? "Create" : "Save"}
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
<Button onClick={p.onCancel} variant="outlined">
|
|
|
|
Cancel
|
|
|
|
</Button>
|
|
|
|
</span>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<NetworkDetails net={p.network} editable={true} onChange={valueChanged} />
|
|
|
|
</VirtWebRouteContainer>
|
|
|
|
);
|
|
|
|
}
|